import React from 'react'; // Header.SearchForm export default class SearchForm extends React.Component { constructor(props) { super(props); this.state = { keyword: '', searchedKeyword: '', }; this.handleChange = this.handleChange.bind(this); this.handleFocus = this.handleFocus.bind(this); this.handleBlur = this.handleBlur.bind(this); this.clearForm = this.clearForm.bind(this); this.ticker = null; } componentDidMount() { this.ticker = setInterval(this.searchFieldTicker.bind(this), this.props.pollInterval); } componentWillUnmount() { clearInterval(this.ticker); } search() { if (this.state.searchedKeyword != this.state.keyword) { this.props.onSearchFormChanged({keyword: this.state.keyword}); this.setState({searchedKeyword: this.state.keyword}); } } getFormClearComponent() { if (this.state.keyword !== '') { return ; } else { return ''; } } clearForm() { this.setState({keyword: ''}); this.search(); } searchFieldTicker() { this.search(); } handleFocus(event) { this.props.isShown(true); } handleBlur(event) { //this.props.isShown(false); } handleChange(event) { const keyword = event.target.value; this.setState({keyword}); } render() { const formClear = this.getFormClearComponent(); return (
{formClear}
); } } SearchForm.propTypes = { onSearchFormChanged: React.PropTypes.func.isRequired, isShown: React.PropTypes.func.isRequired, pollInterval: React.PropTypes.number, }; SearchForm.defaultProps = { pollInterval: 1000, };