Просмотр исходного кода

If an item is not highlighted, perform a normal search.

Shun Miyazawa 2 лет назад
Родитель
Сommit
ef6f7f4e46

+ 15 - 4
apps/app/src/features/search/client/components/SearchForm.tsx

@@ -6,13 +6,16 @@ import { GetInputProps } from '../interfaces/downshift';
 
 
 type Props = {
 type Props = {
   searchKeyword: string,
   searchKeyword: string,
+  highlightedIndex: number | null,
   onChangeSearchText?: (text: string) => void,
   onChangeSearchText?: (text: string) => void,
   onClickClearButton?: () => void,
   onClickClearButton?: () => void,
+  onEnterKeyDownHandler?: () => void,
   getInputProps: GetInputProps,
   getInputProps: GetInputProps,
 }
 }
+
 export const SearchForm = (props: Props): JSX.Element => {
 export const SearchForm = (props: Props): JSX.Element => {
   const {
   const {
-    searchKeyword, onChangeSearchText, onClickClearButton, getInputProps,
+    searchKeyword, highlightedIndex, onChangeSearchText, onClickClearButton, onEnterKeyDownHandler, getInputProps,
   } = props;
   } = props;
 
 
   const inputRef = useRef<HTMLInputElement>(null);
   const inputRef = useRef<HTMLInputElement>(null);
@@ -29,16 +32,24 @@ export const SearchForm = (props: Props): JSX.Element => {
     }
     }
   }, [onClickClearButton]);
   }, [onClickClearButton]);
 
 
+  const enterKeyDownHandler = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {
+    const shouldExecuteHandler = e.key === 'Enter' && !e.nativeEvent.isComposing && highlightedIndex == null && searchKeyword.trim().length !== 0;
+    if (shouldExecuteHandler) {
+      onEnterKeyDownHandler?.();
+    }
+  }, [highlightedIndex, onEnterKeyDownHandler, searchKeyword]);
+
   const inputOption = useMemo(() => {
   const inputOption = useMemo(() => {
-    return ({
+    return {
       type: 'text',
       type: 'text',
       placeholder: 'Search...',
       placeholder: 'Search...',
       className: 'form-control',
       className: 'form-control',
       ref: inputRef,
       ref: inputRef,
       value: searchKeyword,
       value: searchKeyword,
       onChange: changeSearchTextHandler,
       onChange: changeSearchTextHandler,
-    });
-  }, [changeSearchTextHandler, searchKeyword]);
+      onKeyDown: enterKeyDownHandler,
+    };
+  }, [changeSearchTextHandler, enterKeyDownHandler, searchKeyword]);
 
 
   useEffect(() => {
   useEffect(() => {
     if (inputRef.current != null) {
     if (inputRef.current != null) {

+ 8 - 1
apps/app/src/features/search/client/components/SearchModal.tsx

@@ -30,10 +30,15 @@ const SearchModal = (): JSX.Element => {
   }, []);
   }, []);
 
 
   const selectSearchMenuItemHandler = useCallback((url: string) => {
   const selectSearchMenuItemHandler = useCallback((url: string) => {
-    closeSearchModal();
     router.push(url);
     router.push(url);
+    closeSearchModal();
   }, [closeSearchModal, router]);
   }, [closeSearchModal, router]);
 
 
+  const enterKeyDownHandler = useCallback(() => {
+    router.push(`/_search?q=${searchKeyword}`);
+    closeSearchModal();
+  }, [closeSearchModal, router, searchKeyword]);
+
   useEffect(() => {
   useEffect(() => {
     if (!searchModalData?.isOpened) {
     if (!searchModalData?.isOpened) {
       setSearchKeyword('');
       setSearchKeyword('');
@@ -54,9 +59,11 @@ const SearchModal = (): JSX.Element => {
           }) => (
           }) => (
             <div>
             <div>
               <SearchForm
               <SearchForm
+                highlightedIndex={highlightedIndex}
                 searchKeyword={searchKeyword}
                 searchKeyword={searchKeyword}
                 onChangeSearchText={changeSearchTextHandler}
                 onChangeSearchText={changeSearchTextHandler}
                 onClickClearButton={clickClearButtonHandler}
                 onClickClearButton={clickClearButtonHandler}
+                onEnterKeyDownHandler={enterKeyDownHandler}
                 getInputProps={getInputProps}
                 getInputProps={getInputProps}
               />
               />