fix(components): [select] switching single/multiple generates an error (#18030)

closed #17890
This commit is contained in:
qiang 2024-08-29 19:06:41 +08:00 committed by GitHub
parent 8618a6bcd0
commit 044f0afc1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 12 deletions

View File

@ -2741,4 +2741,14 @@ describe('Select', () => {
await wrapper.find(`.${WRAPPER_CLASS_NAME}`).trigger('click')
expect(handleClick).toHaveBeenCalledOnce()
})
test('should be run normally when switching multiple', async () => {
wrapper = getSelectVm({ multiple: false })
const vm = wrapper.vm as any
await (vm.value = undefined)
await (vm.multiple = true)
await (vm.multiple = false)
expect(vm.value).toBe(undefined)
})
})

View File

@ -66,7 +66,7 @@ export const useSelect = (props: ISelectProps, emit) => {
cachedOptions: new Map(),
disabledOptions: new Map(),
optionValues: [] as any[], // sorted value of options
selected: props.multiple ? [] : ({} as any),
selected: [] as any[],
selectionWidth: 0,
calculatorWidth: 0,
collapseItemWidth: 0,
@ -406,7 +406,7 @@ export const useSelect = (props: ISelectProps, emit) => {
: props.modelValue
const option = getOption(value)
states.selectedLabel = option.currentLabel
states.selected = option
states.selected = [option]
return
} else {
states.selectedLabel = ''
@ -456,17 +456,11 @@ export const useSelect = (props: ISelectProps, emit) => {
}
const updateHoveringIndex = () => {
if (!props.multiple) {
states.hoveringIndex = optionsArray.value.findIndex((item) => {
return getValueKey(item) === getValueKey(states.selected)
})
} else {
states.hoveringIndex = optionsArray.value.findIndex((item) =>
states.selected.some(
(selected) => getValueKey(selected) === getValueKey(item)
)
states.hoveringIndex = optionsArray.value.findIndex((item) =>
states.selected.some(
(selected) => getValueKey(selected) === getValueKey(item)
)
}
)
}
const resetSelectionWidth = () => {