SearchForm.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. search() {
  14. if (this.state.searchedKeyword != this.state.keyword) {
  15. this.props.onSearchFormChanged({keyword: this.state.keyword});
  16. this.setState({searchedKeyword: this.state.keyword});
  17. }
  18. }
  19. handleSubmit(event) {
  20. event.preventDefault();
  21. this.search({keyword: this.state.keyword});
  22. }
  23. handleChange(event) {
  24. const keyword = event.target.value;
  25. this.setState({keyword});
  26. }
  27. render() {
  28. return (
  29. <form className="form form-group input-group" onSubmit={this.handleSubmit}>
  30. <input
  31. type="text"
  32. name="q"
  33. value={this.state.keyword}
  34. onChange={this.handleChange}
  35. className="form-control"
  36. />
  37. <span className="input-group-btn">
  38. <button type="submit" className="btn btn-default">
  39. <i className="search-top-icon fa fa-search"></i>
  40. </button>
  41. </span>
  42. </form>
  43. );
  44. }
  45. }
  46. SearchForm.propTypes = {
  47. onSearchFormChanged: React.PropTypes.func.isRequired,
  48. };
  49. SearchForm.defaultProps = {
  50. };