|
|
@ -1,41 +1,56 @@ |
|
|
|
var TODO_COMPONENT = ` |
|
|
|
var TodoList = React.createClass({ |
|
|
|
render: function() { |
|
|
|
var createItem = function(item) { |
|
|
|
return <li key={item.id}>{item.text}</li>; |
|
|
|
}; |
|
|
|
return <ul>{this.props.items.map(createItem)}</ul>; |
|
|
|
class TodoApp extends React.Component { |
|
|
|
constructor(props) { |
|
|
|
super(props); |
|
|
|
this.handleChange = this.handleChange.bind(this); |
|
|
|
this.handleSubmit = this.handleSubmit.bind(this); |
|
|
|
this.state = {items: [], text: ''}; |
|
|
|
} |
|
|
|
}); |
|
|
|
var TodoApp = React.createClass({ |
|
|
|
getInitialState: function() { |
|
|
|
return {items: [], text: ''}; |
|
|
|
}, |
|
|
|
onChange: function(e) { |
|
|
|
this.setState({text: e.target.value}); |
|
|
|
}, |
|
|
|
handleSubmit: function(e) { |
|
|
|
e.preventDefault(); |
|
|
|
var nextItems = this.state.items.concat([{text: this.state.text, id: Date.now()}]); |
|
|
|
var nextText = ''; |
|
|
|
this.setState({items: nextItems, text: nextText}); |
|
|
|
}, |
|
|
|
render: function() { |
|
|
|
|
|
|
|
render() { |
|
|
|
return ( |
|
|
|
<div> |
|
|
|
<h3>TODO</h3> |
|
|
|
<TodoList items={this.state.items} /> |
|
|
|
<form onSubmit={this.handleSubmit}> |
|
|
|
<input onChange={this.onChange} value={this.state.text} /> |
|
|
|
<input onChange={this.handleChange} value={this.state.text} /> |
|
|
|
<button>{'Add #' + (this.state.items.length + 1)}</button> |
|
|
|
</form> |
|
|
|
</div> |
|
|
|
); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
handleChange(e) { |
|
|
|
this.setState({text: e.target.value}); |
|
|
|
} |
|
|
|
|
|
|
|
handleSubmit(e) { |
|
|
|
e.preventDefault(); |
|
|
|
var newItem = { |
|
|
|
text: this.state.text, |
|
|
|
id: Date.now() |
|
|
|
}; |
|
|
|
this.setState((prevState) => ({ |
|
|
|
items: prevState.items.concat(newItem), |
|
|
|
text: '' |
|
|
|
})); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
class TodoList extends React.Component { |
|
|
|
render() { |
|
|
|
return ( |
|
|
|
<ul> |
|
|
|
{this.props.items.map(item => ( |
|
|
|
<li key={item.id}>{item.text}</li> |
|
|
|
))} |
|
|
|
</ul> |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ReactDOM.render(<TodoApp />, mountNode); |
|
|
|
`;
|
|
|
|
`.trim();
|
|
|
|
|
|
|
|
ReactDOM.render( |
|
|
|
<ReactPlayground codeText={TODO_COMPONENT} />, |
|
|
|