fix(runtime-dom): `v-model` string/number coercion for multiselect options (#10576)

Co-authored-by: RicardoErii <‘1974364190@qq.com’>
Co-authored-by: yangchangtao <yangchangtao@kuaishou.com>
This commit is contained in:
yangxiuxiu 2024-03-28 22:09:16 +08:00 committed by GitHub
parent 01172fdb77
commit db374e54c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 3 deletions

View File

@ -1237,4 +1237,36 @@ describe('vModel', () => {
await nextTick()
expect(data.value).toEqual('使用拼音输入')
})
it('multiple select (model is number, option value is string)', async () => {
const component = defineComponent({
data() {
return {
value: [1, 2],
}
},
render() {
return [
withVModel(
h(
'select',
{
multiple: true,
'onUpdate:modelValue': setValue.bind(this),
},
[h('option', { value: '1' }), h('option', { value: '2' })],
),
this.value,
),
]
},
})
render(h(component), root)
await nextTick()
const [foo, bar] = root.querySelectorAll('option')
expect(foo.selected).toEqual(true)
expect(bar.selected).toEqual(true)
})
})

View File

@ -242,9 +242,7 @@ function setSelected(el: HTMLSelectElement, value: any, number: boolean) {
const optionType = typeof optionValue
// fast path for string / number values
if (optionType === 'string' || optionType === 'number') {
option.selected = value.includes(
number ? looseToNumber(optionValue) : optionValue,
)
option.selected = value.some(v => String(v) === String(optionValue))
} else {
option.selected = looseIndexOf(value, optionValue) > -1
}