SearchForm.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import React from 'react';
  2. // Header.SearchForm
  3. export default class SearchForm extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. keyword: '',
  8. searchedKeyword: '',
  9. };
  10. this.handleChange = this.handleChange.bind(this);
  11. this.ticker = null;
  12. }
  13. componentDidMount() {
  14. this.ticker = setInterval(this.searchFieldTicker.bind(this), this.props.pollInterval);
  15. }
  16. componentWillUnmount() {
  17. clearInterval(this.ticker);
  18. }
  19. search() {
  20. if (this.state.searchedKeyword != this.state.keyword) {
  21. this.props.onSearchFormChanged({keyword: this.state.keyword});
  22. this.setState({searchedKeyword: this.state.keyword});
  23. }
  24. }
  25. searchFieldTicker() {
  26. this.search();
  27. }
  28. handleChange(event) {
  29. const keyword = event.target.value;
  30. this.setState({keyword});
  31. }
  32. render() {
  33. return (
  34. <form
  35. action="/_search"
  36. className="search-form form-group input-group search-top-input-group"
  37. >
  38. <input
  39. autocomplete="off"
  40. type="text"
  41. className="search-top-input form-control"
  42. placeholder="Search ..."
  43. name="q"
  44. value={this.state.keyword}
  45. onChange={this.handleChange}
  46. />
  47. <span className="input-group-btn">
  48. <button type="submit" className="btn btn-default">
  49. <i className="search-top-icon fa fa-search"></i>
  50. </button>
  51. </span>
  52. </form>
  53. );
  54. }
  55. }
  56. SearchForm.propTypes = {
  57. onSearchFormChanged: React.PropTypes.func.isRequired,
  58. pollInterval: React.PropTypes.number,
  59. };
  60. SearchForm.defaultProps = {
  61. pollInterval: 1000,
  62. };