Last active
July 7, 2018 20:12
-
-
Save jonathanharrell/96eac8bfc68e0c1b88479388144c5f4a to your computer and use it in GitHub Desktop.
React Autocomplete Component (without Render Props)
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
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