| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import React, {
- FC, forwardRef, ForwardRefRenderFunction, useImperativeHandle,
- useRef, useState,
- } from 'react';
- import { useTranslation } from 'react-i18next';
- import { IFocusable } from '~/client/interfaces/focusable';
- import { IPageWithMeta } from '~/interfaces/page';
- import { IPageSearchMeta } from '~/interfaces/search';
- import SearchTypeahead from './SearchTypeahead';
- type SearchFormHelpProps = {
- isReachable: boolean,
- isShownHelp: boolean,
- }
- const SearchFormHelp: FC<SearchFormHelpProps> = (props: SearchFormHelpProps) => {
- const { t } = useTranslation();
- const { isReachable, isShownHelp } = props;
- if (!isReachable) {
- return (
- <>
- <h5 className="text-danger">Error occured on Search Service</h5>
- Try to reconnect from management page.
- </>
- );
- }
- if (!isShownHelp) {
- return <></>;
- }
- return (
- <table className="table grw-search-table search-help m-0">
- <caption className="text-left text-primary p-2">
- <h5 className="h6"><i className="icon-magnifier pr-2 mb-2" />{ t('search_help.title') }</h5>
- </caption>
- <tbody>
- <tr>
- <th className="py-2">
- <code>word1</code> <code>word2</code><br></br>
- <small>({ t('search_help.and.syntax help') })</small>
- </th>
- <td><h6 className="m-0">{ t('search_help.and.desc', { word1: 'word1', word2: 'word2' }) }</h6></td>
- </tr>
- <tr>
- <th className="py-2">
- <code>"This is GROWI"</code><br></br>
- <small>({ t('search_help.phrase.syntax help') })</small>
- </th>
- <td><h6 className="m-0">{ t('search_help.phrase.desc', { phrase: 'This is GROWI' }) }</h6></td>
- </tr>
- <tr>
- <th className="py-2"><code>-keyword</code></th>
- <td><h6 className="m-0">{ t('search_help.exclude.desc', { word: 'keyword' }) }</h6></td>
- </tr>
- <tr>
- <th className="py-2"><code>prefix:/user/</code></th>
- <td><h6 className="m-0">{ t('search_help.prefix.desc', { path: '/user/' }) }</h6></td>
- </tr>
- <tr>
- <th className="py-2"><code>-prefix:/user/</code></th>
- <td><h6 className="m-0">{ t('search_help.exclude_prefix.desc', { path: '/user/' }) }</h6></td>
- </tr>
- <tr>
- <th className="py-2"><code>tag:wiki</code></th>
- <td><h6 className="m-0">{ t('search_help.tag.desc', { tag: 'wiki' }) }</h6></td>
- </tr>
- <tr>
- <th className="py-2"><code>-tag:wiki</code></th>
- <td><h6 className="m-0">{ t('search_help.exclude_tag.desc', { tag: 'wiki' }) }</h6></td>
- </tr>
- </tbody>
- </table>
- );
- };
- type Props = {
- isSearchServiceReachable: boolean,
- dropup?: boolean,
- keyword?: string,
- disableIncrementalSearch?: boolean,
- onChange?: (data: IPageWithMeta<IPageSearchMeta>[]) => void,
- onBlur?: () => void,
- onFocus?: () => void,
- onSubmit?: (input: string) => void,
- onInputChange?: (text: string) => void,
- };
- const SearchForm: ForwardRefRenderFunction<IFocusable, Props> = (props: Props, ref) => {
- const { t } = useTranslation();
- const {
- isSearchServiceReachable, dropup,
- disableIncrementalSearch,
- onChange, onBlur, onFocus, onSubmit, onInputChange,
- } = props;
- const [searchError, setSearchError] = useState<Error | null>(null);
- const [isShownHelp, setShownHelp] = useState(false);
- const searchTyheaheadRef = useRef<IFocusable>(null);
- // publish focus()
- useImperativeHandle(ref, () => ({
- focus() {
- const instance = searchTyheaheadRef?.current;
- if (instance != null) {
- instance.focus();
- }
- },
- }));
- const placeholder = isSearchServiceReachable
- ? 'Search ...'
- : 'Error on Search Service';
- const emptyLabel = (searchError != null)
- ? 'Error on searching.'
- : t('search.search page bodies');
- return (
- <SearchTypeahead
- ref={searchTyheaheadRef}
- dropup={dropup}
- emptyLabel={emptyLabel}
- placeholder={placeholder}
- disableIncrementalSearch={disableIncrementalSearch}
- onChange={onChange}
- onSubmit={onSubmit}
- onInputChange={onInputChange}
- onSearchError={err => setSearchError(err)}
- onBlur={() => {
- setShownHelp(false);
- if (onBlur != null) {
- onBlur();
- }
- }}
- onFocus={() => {
- setShownHelp(true);
- if (onFocus != null) {
- onFocus();
- }
- }}
- helpElement={<SearchFormHelp isShownHelp={isShownHelp} isReachable={isSearchServiceReachable} />}
- keywordOnInit={props.keyword}
- />
- );
- };
- export default forwardRef(SearchForm);
|