SearchPageForm.jsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withUnstatedContainers } from '../UnstatedUtils';
  4. import AppContainer from '~/client/services/AppContainer';
  5. import SearchForm from '../SearchForm';
  6. import loggerFactory from '~/utils/logger';
  7. const logger = loggerFactory('growi:searchPageForm');
  8. // Search.SearchForm
  9. class SearchPageForm extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. keyword: this.props.keyword,
  14. searchedKeyword: this.props.keyword,
  15. };
  16. this.search = this.search.bind(this);
  17. this.onInputChange = this.onInputChange.bind(this);
  18. }
  19. search() {
  20. if (this.props.onSearchFormChanged != null) {
  21. const keyword = this.state.keyword;
  22. this.props.onSearchFormChanged({ keyword });
  23. this.setState({ searchedKeyword: keyword });
  24. }
  25. else {
  26. throw new Error('onSearchFormChanged method is null');
  27. }
  28. }
  29. onInputChange(input) { // for only submitting with button
  30. this.setState({ keyword: input });
  31. }
  32. render() {
  33. return (
  34. // TODO: modify design after other component is created
  35. <div className="grw-search-form-in-search-result-page d-flex">
  36. <div className="input-group flex-nowrap">
  37. <SearchForm
  38. onSubmit={this.search}
  39. keyword={this.state.searchedKeyword}
  40. onInputChange={this.onInputChange}
  41. />
  42. </div>
  43. <div className="input-group-append">
  44. <button
  45. className="btn btn-secondary"
  46. type="button"
  47. id="button-addon2"
  48. onClick={() => {
  49. try {
  50. this.search();
  51. }
  52. catch (error) {
  53. logger.error(error);
  54. }
  55. }}
  56. >
  57. <i className="icon-magnifier"></i>
  58. </button>
  59. </div>
  60. </div>
  61. );
  62. }
  63. }
  64. /**
  65. * Wrapper component for using unstated
  66. */
  67. const SearchPageFormWrapper = withUnstatedContainers(SearchPageForm, [AppContainer]);
  68. SearchPageForm.propTypes = {
  69. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  70. keyword: PropTypes.string,
  71. onSearchFormChanged: PropTypes.func,
  72. };
  73. SearchPageForm.defaultProps = {
  74. };
  75. export default SearchPageFormWrapper;