2015-12-28 23:34:32 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import pure from 'pure-render-decorator';
|
2016-01-22 23:41:35 +00:00
|
|
|
import SearchResult from './SearchResult';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
|
|
|
@pure
|
|
|
|
export default class Search extends Component {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 02:55:50 +00:00
|
|
|
ref={el => { this.input = el; }}
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|