2018-11-04 06:22:46 +00:00
|
|
|
import React, { memo, useRef, useEffect } from 'react';
|
2016-01-22 23:41:35 +00:00
|
|
|
import SearchResult from './SearchResult';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
const Search = ({ search, onSearch }) => {
|
|
|
|
const inputEl = useRef();
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
useEffect(
|
|
|
|
() => {
|
|
|
|
if (search.show) {
|
|
|
|
inputEl.current.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[search.show]
|
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
const style = {
|
|
|
|
display: search.show ? 'block' : 'none'
|
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
let i = 0;
|
|
|
|
const results = search.results.map(result => (
|
|
|
|
<SearchResult key={i++} result={result} />
|
|
|
|
));
|
2016-01-05 18:29:22 +00:00
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
return (
|
|
|
|
<div className="search" style={style}>
|
|
|
|
<div className="search-input-wrap">
|
|
|
|
<i className="icon-search" />
|
|
|
|
<input
|
|
|
|
ref={inputEl}
|
|
|
|
className="search-input"
|
|
|
|
type="text"
|
|
|
|
onChange={e => onSearch(e.target.value)}
|
|
|
|
/>
|
2015-12-28 23:34:32 +00:00
|
|
|
</div>
|
2018-11-04 06:22:46 +00:00
|
|
|
<div className="search-results">{results}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default memo(Search);
|