Skip to content

Instantly share code, notes, and snippets.

@ir4y
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save ir4y/e86cc57592bab9de91f6 to your computer and use it in GitHub Desktop.

Select an option

Save ir4y/e86cc57592bab9de91f6 to your computer and use it in GitHub Desktop.
React.js based simple TODO list with lenses.
<div id="example">
</div>
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")
);
<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