fix(select): hoverIndex has incorrect when popover is turned on again (#2469)

This commit is contained in:
msidolphin 2021-07-13 09:42:13 +08:00 committed by GitHub
parent f6b17cb79a
commit c1770d8877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -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 () => {

View File

@ -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
}