Skip to content

Commit

Permalink
fix(runtime-dom): fix v-model.number does not work for radio buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
YiMo1 committed May 8, 2024
1 parent 4619461 commit b87e51c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
52 changes: 52 additions & 0 deletions packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1306,4 +1306,56 @@ describe('vModel', () => {

expect(inputNum1.value).toBe('1')
})

// #10886
it('v-model.number should work with radio buttons', async () => {
const component = defineComponent({
data() {
return { value: null }
},
render() {
return [
withVModel(
h('input', {
type: 'radio',
class: 'one',
value: '1',
'onUpdate:modelValue': setValue.bind(this),
}),
this.value,
{
number: true,
},
),
withVModel(
h('input', {
type: 'radio',
class: 'two',
value: '2.2',
'onUpdate:modelValue': setValue.bind(this),
}),
this.value,
{
number: true,
},
),
]
},
})
render(h(component), root)

const one = root.querySelector('.one')
const two = root.querySelector('.two')
const data = root._vnode.component.data

one.checked = true
triggerEvent('change', one)
await nextTick()
expect(data.value).toEqual(1)

two.checked = true
triggerEvent('change', two)
await nextTick()
expect(data.value).toEqual(2.2)
})
})
4 changes: 2 additions & 2 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ function setChecked(
}

export const vModelRadio: ModelDirective<HTMLInputElement> = {
created(el, { value }, vnode) {
created(el, { value, modifiers: { number } }, vnode) {
el.checked = looseEqual(value, vnode.props!.value)
el[assignKey] = getModelAssigner(vnode)
addEventListener(el, 'change', () => {
el[assignKey](getValue(el))
el[assignKey](number ? looseToNumber(getValue(el)) : getValue(el))
})
},
beforeUpdate(el, { value, oldValue }, vnode) {
Expand Down

0 comments on commit b87e51c

Please sign in to comment.