SearchForm.tsx 4.4 KB

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