Jelajahi Sumber

BugFix for the behavior when item is selected

Yuki Takei 4 tahun lalu
induk
melakukan
1c1e028346

+ 13 - 1
packages/app/src/components/Navbar/GlobalSearch.tsx

@@ -3,8 +3,10 @@ import React, {
 } from 'react';
 import { useTranslation } from 'react-i18next';
 
-import { withUnstatedContainers } from '../UnstatedUtils';
 import AppContainer from '~/client/services/AppContainer';
+import { IPage } from '~/interfaces/page';
+
+import { withUnstatedContainers } from '../UnstatedUtils';
 
 import SearchForm from '../SearchForm';
 
@@ -22,6 +24,15 @@ const GlobalSearch: FC<Props> = (props: Props) => {
   const [text, setText] = useState('');
   const [isScopeChildren, setScopeChildren] = useState<boolean>(appContainer.getConfig().isSearchScopeChildrenAsDefault);
 
+  const gotoPage = useCallback((data: unknown[]) => {
+    const page = data[0] as IPage; // should be single page selected
+
+    // navigate to page
+    if (page != null) {
+      window.location.href = page.path;
+    }
+  }, []);
+
   const search = useCallback(() => {
     const url = new URL(window.location.href);
     url.pathname = '/_search';
@@ -61,6 +72,7 @@ const GlobalSearch: FC<Props> = (props: Props) => {
         <SearchForm
           isSearchServiceReachable={isSearchServiceReachable}
           dropup={dropup}
+          onChange={gotoPage}
           onInputChange={text => setText(text)}
           onSubmit={search}
         />

+ 4 - 1
packages/app/src/components/SearchForm.tsx

@@ -83,6 +83,7 @@ type Props = {
 
   dropup?: boolean,
   keyword?: string,
+  onChange?: (data: unknown[]) => void,
   onSubmit?: (input: string) => void,
   onInputChange?: (text: string) => void,
 };
@@ -91,7 +92,8 @@ type Props = {
 const SearchForm: ForwardRefRenderFunction<IFocusable, Props> = (props: Props, ref) => {
   const { t } = useTranslation();
   const {
-    isSearchServiceReachable, dropup, onSubmit, onInputChange,
+    isSearchServiceReachable, dropup,
+    onChange, onSubmit, onInputChange,
   } = props;
 
   const [searchError, setSearchError] = useState<Error | null>(null);
@@ -123,6 +125,7 @@ const SearchForm: ForwardRefRenderFunction<IFocusable, Props> = (props: Props, r
       dropup={dropup}
       emptyLabel={emptyLabel}
       placeholder={placeholder}
+      onChange={onChange}
       onSubmit={onSubmit}
       onInputChange={onInputChange}
       onSearchError={err => setSearchError(err)}