Created
June 24, 2014 15:02
-
-
Save proudlygeek/314bfbbe55e48df72928 to your computer and use it in GitHub Desktop.
React JS Todo APP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** @jsx React.DOM */ | |
| var InputForm = React.createClass({ | |
| handleSubmit: function(e) { | |
| e.preventDefault(); | |
| var value = this.refs.text.getDOMNode().value; | |
| this.props.onAddItem({ text: value, done: false }); | |
| this.refs.text.getDOMNode().value = ''; | |
| }, | |
| render: function() { | |
| return ( | |
| <div className="inputForm"> | |
| <form className="inputForm" onSubmit={this.handleSubmit}> | |
| <input type="text" placeholder="What?" ref="text" /> | |
| <input type="submit" name="Add" /> | |
| </form> | |
| </div> | |
| ); | |
| } | |
| }); | |
| var TodoList = React.createClass({ | |
| updateTodoStatus: function(todo) { | |
| todo.done = !todo.done; | |
| this.props.onCheckItem(todo.id); | |
| return false; | |
| }, | |
| render: function() { | |
| var todos = this.props.data.map(function(todo) { | |
| return ( | |
| <li key={todo.id} className={(todo.done) ? 'done' : ''}> | |
| <input onChange={this.updateTodoStatus.bind(this, todo)} type="checkbox" checked={todo.done} /> | |
| {todo.text} | |
| </li> | |
| ); | |
| }.bind(this)); | |
| return ( | |
| <ul className="todos"> | |
| {todos} | |
| </ul> | |
| ); | |
| } | |
| }); | |
| var TodoApp = React.createClass({ | |
| getInitialState: function() { | |
| var data = JSON.parse(localStorage.getItem('todo-app')) || []; | |
| return {data: data}; | |
| }, | |
| saveTodoList: function(value) { | |
| localStorage.setItem('todo-app', JSON.stringify(value)); | |
| }, | |
| handleAddItem: function(item) { | |
| item.id = this.state.data.length + 1; | |
| var todos = this.state.data; | |
| var newTodos = todos.concat([item]); | |
| this.setState({data: newTodos}); | |
| this.saveTodoList(newTodos); | |
| }, | |
| handleUpdateItem: function(id) { | |
| for (var i = 0; i < this.state.data; i++) { | |
| if (this.state.data[i].id === id) { | |
| this.state.data[i].done = !this.state.data[i].done; | |
| break; | |
| } | |
| } | |
| this.forceUpdate(); | |
| this.saveTodoList(this.state.data); | |
| }, | |
| handleClear: function() { | |
| this.setState({data: []}); | |
| this.saveTodoList([]); | |
| }, | |
| render: function() { | |
| return( | |
| <div className="todoApp" > | |
| <h1>Todo List</h1> | |
| <TodoList onCheckItem={this.handleUpdateItem} data={this.state.data} /> | |
| <InputForm onAddItem={this.handleAddItem} /> | |
| <input type="button" value="Clear List" onClick={this.handleClear} /> | |
| </div> | |
| ); | |
| } | |
| }); | |
| React.renderComponent(<TodoApp />, document.getElementById('app-container')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Todo App</title> | |
| <style> | |
| .done { | |
| text-decoration: line-through; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="app-container"></div> | |
| <script type="text/javascript" src="/bower_components/react/JSXTransformer.js"></script> | |
| <script type="text/javascript" src="/bower_components/react/react.js"></script> | |
| <script type="text/jsx" src="/src/app.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment