SearchForm.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. import styles from './SearchForm.module.scss';
  11. type SearchFormHelpProps = {
  12. isReachable: boolean,
  13. }
  14. const SearchFormHelp: FC<SearchFormHelpProps> = React.memo((props: SearchFormHelpProps) => {
  15. const { t } = useTranslation();
  16. const { isReachable } = 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. return (
  26. <table className={`${styles['grw-search-table']} table grw-search-table search-help m-0`}>
  27. <caption className="text-left text-primary p-2">
  28. <h5 className="h6"><i className="icon-magnifier pr-2 mb-2" />{ t('search_help.title') }</h5>
  29. </caption>
  30. <tbody>
  31. <tr>
  32. <th className="py-2">
  33. <code>word1</code> <code>word2</code><br></br>
  34. <small>({ t('search_help.and.syntax help') })</small>
  35. </th>
  36. <td><h6 className="m-0">{ t('search_help.and.desc', { word1: 'word1', word2: 'word2' }) }</h6></td>
  37. </tr>
  38. <tr>
  39. <th className="py-2">
  40. <code>&quot;This is GROWI&quot;</code><br></br>
  41. <small>({ t('search_help.phrase.syntax help') })</small>
  42. </th>
  43. <td><h6 className="m-0">{ t('search_help.phrase.desc', { phrase: 'This is GROWI' }) }</h6></td>
  44. </tr>
  45. <tr>
  46. <th className="py-2"><code>-keyword</code></th>
  47. <td><h6 className="m-0">{ t('search_help.exclude.desc', { word: 'keyword' }) }</h6></td>
  48. </tr>
  49. <tr>
  50. <th className="py-2"><code>prefix:/user/</code></th>
  51. <td><h6 className="m-0">{ t('search_help.prefix.desc', { path: '/user/' }) }</h6></td>
  52. </tr>
  53. <tr>
  54. <th className="py-2"><code>-prefix:/user/</code></th>
  55. <td><h6 className="m-0">{ t('search_help.exclude_prefix.desc', { path: '/user/' }) }</h6></td>
  56. </tr>
  57. <tr>
  58. <th className="py-2"><code>tag:wiki</code></th>
  59. <td><h6 className="m-0">{ t('search_help.tag.desc', { tag: 'wiki' }) }</h6></td>
  60. </tr>
  61. <tr>
  62. <th className="py-2"><code>-tag:wiki</code></th>
  63. <td><h6 className="m-0">{ t('search_help.exclude_tag.desc', { tag: 'wiki' }) }</h6></td>
  64. </tr>
  65. </tbody>
  66. </table>
  67. );
  68. });
  69. SearchFormHelp.displayName = 'SearchFormHelp';
  70. type Props = TypeaheadProps & {
  71. isSearchServiceReachable: boolean,
  72. keywordOnInit?: string,
  73. disableIncrementalSearch?: boolean,
  74. onChange?: (data: IPageWithSearchMeta[]) => void,
  75. onSubmit?: (input: string) => void,
  76. };
  77. const SearchForm: ForwardRefRenderFunction<IFocusable, Props> = (props: Props, ref) => {
  78. const { t } = useTranslation();
  79. const {
  80. isSearchServiceReachable,
  81. keywordOnInit,
  82. disableIncrementalSearch,
  83. dropup, onChange, onBlur, onFocus, onSubmit, onInputChange,
  84. } = props;
  85. const [searchError, setSearchError] = useState<Error | null>(null);
  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={onBlur}
  113. onFocus={onFocus}
  114. keywordOnInit={keywordOnInit}
  115. disableIncrementalSearch={disableIncrementalSearch}
  116. helpElement={<SearchFormHelp isReachable={isSearchServiceReachable} />}
  117. />
  118. );
  119. };
  120. export default forwardRef(SearchForm);