Created
December 5, 2014 15:05
-
-
Save hunterc/50008b0024d46b7c2889 to your computer and use it in GitHub Desktop.
react component to create filter list
This file contains 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 itemList = [ | |
'Apples', | |
'Pie', | |
'Brocooli', | |
'Chicken', | |
'Duck', | |
'Eggs', | |
'Fish', | |
'Granola', | |
'Hash Browns' | |
]; | |
var FilteredList = React.createClass({ | |
filterList: function(event) { | |
var updatedList = this.props.initialItems; | |
updatedList = updatedList.filter(function(item) { | |
return item.toLowerCase().indexOf( | |
event.target.value.toLowerCase() | |
) !== -1; | |
}); | |
this.setState({ | |
items: updatedList | |
}); | |
}, | |
componentWillMount: function() { | |
this.setState({ | |
items: this.props.initialItems | |
}); | |
}, | |
render: function() { | |
return ( | |
<div className="filter-list"> | |
<input type="text" placeholder="Search" onChange={this.filterList} /> | |
<List items={this.state.items} /> | |
</div> | |
); | |
} | |
}); | |
var List = React.createClass({ | |
render: function() { | |
return ( | |
<ul> | |
{ | |
this.props.items.map(function(item) { | |
return <li key={item}>{item}</li> | |
}) | |
} | |
</ul> | |
); | |
} | |
}); | |
React.renderComponent( | |
<FilteredList initialItems={itemList} />, | |
document.getElementById('mount-point') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment