dispatch/client/src/js/components/Search.js

41 lines
1010 B
JavaScript
Raw Normal View History

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) {
this.refs.input.focus();
}
}
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
ref="input"
className="search-input"
type="text"
onChange={this.handleSearch}
/>
</div>
2015-12-28 23:34:32 +00:00
<div className="search-results">{results}</div>
</div>
);
}
}