From d249bba3e0d7ed8271933f6a16441d53cc4d3d3d Mon Sep 17 00:00:00 2001 From: Ali Date: Mon, 19 Jun 2023 17:01:36 +0800 Subject: [PATCH] fix(auto-complete): fix: error when pressing enter fix #2298 --- src/auto-complete/AutoComplete.tsx | 1 + src/auto-complete/OptionList.tsx | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/auto-complete/AutoComplete.tsx b/src/auto-complete/AutoComplete.tsx index ee8392b5fb..a418dc76d4 100644 --- a/src/auto-complete/AutoComplete.tsx +++ b/src/auto-complete/AutoComplete.tsx @@ -63,6 +63,7 @@ const AutoComplete = forwardRef((originalPro })(); const onInputChange: TdInputProps['onChange'] = (value, context) => { + setPopupVisible(true); setTValue(value, context); }; diff --git a/src/auto-complete/OptionList.tsx b/src/auto-complete/OptionList.tsx index 55978dc7c5..72060a640f 100644 --- a/src/auto-complete/OptionList.tsx +++ b/src/auto-complete/OptionList.tsx @@ -26,7 +26,7 @@ export interface OptionsListRef { } const OptionsList = forwardRef((props: OptionsListProps, ref) => { - const { value, onSelect, popupVisible } = props; + const { value, popupVisible, onSelect } = props; const { classPrefix } = useConfig(); const [active, setActive] = useState(''); const activeIndexRef = useRef(-1); @@ -86,7 +86,14 @@ const OptionsList = forwardRef((props: Options // 键盘事件,上下选择 const onKeyInnerPress = (e: KeyboardEvent) => { if (e.code === 'Enter' || e.key === 'Enter') { - onSelect?.(tOptions[activeIndexRef.current].text, { e }); + // 当没有匹配项的时候, 默认回车选中第一个 + let currentIndex = activeIndexRef.current; + + if (!~currentIndex) { + currentIndex = 0; + } + + onSelect?.(tOptions[currentIndex]?.text, { e }); } else { const index = activeIndexRef.current; let newIndex;