From a36fdf6a7d031ef2b6514bfba00f566dcdc637d6 Mon Sep 17 00:00:00 2001 From: HyaCinth <88471803+HyaCiovo@users.noreply.github.com> Date: Wed, 13 Aug 2025 17:14:21 +0800 Subject: [PATCH] chore(cmdk): Resolve default option selection issue in GotoAnything component (#23878) (#23813) --- web/app/components/goto-anything/index.tsx | 83 +++++++++++----------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/web/app/components/goto-anything/index.tsx b/web/app/components/goto-anything/index.tsx index bff0773a4..22e666154 100644 --- a/web/app/components/goto-anything/index.tsx +++ b/web/app/components/goto-anything/index.tsx @@ -32,7 +32,7 @@ const GotoAnything: FC = ({ const { t } = useTranslation() const [show, setShow] = useState(false) const [searchQuery, setSearchQuery] = useState('') - const [cmdVal, setCmdVal] = useState('') + const [cmdVal, setCmdVal] = useState('_') const inputRef = useRef(null) const handleNavSearch = useCallback((q: string) => { setShow(true) @@ -120,9 +120,14 @@ const GotoAnything: FC = ({ }, ) + // Prevent automatic selection of the first option when cmdVal is not set + const clearSelection = () => { + setCmdVal('_') + } + const handleCommandSelect = useCallback((commandKey: string) => { setSearchQuery(`${commandKey} `) - setCmdVal('') + clearSelection() setTimeout(() => { inputRef.current?.focus() }, 0) @@ -233,9 +238,6 @@ const GotoAnything: FC = ({ inputRef.current?.focus() }) } - return () => { - setCmdVal('') - } }, [show]) return ( @@ -245,6 +247,7 @@ const GotoAnything: FC = ({ onClose={() => { setShow(false) setSearchQuery('') + clearSelection() onHide?.() }} closable={false} @@ -268,7 +271,7 @@ const GotoAnything: FC = ({ onChange={(e) => { setSearchQuery(e.target.value) if (!e.target.value.startsWith('@')) - setCmdVal('') + clearSelection() }} className='flex-1 !border-0 !bg-transparent !shadow-none' wrapperClassName='flex-1 !border-0 !bg-transparent' @@ -321,40 +324,40 @@ const GotoAnything: FC = ({ /> ) : ( Object.entries(groupedResults).map(([type, results], groupIndex) => ( - { - const typeMap: Record = { - 'app': 'app.gotoAnything.groups.apps', - 'plugin': 'app.gotoAnything.groups.plugins', - 'knowledge': 'app.gotoAnything.groups.knowledgeBases', - 'workflow-node': 'app.gotoAnything.groups.workflowNodes', - } - return t(typeMap[type] || `${type}s`) - })()} className='p-2 capitalize text-text-secondary'> - {results.map(result => ( - handleNavigate(result)} - > - {result.icon} -
-
- {result.title} -
- {result.description && ( -
- {result.description} + { + const typeMap: Record = { + 'app': 'app.gotoAnything.groups.apps', + 'plugin': 'app.gotoAnything.groups.plugins', + 'knowledge': 'app.gotoAnything.groups.knowledgeBases', + 'workflow-node': 'app.gotoAnything.groups.workflowNodes', + } + return t(typeMap[type] || `${type}s`) + })()} className='p-2 capitalize text-text-secondary'> + {results.map(result => ( + handleNavigate(result)} + > + {result.icon} +
+
+ {result.title}
- )} -
-
- {result.type} -
-
- ))} -
- )) + {result.description && ( +
+ {result.description} +
+ )} +
+
+ {result.type} +
+ + ))} + + )) )} {!isCommandsMode && emptyResult} {!isCommandsMode && defaultUI} @@ -373,7 +376,7 @@ const GotoAnything: FC = ({ {t('app.gotoAnything.resultCount', { count: searchResults.length })} {searchMode !== 'general' && ( -{t('app.gotoAnything.inScope', { scope: searchMode.replace('@', '') })} + {t('app.gotoAnything.inScope', { scope: searchMode.replace('@', '') })} )}