Skip to content

Instantly share code, notes, and snippets.

@cassiozen
Created February 15, 2016 20:29
Show Gist options
  • Save cassiozen/2f9b9ef6974da57d7d18 to your computer and use it in GitHub Desktop.
Save cassiozen/2f9b9ef6974da57d7d18 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import {render} from 'react-dom';
class SearchBox extends Component {
render(){
return (
<input type="search"
value={this.props.value}
onChange={this.props.onSearch.bind(this)}/>
)
}
}
class ContactList extends Component {
render(){
return (
<ul>
{this.props.contact.map(contact => <li>{contact.name}</li>)}
</ul>
)
}
}
class App extends Component {
constructor(){
super();
this.state={
searchValue: ''
}
}
handleSearch(event){
this.setState({
searchValue: event.target.value
})
}
render(){
let filteredContacts = this.props.contacts.filter(contact => {
return contact.name.indexOf(this.state.searchValue) !== -1
})
return (
<div>
<SearchBox value={this.state.searchValue}
onSearch={this.handleSearch.bind(this)} />
<ContactList contact={filteredContacts} />
</div>
);
}
}
let contacts = [
{ name: "Cassio Zen", email: "cassiozen@gmail.com" },
{ name: "Dan Abramov", email: "gaearon@somewhere.com" },
{ name: "Pete Hunt", email: "floydophone@somewhere.com" },
{ name: "Paul O’Shannessy", email: "zpao@somewhere.com" },
{ name: "Ryan Florence", email: "rpflorence@somewhere.com" },
{ name: "Sebastian Markbage", email: "sebmarkbage@here.com" },
]
render(<App contacts={contacts} />, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment