Skip to content

Commit

Permalink
fix(runtime-dom): v-model string/number coercion for multiselect op…
Browse files Browse the repository at this point in the history
…tions (#10576)

Co-authored-by: RicardoErii <‘[email protected]’>
Co-authored-by: yangchangtao <[email protected]>
  • Loading branch information
3 people authored Mar 28, 2024
1 parent 01172fd commit db374e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
32 changes: 32 additions & 0 deletions packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
4 changes: 1 addition & 3 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit db374e5

Please sign in to comment.