2015-12-28 23:34:32 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import pure from 'pure-render-decorator';
|
|
|
|
import { timestamp } from '../util';
|
|
|
|
|
|
|
|
@pure
|
|
|
|
export default class Search extends Component {
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (!prevProps.search.show && this.props.search.show) {
|
|
|
|
this.refs.input.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 18:29:22 +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 => (
|
|
|
|
<p key={result.id}>
|
|
|
|
{timestamp(new Date(result.time * 1000))} {result.from} {result.content}
|
|
|
|
</p>
|
|
|
|
));
|
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
return (
|
|
|
|
<div className="search" style={style}>
|
|
|
|
<input
|
|
|
|
ref="input"
|
|
|
|
className="search-input"
|
|
|
|
type="text"
|
2016-01-05 18:29:22 +00:00
|
|
|
onChange={this.handleSearch}
|
2015-12-28 23:34:32 +00:00
|
|
|
/>
|
|
|
|
<div className="search-results">{results}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|