SearchForm.tsx 4.6 KB

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