Skip to content

Instantly share code, notes, and snippets.

@jonathanharrell
Last active July 7, 2018 20:12
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 jonathanharrell/96eac8bfc68e0c1b88479388144c5f4a to your computer and use it in GitHub Desktop.
Save jonathanharrell/96eac8bfc68e0c1b88479388144c5f4a to your computer and use it in GitHub Desktop.
React Autocomplete Component (without Render Props)
class Autocomplete extends React.Component {
constructor(props) {
super(props)
this.state = {
results: props.options
}
}
searchList(event) {
const results = this.props.options
.filter(option => option.toLowerCase().includes(event.target.value.toLowerCase()))
this.setState({ results })
}
render () {
return (
<div className="autocomplete">
<input
type="text"
placeholder="Type to search list"
onChange={searchList}
/>
<div className="autocomplete-dropdown">
<ul className="autocomplete-search-results">
{this.state.results.map(option => (
<li class="autocomplete-search-result">{option}</li>
))}
</ul>
</div>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment