Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(select): 修复过滤选项后上下键与Enter键操作无法正常选取值的问题 #3610

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/select/hooks/useKeyboardControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import type { ChangeHandler } from '../../hooks/useVModel';
import type { PopupVisibleChangeContext } from '../../popup';

export type useKeyboardControlType = {
displayOptions: ComputedRef<SelectOption[]>;
displayOptions: ComputedRef<
SelectOption &
{
index?: number;
disabled?: boolean;
}[]
>;
optionsList: ComputedRef<TdOptionProps[]>;
innerPopupVisible: Ref<boolean>;
setInnerPopupVisible: ChangeHandler<boolean, [context: PopupVisibleChangeContext]>;
Expand Down Expand Up @@ -37,6 +43,7 @@ export default function useKeyboardControl({
multiple,
max,
}: useKeyboardControlType) {
let optionIndex = -1;
const hoverIndex = ref(-1);
const virtualFilteredOptions = ref([]); // 处理虚拟滚动下选项过滤通过键盘选择的问题
const classPrefix = usePrefixClass();
Expand All @@ -54,7 +61,7 @@ export default function useKeyboardControl({
} else {
newIndex--;
}
if (optionsList.value[newIndex]?.disabled) {
if (displayOptions.value[newIndex]?.disabled) {
newIndex--;
}
hoverIndex.value = newIndex;
Expand All @@ -66,7 +73,7 @@ export default function useKeyboardControl({
} else {
newIndex++;
}
if (optionsList.value[newIndex]?.disabled) {
if (displayOptions.value[newIndex]?.disabled) {
newIndex++;
}
hoverIndex.value = newIndex;
Expand All @@ -83,17 +90,17 @@ export default function useKeyboardControl({
: optionsList.value;

if (!multiple) {
const selectedOptions = getSelectedOptions(filteredOptions[hoverIndex.value].value);
setInnerValue(filteredOptions[hoverIndex.value].value, {
const selectedOptions = getSelectedOptions(filteredOptions[optionIndex].value);
setInnerValue(filteredOptions[optionIndex].value, {
option: selectedOptions?.[0],
selectedOptions: getSelectedOptions(filteredOptions[hoverIndex.value].value),
selectedOptions: getSelectedOptions(filteredOptions[optionIndex].value),
trigger: 'check',
e,
});
setInnerPopupVisible(false, { e });
} else {
if (hoverIndex.value === -1) return;
const optionValue = filteredOptions[hoverIndex.value]?.value;
const optionValue = filteredOptions[optionIndex]?.value;

if (!optionValue) return;
const newValue = getNewMultipleValue(innerValue.value, optionValue);
Expand Down Expand Up @@ -134,6 +141,9 @@ export default function useKeyboardControl({
top: scrollHeight,
behavior: 'smooth',
});

// 保存hoverIndex对应选项的index值
optionIndex = index >= 0 ? displayOptions.value[index]?.index : index;
});

return {
Expand Down