SearchForm.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { 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. type Props = TypeaheadProps & {
  69. isSearchServiceReachable: boolean,
  70. keywordOnInit?: string,
  71. disableIncrementalSearch?: boolean,
  72. onChange?: (data: IPageWithSearchMeta[]) => void,
  73. onSubmit?: (input: string) => void,
  74. };
  75. const SearchForm: ForwardRefRenderFunction<IFocusable, Props> = (props: Props, ref) => {
  76. const { t } = useTranslation();
  77. const {
  78. isSearchServiceReachable,
  79. keywordOnInit,
  80. disableIncrementalSearch,
  81. dropup, onChange, onBlur, onFocus, onSubmit, onInputChange,
  82. } = props;
  83. const [searchError, setSearchError] = useState<Error | null>(null);
  84. const searchTyheaheadRef = useRef<IFocusable>(null);
  85. // publish focus()
  86. useImperativeHandle(ref, () => ({
  87. focus() {
  88. const instance = searchTyheaheadRef?.current;
  89. if (instance != null) {
  90. instance.focus();
  91. }
  92. },
  93. }));
  94. const placeholder = isSearchServiceReachable
  95. ? 'Search ...'
  96. : 'Error on Search Service';
  97. const emptyLabel = (searchError != null)
  98. ? 'Error on searching.'
  99. : t('search.search page bodies');
  100. return (
  101. <SearchTypeahead
  102. ref={searchTyheaheadRef}
  103. dropup={dropup}
  104. emptyLabel={emptyLabel}
  105. placeholder={placeholder}
  106. onChange={onChange}
  107. onSubmit={onSubmit}
  108. onInputChange={onInputChange}
  109. onSearchError={err => setSearchError(err)}
  110. onBlur={onBlur}
  111. onFocus={onFocus}
  112. keywordOnInit={keywordOnInit}
  113. disableIncrementalSearch={disableIncrementalSearch}
  114. helpElement={<SearchFormHelp isReachable={isSearchServiceReachable} />}
  115. />
  116. );
  117. };
  118. export default forwardRef(SearchForm);