SearchForm.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. searchFieldTicker() {
  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. handleSubmit(event) {
  22. event.preventDefault();
  23. }
  24. handleChange(event) {
  25. const keyword = event.target.value;
  26. this.setState({keyword});
  27. }
  28. render() {
  29. return (
  30. <form
  31. className="search-form form-group input-group search-top-input-group"
  32. onSubmit={this.handleSubmit}
  33. >
  34. <input type="text" className="search-top-input form-control" placeholder="Search ..."
  35. value={this.state.keyword}
  36. onChange={this.handleChange}
  37. />
  38. <span className="input-group-btn">
  39. <button type="submit" className="btn btn-default" type="button">
  40. <i className="search-top-icon fa fa-search"></i>
  41. </button>
  42. </span>
  43. </form>
  44. );
  45. }
  46. }
  47. SearchForm.propTypes = {
  48. onSearchFormChanged: React.PropTypes.func.isRequired,
  49. pollInterval: React.PropTypes.number,
  50. };
  51. SearchForm.defaultProps = {
  52. pollInterval: 1000,
  53. };