SearchPageForm.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import FormGroup from 'react-bootstrap/es/FormGroup';
  4. import Button from 'react-bootstrap/es/Button';
  5. import InputGroup from 'react-bootstrap/es/InputGroup';
  6. import { createSubscribedElement } from '../UnstatedUtils';
  7. import AppContainer from '../../services/AppContainer';
  8. import SearchForm from '../SearchForm';
  9. // Search.SearchForm
  10. class SearchPageForm extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. keyword: this.props.keyword,
  15. searchedKeyword: this.props.keyword,
  16. };
  17. this.search = this.search.bind(this);
  18. this.onInputChange = this.onInputChange.bind(this);
  19. }
  20. search() {
  21. const keyword = this.state.keyword;
  22. this.props.onSearchFormChanged({ keyword });
  23. this.setState({ searchedKeyword: keyword });
  24. }
  25. onInputChange(input) { // for only submitting with button
  26. this.setState({ keyword: input });
  27. }
  28. render() {
  29. return (
  30. <FormGroup>
  31. <InputGroup>
  32. <SearchForm
  33. t={this.props.t}
  34. onSubmit={this.search}
  35. keyword={this.state.searchedKeyword}
  36. onInputChange={this.onInputChange}
  37. />
  38. <InputGroup.Button className="">
  39. <Button onClick={this.search}>
  40. <i className="icon-magnifier"></i>
  41. </Button>
  42. </InputGroup.Button>
  43. </InputGroup>
  44. </FormGroup>
  45. );
  46. }
  47. }
  48. /**
  49. * Wrapper component for using unstated
  50. */
  51. const SearchPageFormWrapper = (props) => {
  52. return createSubscribedElement(SearchPageForm, props, [AppContainer]);
  53. };
  54. SearchPageForm.propTypes = {
  55. t: PropTypes.func.isRequired, // i18next
  56. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  57. keyword: PropTypes.string,
  58. onSearchFormChanged: PropTypes.func.isRequired,
  59. };
  60. SearchPageForm.defaultProps = {
  61. };
  62. export default SearchPageFormWrapper;