SearchPageForm.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 align-items-center">
  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 className="btn-group-submit-search">
  43. <span
  44. role="button"
  45. className="text-decoration-none"
  46. onClick={() => {
  47. try {
  48. this.search();
  49. }
  50. catch (error) {
  51. logger.error(error);
  52. }
  53. }}
  54. >
  55. <i className="icon-magnifier"></i>
  56. </span>
  57. </div>
  58. </div>
  59. </div>
  60. );
  61. }
  62. }
  63. /**
  64. * Wrapper component for using unstated
  65. */
  66. const SearchPageFormWrapper = withUnstatedContainers(SearchPageForm, [AppContainer]);
  67. SearchPageForm.propTypes = {
  68. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  69. keyword: PropTypes.string,
  70. onSearchFormChanged: PropTypes.func,
  71. };
  72. SearchPageForm.defaultProps = {
  73. };
  74. export default SearchPageFormWrapper;