diff --git a/packages/select/__tests__/select.spec.ts b/packages/select/__tests__/select.spec.ts index 2431b86fd4..d1ca5c5c7d 100644 --- a/packages/select/__tests__/select.spec.ts +++ b/packages/select/__tests__/select.spec.ts @@ -4,6 +4,7 @@ import { EVENT_CODE } from '@element-plus/utils/aria' import Select from '../src/select.vue' import Group from '../src/option-group.vue' import Option from '../src/option.vue' +import { sleep } from '@element-plus/test-utils' jest.useFakeTimers() @@ -313,11 +314,20 @@ describe('Select', () => { vm.navigateOptions('next') } vm.navigateOptions('prev') + vm.navigateOptions('prev') + vm.navigateOptions('prev') await vm.$nextTick() - expect(vm.hoverIndex).toBe(0) + expect(vm.hoverIndex).toBe(3) vm.selectOption() await vm.$nextTick() - expect((wrapper.vm as any).value).toBe('选项1') + expect((wrapper.vm as any).value).toBe('选项4') + vm.toggleMenu() + const timer = sleep(300) + jest.runAllTimers() + await timer + vm.toggleMenu() + await vm.$nextTick + expect(vm.hoverIndex).toBe(3) }) test('clearable', async () => { diff --git a/packages/select/src/useSelect.ts b/packages/select/src/useSelect.ts index e181a41f8d..238fa0ed96 100644 --- a/packages/select/src/useSelect.ts +++ b/packages/select/src/useSelect.ts @@ -408,11 +408,18 @@ export const useSelect = (props, states: States, ctx) => { const resetHoverIndex = () => { setTimeout(() => { + const valueKey = props.valueKey if (!props.multiple) { - states.hoverIndex = optionsArray.value.indexOf(states.selected) + states.hoverIndex = optionsArray.value.findIndex(item => { + return getValueByPath(item, valueKey) === getValueByPath(states.selected, valueKey) + }) } else { if (states.selected.length > 0) { - states.hoverIndex = Math.min.apply(null, states.selected.map(item => optionsArray.value.indexOf(item))) + states.hoverIndex = Math.min.apply(null, states.selected.map(selected => { + return optionsArray.value.findIndex(item => { + return getValueByPath(item, valueKey) === getValueByPath(selected, valueKey) + }) + })) } else { states.hoverIndex = -1 }