SearchForm.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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" 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. </form>
  42. );
  43. }
  44. }
  45. SearchForm.propTypes = {
  46. onSearchFormChanged: React.PropTypes.func.isRequired,
  47. };
  48. SearchForm.defaultProps = {
  49. };