SearchForm.js 1.6 KB

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