SearchForm.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import React from 'react';
  2. // Search.SearchForm
  3. export default class SearchForm extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. keyword: this.props.keyword,
  8. searchedKeyword: this.props.keyword,
  9. };
  10. this.handleSubmit = this.handleSubmit.bind(this);
  11. this.handleChange = this.handleChange.bind(this);
  12. }
  13. componentDidMount() {
  14. }
  15. componentWillUnmount() {
  16. }
  17. search() {
  18. if (this.state.searchedKeyword != this.state.keyword) {
  19. this.props.onSearchFormChanged({keyword: this.state.keyword});
  20. this.setState({searchedKeyword: this.state.keyword});
  21. }
  22. }
  23. handleSubmit(event) {
  24. event.preventDefault();
  25. this.search({keyword: this.state.keyword});
  26. }
  27. handleChange(event) {
  28. const keyword = event.target.value;
  29. this.setState({keyword});
  30. }
  31. render() {
  32. return (
  33. <form className="form form-group input-group" onSubmit={this.handleSubmit}>
  34. <input
  35. type="text"
  36. name="q"
  37. value={this.state.keyword}
  38. onChange={this.handleChange}
  39. className="form-control"
  40. />
  41. <span className="input-group-btn">
  42. <button type="submit" className="btn btn-default">
  43. <i className="search-top-icon fa fa-search"></i>
  44. </button>
  45. </span>
  46. </form>
  47. );
  48. }
  49. }
  50. SearchForm.propTypes = {
  51. onSearchFormChanged: React.PropTypes.func.isRequired,
  52. };
  53. SearchForm.defaultProps = {
  54. };