2017-02-17 00:46:39 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2016-01-22 23:41:35 +00:00
|
|
|
import SearchResult from './SearchResult';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-02-17 00:46:39 +00:00
|
|
|
export default class Search extends PureComponent {
|
2015-12-28 23:34:32 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (!prevProps.search.show && this.props.search.show) {
|
2017-02-16 02:55:50 +00:00
|
|
|
this.input.focus();
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
inputRef = el => {
|
|
|
|
this.input = el;
|
|
|
|
};
|
2017-02-16 21:56:32 +00:00
|
|
|
|
2016-01-06 23:28:11 +00:00
|
|
|
handleSearch = e => this.props.onSearch(e.target.value);
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2016-01-05 18:29:22 +00:00
|
|
|
render() {
|
|
|
|
const { search } = this.props;
|
2015-12-28 23:34:32 +00:00
|
|
|
const style = {
|
|
|
|
display: search.show ? 'block' : 'none'
|
|
|
|
};
|
|
|
|
|
2016-01-05 18:29:22 +00:00
|
|
|
const results = search.results.map(result => (
|
2016-01-22 23:41:35 +00:00
|
|
|
<SearchResult key={result.id} result={result} />
|
2016-01-05 18:29:22 +00:00
|
|
|
));
|
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
return (
|
|
|
|
<div className="search" style={style}>
|
2016-01-18 02:21:58 +00:00
|
|
|
<div className="search-input-wrap">
|
|
|
|
<i className="icon-search" />
|
|
|
|
<input
|
2017-02-16 21:56:32 +00:00
|
|
|
ref={this.inputRef}
|
2016-01-18 02:21:58 +00:00
|
|
|
className="search-input"
|
|
|
|
type="text"
|
|
|
|
onChange={this.handleSearch}
|
|
|
|
/>
|
|
|
|
</div>
|
2015-12-28 23:34:32 +00:00
|
|
|
<div className="search-results">{results}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|