Commit 3a93144f authored by Pascal Hartig's avatar Pascal Hartig

Merge pull request #1481 from tastejs/react/controlled-input-component

React: Remove direct DOM manipulation
parents aff74a63 15db45e6
......@@ -20,7 +20,8 @@ var app = app || {};
getInitialState: function () {
return {
nowShowing: app.ALL_TODOS,
editing: null
editing: null,
newTodo: ''
};
},
......@@ -34,6 +35,10 @@ var app = app || {};
router.init('/');
},
handleChange: function (event) {
this.setState({newTodo: event.target.value});
},
handleNewTodoKeyDown: function (event) {
if (event.keyCode !== ENTER_KEY) {
return;
......@@ -41,11 +46,11 @@ var app = app || {};
event.preventDefault();
var val = React.findDOMNode(this.refs.newField).value.trim();
var val = this.state.newTodo.trim();
if (val) {
this.props.model.addTodo(val);
React.findDOMNode(this.refs.newField).value = '';
this.setState({newTodo: ''});
}
},
......@@ -147,10 +152,11 @@ var app = app || {};
<header className="header">
<h1>todos</h1>
<input
ref="newField"
className="new-todo"
placeholder="What needs to be done?"
value={this.state.newTodo}
onKeyDown={this.handleNewTodoKeyDown}
onChange={this.handleChange}
autoFocus={true}
/>
</header>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment