SearchForm.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import React, {
  2. FC, forwardRef, ForwardRefRenderFunction, useImperativeHandle,
  3. useRef, useState,
  4. } from 'react';
  5. import { useTranslation } from 'next-i18next';
  6. import { IFocusable } from '~/client/interfaces/focusable';
  7. import { TypeaheadProps } from '~/client/interfaces/react-bootstrap-typeahead';
  8. import { IPageWithSearchMeta } from '~/interfaces/search';
  9. import SearchTypeahead from './SearchTypeahead';
  10. type SearchFormHelpProps = {
  11. isReachable: boolean,
  12. }
  13. const SearchFormHelp: FC<SearchFormHelpProps> = React.memo((props: SearchFormHelpProps) => {
  14. const { t } = useTranslation();
  15. const { isReachable } = props;
  16. if (!isReachable) {
  17. return (
  18. <>
  19. <h5 className="text-danger">Error occured on Search Service</h5>
  20. Try to reconnect from management page.
  21. </>
  22. );
  23. }
  24. return (
  25. <table className="table grw-search-table search-help m-0">
  26. <caption className="text-left text-primary p-2">
  27. <h5 className="h6"><i className="icon-magnifier pr-2 mb-2" />{ t('search_help.title') }</h5>
  28. </caption>
  29. <tbody>
  30. <tr>
  31. <th className="py-2">
  32. <code>word1</code> <code>word2</code><br></br>
  33. <small>({ t('search_help.and.syntax help') })</small>
  34. </th>
  35. <td><h6 className="m-0">{ t('search_help.and.desc', { word1: 'word1', word2: 'word2' }) }</h6></td>
  36. </tr>
  37. <tr>
  38. <th className="py-2">
  39. <code>&quot;This is GROWI&quot;</code><br></br>
  40. <small>({ t('search_help.phrase.syntax help') })</small>
  41. </th>
  42. <td><h6 className="m-0">{ t('search_help.phrase.desc', { phrase: 'This is GROWI' }) }</h6></td>
  43. </tr>
  44. <tr>
  45. <th className="py-2"><code>-keyword</code></th>
  46. <td><h6 className="m-0">{ t('search_help.exclude.desc', { word: 'keyword' }) }</h6></td>
  47. </tr>
  48. <tr>
  49. <th className="py-2"><code>prefix:/user/</code></th>
  50. <td><h6 className="m-0">{ t('search_help.prefix.desc', { path: '/user/' }) }</h6></td>
  51. </tr>
  52. <tr>
  53. <th className="py-2"><code>-prefix:/user/</code></th>
  54. <td><h6 className="m-0">{ t('search_help.exclude_prefix.desc', { path: '/user/' }) }</h6></td>
  55. </tr>
  56. <tr>
  57. <th className="py-2"><code>tag:wiki</code></th>
  58. <td><h6 className="m-0">{ t('search_help.tag.desc', { tag: 'wiki' }) }</h6></td>
  59. </tr>
  60. <tr>
  61. <th className="py-2"><code>-tag:wiki</code></th>
  62. <td><h6 className="m-0">{ t('search_help.exclude_tag.desc', { tag: 'wiki' }) }</h6></td>
  63. </tr>
  64. </tbody>
  65. </table>
  66. );
  67. });
  68. SearchFormHelp.displayName = 'SearchFormHelp';
  69. type Props = TypeaheadProps & {
  70. isSearchServiceReachable: boolean,
  71. keywordOnInit?: string,
  72. disableIncrementalSearch?: boolean,
  73. onChange?: (data: IPageWithSearchMeta[]) => void,
  74. onSubmit?: (input: string) => void,
  75. };
  76. const SearchForm: ForwardRefRenderFunction<IFocusable, Props> = (props: Props, ref) => {
  77. const { t } = useTranslation();
  78. const {
  79. isSearchServiceReachable,
  80. keywordOnInit,
  81. disableIncrementalSearch,
  82. dropup, onChange, onBlur, onFocus, onSubmit, onInputChange,
  83. } = props;
  84. const [searchError, setSearchError] = useState<Error | null>(null);
  85. const searchTyheaheadRef = useRef<IFocusable>(null);
  86. // publish focus()
  87. useImperativeHandle(ref, () => ({
  88. focus() {
  89. const instance = searchTyheaheadRef?.current;
  90. if (instance != null) {
  91. instance.focus();
  92. }
  93. },
  94. }));
  95. const placeholder = isSearchServiceReachable
  96. ? 'Search ...'
  97. : 'Error on Search Service';
  98. const emptyLabel = (searchError != null)
  99. ? 'Error on searching.'
  100. : t('search.search page bodies');
  101. return (
  102. <SearchTypeahead
  103. ref={searchTyheaheadRef}
  104. dropup={dropup}
  105. emptyLabel={emptyLabel}
  106. placeholder={placeholder}
  107. onChange={onChange}
  108. onSubmit={onSubmit}
  109. onInputChange={onInputChange}
  110. onSearchError={err => setSearchError(err)}
  111. onBlur={onBlur}
  112. onFocus={onFocus}
  113. keywordOnInit={keywordOnInit}
  114. disableIncrementalSearch={disableIncrementalSearch}
  115. helpElement={<SearchFormHelp isReachable={isSearchServiceReachable} />}
  116. />
  117. );
  118. };
  119. export default forwardRef(SearchForm);