SearchForm.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. }
  32. handleChange(event) {
  33. const keyword = event.target.value;
  34. this.setState({keyword});
  35. }
  36. render() {
  37. return (
  38. <form
  39. action="/_search"
  40. className="search-form form-group input-group search-top-input-group"
  41. onSubmit={this.handleSubmit}
  42. >
  43. <input
  44. autocomplete="off"
  45. type="text"
  46. className="search-top-input form-control"
  47. placeholder="Search ..."
  48. name="q"
  49. value={this.state.keyword}
  50. onChange={this.handleChange}
  51. />
  52. <span className="input-group-btn">
  53. <button type="submit" className="btn btn-default">
  54. <i className="search-top-icon fa fa-search"></i>
  55. </button>
  56. </span>
  57. </form>
  58. );
  59. }
  60. }
  61. SearchForm.propTypes = {
  62. onSearchFormChanged: React.PropTypes.func.isRequired,
  63. pollInterval: React.PropTypes.number,
  64. };
  65. SearchForm.defaultProps = {
  66. pollInterval: 1000,
  67. };