Skip to content

Instantly share code, notes, and snippets.

@grantglidewell
Last active August 1, 2018 21:29
Show Gist options
  • Save grantglidewell/d7de2d8cef48bf918bee7cfc190d49de to your computer and use it in GitHub Desktop.
Save grantglidewell/d7de2d8cef48bf918bee7cfc190d49de to your computer and use it in GitHub Desktop.
export class Search extends Component {
state = {
searchTerm: ''
}
componentDidMount() {
// lets the user override the initial value in the search box
if (this.props.override) {
this.setState({ searchTerm: this.props.override })
}
}
render() {
return (
<form
className={cx(styles.search, this.props.className)}
onSubmit={this.handleSubmit}>
<Button className={styles.searchBtn} type="submit">
<i
className={cx(styles.searchIcon, 'fa fa-search')}
aria-hidden="true"
/>
</Button>
<input
type="text"
name="term"
autoComplete="off"
value={this.state.searchTerm}
className={styles.searchField}
placeholder={this.props.placeholder}
onFocus={this.props.onFocus}
onChange={this.handleKeyUp}
/>
</form>
)
}
handleSubmit = evt => {
evt.preventDefault()
this.props.onSubmit(this.state.searchTerm)
}
handleKeyUp = evt => {
evt.preventDefault()
// return the target value of the input
this.setState({ searchTerm: evt.target.value }, () =>
this.props.onKeyUp(this.state.searchTerm)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment