SearchForm.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withUnstatedContainers } from './UnstatedUtils';
  4. import AppContainer from '../services/AppContainer';
  5. import SearchTypeahead from './SearchTypeahead';
  6. // SearchTypeahead wrapper
  7. class SearchForm extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. searchError: null,
  12. };
  13. this.onSearchError = this.onSearchError.bind(this);
  14. this.onChange = this.onChange.bind(this);
  15. }
  16. componentDidMount() {
  17. }
  18. componentWillUnmount() {
  19. }
  20. onSearchError(err) {
  21. this.setState({
  22. searchError: err,
  23. });
  24. }
  25. onChange(selected) {
  26. const page = selected[0]; // should be single page selected
  27. // navigate to page
  28. if (page != null) {
  29. window.location = page.path;
  30. }
  31. }
  32. getHelpElement() {
  33. const { t, appContainer } = this.props;
  34. const config = appContainer.getConfig();
  35. const isReachable = config.isSearchServiceReachable;
  36. if (!isReachable) {
  37. return (
  38. <>
  39. <h5 className="text-danger">Error occured on Search Service</h5>
  40. Try to reconnect from management page.
  41. </>
  42. );
  43. }
  44. return (
  45. <table className="table grw-search-table search-help m-0">
  46. <caption className="text-left text-primary p-2">
  47. <h5 className="h6"><i className="icon-magnifier pr-2 mb-2" />{ t('search_help.title') }</h5>
  48. </caption>
  49. <tbody>
  50. <tr>
  51. <th className="py-2">
  52. <code>word1</code> <code>word2</code><br></br>
  53. <small>({ t('search_help.and.syntax help') })</small>
  54. </th>
  55. <td><h6 className="m-0">{ t('search_help.and.desc', { word1: 'word1', word2: 'word2' }) }</h6></td>
  56. </tr>
  57. <tr>
  58. <th className="py-2">
  59. <code>&quot;This is GROWI&quot;</code><br></br>
  60. <small>({ t('search_help.phrase.syntax help') })</small>
  61. </th>
  62. <td><h6 className="m-0">{ t('search_help.phrase.desc', { phrase: 'This is GROWI' }) }</h6></td>
  63. </tr>
  64. <tr>
  65. <th className="py-2"><code>-keyword</code></th>
  66. <td><h6 className="m-0">{ t('search_help.exclude.desc', { word: 'keyword' }) }</h6></td>
  67. </tr>
  68. <tr>
  69. <th className="py-2"><code>prefix:/user/</code></th>
  70. <td><h6 className="m-0">{ t('search_help.prefix.desc', { path: '/user/' }) }</h6></td>
  71. </tr>
  72. <tr>
  73. <th className="py-2"><code>-prefix:/user/</code></th>
  74. <td><h6 className="m-0">{ t('search_help.exclude_prefix.desc', { path: '/user/' }) }</h6></td>
  75. </tr>
  76. <tr>
  77. <th className="py-2"><code>tag:wiki</code></th>
  78. <td><h6 className="m-0">{ t('search_help.tag.desc', { tag: 'wiki' }) }</h6></td>
  79. </tr>
  80. <tr>
  81. <th className="py-2"><code>-tag:wiki</code></th>
  82. <td><h6 className="m-0">{ t('search_help.exclude_tag.desc', { tag: 'wiki' }) }</h6></td>
  83. </tr>
  84. </tbody>
  85. </table>
  86. );
  87. }
  88. render() {
  89. const { t, appContainer, dropup } = this.props;
  90. const config = appContainer.getConfig();
  91. const isReachable = config.isSearchServiceReachable;
  92. const placeholder = isReachable
  93. ? 'Search ...'
  94. : 'Error on Search Service';
  95. const emptyLabel = (this.state.searchError !== null)
  96. ? 'Error on searching.'
  97. : t('search.search page bodies');
  98. return (
  99. <SearchTypeahead
  100. dropup={dropup}
  101. onChange={this.onChange}
  102. onSubmit={this.props.onSubmit}
  103. onInputChange={this.props.onInputChange}
  104. onSearchError={this.onSearchError}
  105. emptyLabel={emptyLabel}
  106. placeholder={placeholder}
  107. helpElement={this.getHelpElement()}
  108. keywordOnInit={this.props.keyword}
  109. />
  110. );
  111. }
  112. }
  113. /**
  114. * Wrapper component for using unstated
  115. */
  116. const SearchFormWrapper = withUnstatedContainers(SearchForm, [AppContainer]);
  117. SearchForm.propTypes = {
  118. t: PropTypes.func.isRequired, // i18next
  119. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  120. dropup: PropTypes.bool,
  121. keyword: PropTypes.string,
  122. onSubmit: PropTypes.func.isRequired,
  123. onInputChange: PropTypes.func,
  124. };
  125. SearchForm.defaultProps = {
  126. onInputChange: () => {},
  127. };
  128. export default SearchFormWrapper;