SearchForm.js 2.0 KB

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