Based on hexlet.io react.js course's final task https://ru.hexlet.io/lessons/one_way_data_flow/theory_unit
Last active
August 29, 2015 14:24
-
-
Save ir4y/e86cc57592bab9de91f6 to your computer and use it in GitHub Desktop.
React.js based simple TODO list with lenses.
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
| <div id="example"> | |
| </div> |
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
| var itemsLens = Tscope.attr('items'); | |
| var valueLens = Tscope.attr('value'); | |
| var Item = React.createClass({ | |
| handleOnRemove: function() { | |
| this.props.onRemove(this.props.item.id); | |
| }, | |
| render: function() { | |
| return <div> | |
| {this.props.item.value} | |
| <input type="button" className="remove" value="remove" onClick={this.handleOnRemove} /> | |
| </div> | |
| } | |
| }) | |
| var Box = React.createClass({ | |
| getInitialState: function() { | |
| return {value: "", items: []}; | |
| }, | |
| handleOnChange: function(e) { | |
| e.preventDefault(); | |
| this.setState({value: e.target.value}) | |
| }, | |
| handleOnRemove: function(id) { | |
| var items = itemsLens.traversal(function(item) { | |
| return item.id !== id | |
| }).get(this.state); | |
| this.setState(itemsLens.set(this.state, items)); | |
| }, | |
| handleOnClick: function(e) { | |
| e.preventDefault(); | |
| var items = itemsLens(this.state); | |
| var item = {id: Math.random(), value: this.state.value} | |
| items.push(item); | |
| this.setState(itemsLens(valueLens(this.state, ""), items)); | |
| }, | |
| render: function() { | |
| var value = this.state.value; | |
| var items = this.state.items; | |
| var self = this; | |
| return ( | |
| <div> | |
| <form action=""> | |
| <input className="text" type="text" value={value} onChange={this.handleOnChange} /> | |
| <input className="submit" type="submit" onClick={this.handleOnClick} /> | |
| </form> | |
| <div className="items"> | |
| {items.map(function(item){ return <Item item={item} onRemove={self.handleOnRemove} /> })} | |
| </div> | |
| </div> | |
| ); | |
| } | |
| }) | |
| React.render( | |
| <Box />, | |
| document.getElementById("example") | |
| ); |
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
| <script src="//cdnjs.cloudflare.com/ajax/libs/react/0.13.0/react.min.js"></script> | |
| <script src="http://bro.agency/js/tscope.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment