SearchForm.tsx 4.1 KB

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