Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.