Skip to content

Commit

Permalink
fix(auto-complete): fix: error when pressing enter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali authored and Ali-ovo committed Jun 21, 2023
1 parent 11e2329 commit d249bba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/auto-complete/AutoComplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const AutoComplete = forwardRef<AutoCompleteRef, AutoCompleteProps>((originalPro
})();

const onInputChange: TdInputProps['onChange'] = (value, context) => {
setPopupVisible(true);
setTValue(value, context);
};

Expand Down
11 changes: 9 additions & 2 deletions src/auto-complete/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface OptionsListRef {
}

const OptionsList = forwardRef<OptionsListRef, OptionsListProps>((props: OptionsListProps, ref) => {
const { value, onSelect, popupVisible } = props;
const { value, popupVisible, onSelect } = props;
const { classPrefix } = useConfig();
const [active, setActive] = useState('');
const activeIndexRef = useRef(-1);
Expand Down Expand Up @@ -86,7 +86,14 @@ const OptionsList = forwardRef<OptionsListRef, OptionsListProps>((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;
Expand Down

0 comments on commit d249bba

Please sign in to comment.