|
@@ -1,5 +1,5 @@
|
|
|
import React, {
|
|
import React, {
|
|
|
- FC, KeyboardEvent, useCallback, useReducer, useState,
|
|
|
|
|
|
|
+ FC, KeyboardEvent, useCallback, useMemo, useReducer, useState,
|
|
|
} from 'react';
|
|
} from 'react';
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
import { AxiosResponse } from 'axios';
|
|
import { AxiosResponse } from 'axios';
|
|
@@ -54,7 +54,9 @@ type Props = {
|
|
|
|
|
|
|
|
export const SearchTypeahead: FC<Props> = (props: Props) => {
|
|
export const SearchTypeahead: FC<Props> = (props: Props) => {
|
|
|
const {
|
|
const {
|
|
|
|
|
+ keywordOnInit,
|
|
|
onSearchSuccess, onSearchError, onInputChange, onSubmit,
|
|
onSearchSuccess, onSearchError, onInputChange, onSubmit,
|
|
|
|
|
+ emptyLabel, emptyLabelExceptError, helpElement,
|
|
|
} = props;
|
|
} = props;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
@@ -64,6 +66,22 @@ export const SearchTypeahead: FC<Props> = (props: Props) => {
|
|
|
const [searchError, setSearchError] = useState<Error | null>(null);
|
|
const [searchError, setSearchError] = useState<Error | null>(null);
|
|
|
const [isLoading, setLoaded] = useReducer(() => true, false);
|
|
const [isLoading, setLoaded] = useReducer(() => true, false);
|
|
|
|
|
|
|
|
|
|
+ const changeKeyword = (text) => {
|
|
|
|
|
+ // see https://github.com/ericgio/react-bootstrap-typeahead/issues/266#issuecomment-414987723
|
|
|
|
|
+ // const instance = this.typeahead.getInstance();
|
|
|
|
|
+ // instance.clear();
|
|
|
|
|
+ // instance.setState({ text });
|
|
|
|
|
+ console.log('changeKeyword', text);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const restoreInitialData = () => {
|
|
|
|
|
+ changeKeyword(keywordOnInit);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const clearKeyword = () => {
|
|
|
|
|
+ changeKeyword('');
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Callback function which is occured when search is exit successfully
|
|
* Callback function which is occured when search is exit successfully
|
|
|
*/
|
|
*/
|
|
@@ -124,29 +142,21 @@ export const SearchTypeahead: FC<Props> = (props: Props) => {
|
|
|
}
|
|
}
|
|
|
}, [input, onSubmit]);
|
|
}, [input, onSubmit]);
|
|
|
|
|
|
|
|
- // getEmptyLabel() {
|
|
|
|
|
- // const { emptyLabel, helpElement } = this.props;
|
|
|
|
|
- // const { input } = this.state;
|
|
|
|
|
-
|
|
|
|
|
- // // show help element if empty
|
|
|
|
|
- // if (input.length === 0) {
|
|
|
|
|
- // return helpElement;
|
|
|
|
|
- // }
|
|
|
|
|
-
|
|
|
|
|
- // // use props.emptyLabel as is if defined
|
|
|
|
|
- // if (emptyLabel !== undefined) {
|
|
|
|
|
- // return this.props.emptyLabel;
|
|
|
|
|
- // }
|
|
|
|
|
|
|
+ const getEmptyLabel = () => {
|
|
|
|
|
+ // show help element if empty
|
|
|
|
|
+ if (input.length === 0) {
|
|
|
|
|
+ return helpElement;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // let emptyLabelExceptError = 'No matches found on title...';
|
|
|
|
|
- // if (this.props.emptyLabelExceptError !== undefined) {
|
|
|
|
|
- // emptyLabelExceptError = this.props.emptyLabelExceptError;
|
|
|
|
|
- // }
|
|
|
|
|
|
|
+ // use props.emptyLabel as is if defined
|
|
|
|
|
+ if (emptyLabel !== undefined) {
|
|
|
|
|
+ return emptyLabel;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // return (this.state.searchError !== null)
|
|
|
|
|
- // ? 'Error on searching.'
|
|
|
|
|
- // : emptyLabelExceptError;
|
|
|
|
|
- // }
|
|
|
|
|
|
|
+ return (searchError !== null)
|
|
|
|
|
+ ? 'Error on searching.'
|
|
|
|
|
+ : (emptyLabelExceptError ?? 'No matches found on title...');
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
const defaultSelected = (props.keywordOnInit !== '')
|
|
const defaultSelected = (props.keywordOnInit !== '')
|
|
|
? [{ path: props.keywordOnInit }]
|
|
? [{ path: props.keywordOnInit }]
|
|
@@ -158,8 +168,15 @@ export const SearchTypeahead: FC<Props> = (props: Props) => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const isClearBtn = props.behaviorOfResetBtn === 'clear';
|
|
const isClearBtn = props.behaviorOfResetBtn === 'clear';
|
|
|
- // const resetForm = isClearBtn ? this.clearKeyword : this.restoreInitialData;
|
|
|
|
|
- const resetForm = () => {};
|
|
|
|
|
|
|
+ const resetForm = isClearBtn ? clearKeyword : restoreInitialData;
|
|
|
|
|
+
|
|
|
|
|
+ const renderMenuItemChildren = (page: IPage) => (
|
|
|
|
|
+ <span>
|
|
|
|
|
+ <UserPicture user={page.lastUpdateUser} size="sm" noLink />
|
|
|
|
|
+ <span className="ml-1 text-break text-wrap"><PagePathLabel page={page} /></span>
|
|
|
|
|
+ <PageListMeta page={page} />
|
|
|
|
|
+ </span>
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<div className="search-typeahead">
|
|
<div className="search-typeahead">
|
|
@@ -173,13 +190,13 @@ export const SearchTypeahead: FC<Props> = (props: Props) => {
|
|
|
minLength={0}
|
|
minLength={0}
|
|
|
options={pages} // Search result (Some page names)
|
|
options={pages} // Search result (Some page names)
|
|
|
promptText={props.helpElement}
|
|
promptText={props.helpElement}
|
|
|
- // emptyLabel={this.getEmptyLabel()}
|
|
|
|
|
|
|
+ emptyLabel={getEmptyLabel()}
|
|
|
align="left"
|
|
align="left"
|
|
|
submitFormOnEnter
|
|
submitFormOnEnter
|
|
|
onSearch={search}
|
|
onSearch={search}
|
|
|
onInputChange={inputChangeHandler}
|
|
onInputChange={inputChangeHandler}
|
|
|
onKeyDown={keyDownHandler}
|
|
onKeyDown={keyDownHandler}
|
|
|
- // renderMenuItemChildren={this.renderMenuItemChildren}
|
|
|
|
|
|
|
+ renderMenuItemChildren={renderMenuItemChildren}
|
|
|
caseSensitive={false}
|
|
caseSensitive={false}
|
|
|
defaultSelected={defaultSelected}
|
|
defaultSelected={defaultSelected}
|
|
|
autoFocus={props.autoFocus}
|
|
autoFocus={props.autoFocus}
|