SearchForm.js 1.5 KB

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