SearchForm.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.handleSubmit = this.handleSubmit.bind(this);
  12. this.ticker = null;
  13. }
  14. componentDidMount() {
  15. this.ticker = setInterval(this.searchFieldTicker.bind(this), this.props.pollInterval);
  16. }
  17. componentWillUnmount() {
  18. clearInterval(this.ticker);
  19. }
  20. search() {
  21. if (this.state.searchedKeyword != this.state.keyword) {
  22. this.props.onSearchFormChanged({keyword: this.state.keyword});
  23. this.setState({searchedKeyword: this.state.keyword});
  24. }
  25. }
  26. searchFieldTicker() {
  27. this.search();
  28. }
  29. handleSubmit(event) {
  30. event.preventDefault();
  31. this.search();
  32. }
  33. handleChange(event) {
  34. const keyword = event.target.value;
  35. this.setState({keyword});
  36. }
  37. render() {
  38. return (
  39. <form
  40. action="/_search"
  41. className="search-form form-group input-group search-top-input-group"
  42. onSubmit={this.handleSubmit}
  43. >
  44. <input
  45. autocomplete="off"
  46. type="text"
  47. className="search-top-input form-control"
  48. placeholder="Search ..."
  49. name="q"
  50. value={this.state.keyword}
  51. onChange={this.handleChange}
  52. />
  53. <span className="input-group-btn">
  54. <button type="submit" className="btn btn-default">
  55. <i className="search-top-icon fa fa-search"></i>
  56. </button>
  57. </span>
  58. </form>
  59. );
  60. }
  61. }
  62. SearchForm.propTypes = {
  63. onSearchFormChanged: React.PropTypes.func.isRequired,
  64. pollInterval: React.PropTypes.number,
  65. };
  66. SearchForm.defaultProps = {
  67. pollInterval: 1000,
  68. };