Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-dom): v-model.number work with select tag #2308

Merged
merged 3 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,43 @@ describe('vModel', () => {
expect(bar.selected).toEqual(true)
})

it('v-model.number should work with select tag', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should include a test case for multiple select also?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.Thanks~😄

const component = defineComponent({
data() {
return { value: null }
},
render() {
return [
withVModel(
h(
'select',
{
value: null,
'onUpdate:modelValue': setValue.bind(this)
},
[h('option', { value: '1' }), h('option', { value: '2' })]
),
this.value,
{
number: true
}
)
]
}
})
render(h(component), root)

const input = root.querySelector('select')
const one = root.querySelector('option[value="1"]')
const data = root._vnode.component.data

one.selected = true
triggerEvent('change', input)
await nextTick()
expect(typeof data.value).toEqual('number')
expect(data.value).toEqual(1)
})

it('should work with composition session', async () => {
const component = defineComponent({
data() {
Expand Down
8 changes: 6 additions & 2 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,15 @@ export const vModelRadio: ModelDirective<HTMLInputElement> = {
}

export const vModelSelect: ModelDirective<HTMLSelectElement> = {
created(el, binding, vnode) {
created(el, { modifiers: { number } }, vnode) {
const castToNumber = number
addEventListener(el, 'change', () => {
const selectedVal = Array.prototype.filter
.call(el.options, (o: HTMLOptionElement) => o.selected)
.map(getValue)
.map((o: HTMLOptionElement) => {
const domValue = getValue(o)
return castToNumber ? toNumber(domValue) : domValue
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe no need to castToNumber? every time in the .map.

const selectedVal = castToNumber ? temp.map(o=>toNumber(getValue(o))) : temp.map(getValue)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐂B.

el._assign(el.multiple ? selectedVal : selectedVal[0])
})
el._assign = getModelAssigner(vnode)
Expand Down