Skip to content

Instantly share code, notes, and snippets.

@hunterc
Created December 5, 2014 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hunterc/50008b0024d46b7c2889 to your computer and use it in GitHub Desktop.
Save hunterc/50008b0024d46b7c2889 to your computer and use it in GitHub Desktop.
react component to create filter list
/** @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