SearchForm.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React from 'react';
  2. export default class SearchForm extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. keyword: '',
  7. searchedKeyword: '',
  8. };
  9. this.handleSubmit= this.handleSubmit.bind(this);
  10. this.handleChange= this.handleChange.bind(this);
  11. }
  12. componentDidMount() {
  13. setInterval(this.searchFieldTicker.bind(this), this.props.pollInterval);
  14. }
  15. search() {
  16. if (this.state.searchedKeyword != this.state.keyword) {
  17. this.props.onSearchFormChanged({keyword: this.state.keyword});
  18. this.setState({searchedKeyword: this.state.keyword});
  19. }
  20. }
  21. searchFieldTicker() {
  22. this.search();
  23. }
  24. handleSubmit(event) {
  25. event.preventDefault();
  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. };