Skip to content

Commit

Permalink
fix(runtime-core): vModelSelect type compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoErii committed Mar 23, 2024
1 parent b49306a commit 1a00d6a
Show file tree
Hide file tree
Showing 2 changed files with 39 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)
})
})
10 changes: 7 additions & 3 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,13 @@ 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.find(v => {
let value = optionValue
if (number) {
value = looseToNumber(optionValue)
}
return looseEqual(value, v)
})
} else {
option.selected = looseIndexOf(value, optionValue) > -1
}
Expand Down

0 comments on commit 1a00d6a

Please sign in to comment.