fix: update quick search close function

This commit is contained in:
JimmFly 2023-02-07 11:07:52 +08:00
parent 4d5213608b
commit c0a36d36cc
4 changed files with 18 additions and 25 deletions

View File

@ -1,26 +1,23 @@
import React from 'react'; import React from 'react';
import { AddIcon } from '@blocksuite/icons'; import { AddIcon } from '@blocksuite/icons';
import { StyledModalFooterContent } from './style'; import { StyledModalFooterContent } from './style';
import { useModal } from '@/providers/GlobalModalProvider';
import { Command } from 'cmdk'; import { Command } from 'cmdk';
import { usePageHelper } from '@/hooks/use-page-helper'; import { usePageHelper } from '@/hooks/use-page-helper';
import { useTranslation } from '@affine/i18n'; import { useTranslation } from '@affine/i18n';
export const Footer = (props: { query: string }) => { export const Footer = (props: { query: string; onClose: () => void }) => {
const { triggerQuickSearchModal } = useModal();
const { openPage, createPage } = usePageHelper(); const { openPage, createPage } = usePageHelper();
const { t } = useTranslation(); const { t } = useTranslation();
const query = props.query; const { query, onClose } = props;
return ( return (
<Command.Item <Command.Item
data-testid="quickSearch-addNewPage" data-testid="quickSearch-addNewPage"
onSelect={async () => { onSelect={async () => {
onClose();
const pageId = await createPage({ title: query }); const pageId = await createPage({ title: query });
if (pageId) { if (pageId) {
openPage(pageId); openPage(pageId);
} }
triggerQuickSearchModal();
}} }}
> >
<StyledModalFooterContent> <StyledModalFooterContent>

View File

@ -28,12 +28,9 @@ export const Input = (props: {
}, },
true true
); );
setInputValue(props.query);
return inputRef.current?.focus(); return inputRef.current?.focus();
}, [inputRef]); }, [inputRef, props.query]);
useEffect(() => {
return setInputValue(props.query);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return ( return (
<StyledInputContent> <StyledInputContent>
<StyledLabel htmlFor=":r5:"> <StyledLabel htmlFor=":r5:">

View File

@ -1,6 +1,5 @@
import { Command } from 'cmdk'; import { Command } from 'cmdk';
import { StyledListItem, StyledNotFound } from './style'; import { StyledListItem, StyledNotFound } from './style';
import { useModal } from '@/providers/GlobalModalProvider';
import { PaperIcon, EdgelessIcon } from '@blocksuite/icons'; import { PaperIcon, EdgelessIcon } from '@blocksuite/icons';
import { Dispatch, SetStateAction, useEffect, useState } from 'react'; import { Dispatch, SetStateAction, useEffect, useState } from 'react';
import { useAppState } from '@/providers/app-state-provider'; import { useAppState } from '@/providers/app-state-provider';
@ -12,14 +11,11 @@ import usePageHelper from '@/hooks/use-page-helper';
export const Results = (props: { export const Results = (props: {
query: string; query: string;
loading: boolean; loading: boolean;
onClose: () => void;
setLoading: Dispatch<SetStateAction<boolean>>; setLoading: Dispatch<SetStateAction<boolean>>;
setShowCreatePage: Dispatch<SetStateAction<boolean>>; setShowCreatePage: Dispatch<SetStateAction<boolean>>;
}) => { }) => {
const query = props.query; const { query, loading, setLoading, setShowCreatePage, onClose } = props;
const loading = props.loading;
const setLoading = props.setLoading;
const setShowCreatePage = props.setShowCreatePage;
const { triggerQuickSearchModal } = useModal();
const { openPage } = usePageHelper(); const { openPage } = usePageHelper();
const router = useRouter(); const router = useRouter();
const { currentWorkspace, pageList } = useAppState(); const { currentWorkspace, pageList } = useAppState();
@ -55,8 +51,8 @@ export const Results = (props: {
<Command.Item <Command.Item
key={result.id} key={result.id}
onSelect={() => { onSelect={() => {
onClose();
openPage(result.id); openPage(result.id);
triggerQuickSearchModal();
}} }}
value={result.id} value={result.id}
> >
@ -86,8 +82,8 @@ export const Results = (props: {
key={link.title} key={link.title}
value={link.title} value={link.title}
onSelect={() => { onSelect={() => {
onClose();
router.push(link.href); router.push(link.href);
triggerQuickSearchModal();
}} }}
> >
<StyledListItem> <StyledListItem>

View File

@ -31,11 +31,16 @@ export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
const [showCreatePage, setShowCreatePage] = useState(true); const [showCreatePage, setShowCreatePage] = useState(true);
const { triggerQuickSearchModal } = useModal(); const { triggerQuickSearchModal } = useModal();
const handleClose = () => {
setQuery('');
onClose();
};
// Add ‘⌘+K shortcut keys as switches // Add ‘⌘+K shortcut keys as switches
useEffect(() => { useEffect(() => {
const down = (e: KeyboardEvent) => { const down = (e: KeyboardEvent) => {
if ((e.key === 'k' && e.metaKey) || (e.key === 'k' && e.ctrlKey)) { if ((e.key === 'k' && e.metaKey) || (e.key === 'k' && e.ctrlKey)) {
const selection = window.getSelection(); const selection = window.getSelection();
setQuery('');
if (selection?.toString()) { if (selection?.toString()) {
triggerQuickSearchModal(false); triggerQuickSearchModal(false);
return; return;
@ -45,9 +50,6 @@ export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
} }
} }
}; };
if (!open) {
setQuery('');
}
document.addEventListener('keydown', down, { capture: true }); document.addEventListener('keydown', down, { capture: true });
return () => return () =>
document.removeEventListener('keydown', down, { capture: true }); document.removeEventListener('keydown', down, { capture: true });
@ -64,7 +66,7 @@ export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
return ( return (
<Modal <Modal
open={open} open={open}
onClose={onClose} onClose={handleClose}
wrapperPosition={['top', 'center']} wrapperPosition={['top', 'center']}
data-testid="quickSearch" data-testid="quickSearch"
> >
@ -108,6 +110,7 @@ export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
query={query} query={query}
loading={loading} loading={loading}
setLoading={setLoading} setLoading={setLoading}
onClose={handleClose}
setShowCreatePage={setShowCreatePage} setShowCreatePage={setShowCreatePage}
/> />
) : ( ) : (
@ -115,7 +118,7 @@ export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
query={query} query={query}
loading={loading} loading={loading}
setLoading={setLoading} setLoading={setLoading}
onClose={onClose} onClose={handleClose}
setPublishWorkspaceName={setPublishWorkspaceName} setPublishWorkspaceName={setPublishWorkspaceName}
/> />
)} )}
@ -125,7 +128,7 @@ export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => {
<> <>
<StyledModalDivider /> <StyledModalDivider />
<StyledModalFooter> <StyledModalFooter>
<Footer query={query} /> <Footer query={query} onClose={handleClose} />
</StyledModalFooter> </StyledModalFooter>
</> </>
) : null ) : null