SearchForm.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. type="text"
  40. className="search-top-input form-control"
  41. placeholder="Search ..."
  42. name="q"
  43. value={this.state.keyword}
  44. onChange={this.handleChange}
  45. />
  46. <span className="input-group-btn">
  47. <button type="submit" className="btn btn-default">
  48. <i className="search-top-icon fa fa-search"></i>
  49. </button>
  50. </span>
  51. </form>
  52. );
  53. }
  54. }
  55. SearchForm.propTypes = {
  56. onSearchFormChanged: React.PropTypes.func.isRequired,
  57. pollInterval: React.PropTypes.number,
  58. };
  59. SearchForm.defaultProps = {
  60. pollInterval: 1000,
  61. };