Skip to content

Commit 7fb6eb8

Browse files
skirtles-codezhaozhongyuyyx990803
authored
fix(v-model): component v-model modifiers trim and number when cases don't match (#9609)
close #4848 close #4850 (based on commits from #4850) Co-authored-by: zhaozhongyu <zhaozhongyu@xunlei.com> Co-authored-by: Evan You <evan@vuejs.org>
1 parent 05779a7 commit 7fb6eb8

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

packages/runtime-core/__tests__/componentEmits.spec.ts

+77
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,83 @@ describe('component: emit', () => {
356356
expect(fn2).toHaveBeenCalledWith('two')
357357
})
358358

359+
test('.trim modifier should work with v-model on component for kebab-cased props and camelCased emit', () => {
360+
const Foo = defineComponent({
361+
render() {},
362+
created() {
363+
this.$emit('update:firstName', ' one ')
364+
},
365+
})
366+
367+
const fn1 = vi.fn()
368+
369+
const Comp = () =>
370+
h(Foo, {
371+
'first-name': null,
372+
'first-nameModifiers': { trim: true },
373+
'onUpdate:first-name': fn1,
374+
})
375+
376+
render(h(Comp), nodeOps.createElement('div'))
377+
378+
expect(fn1).toHaveBeenCalledTimes(1)
379+
expect(fn1).toHaveBeenCalledWith('one')
380+
})
381+
382+
test('.trim modifier should work with v-model on component for camelCased props and kebab-cased emit', () => {
383+
const Foo = defineComponent({
384+
render() {},
385+
created() {
386+
this.$emit('update:model-value', ' one ')
387+
this.$emit('update:first-name', ' two ')
388+
},
389+
})
390+
391+
const fn1 = vi.fn()
392+
const fn2 = vi.fn()
393+
394+
const Comp = () =>
395+
h(Foo, {
396+
modelValue: null,
397+
modelModifiers: { trim: true },
398+
'onUpdate:modelValue': fn1,
399+
400+
firstName: null,
401+
firstNameModifiers: { trim: true },
402+
'onUpdate:firstName': fn2,
403+
})
404+
405+
render(h(Comp), nodeOps.createElement('div'))
406+
407+
expect(fn1).toHaveBeenCalledTimes(1)
408+
expect(fn1).toHaveBeenCalledWith('one')
409+
expect(fn2).toHaveBeenCalledTimes(1)
410+
expect(fn2).toHaveBeenCalledWith('two')
411+
})
412+
413+
test('.trim modifier should work with v-model on component for mixed cased props and emit', () => {
414+
const Foo = defineComponent({
415+
render() {},
416+
created() {
417+
this.$emit('update:base-URL', ' one ')
418+
},
419+
})
420+
421+
const fn1 = vi.fn()
422+
423+
const Comp = () =>
424+
h(Foo, {
425+
'base-URL': null,
426+
'base-URLModifiers': { trim: true },
427+
'onUpdate:base-URL': fn1,
428+
})
429+
430+
render(h(Comp), nodeOps.createElement('div'))
431+
432+
expect(fn1).toHaveBeenCalledTimes(1)
433+
expect(fn1).toHaveBeenCalledWith('one')
434+
})
435+
359436
test('.trim and .number modifiers should work with v-model on component', () => {
360437
const Foo = defineComponent({
361438
render() {},

packages/runtime-core/src/helpers/useModel.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,10 @@ export function useModel(
108108
export const getModelModifiers = (
109109
props: Record<string, any>,
110110
modelName: string,
111-
): Record<string, boolean> | undefined =>
112-
props[`${modelName === 'modelValue' ? 'model' : modelName}Modifiers`]
111+
): Record<string, boolean> | undefined => {
112+
return modelName === 'modelValue' || modelName === 'model-value'
113+
? props.modelModifiers
114+
: props[`${modelName}Modifiers`] ||
115+
props[`${camelize(modelName)}Modifiers`] ||
116+
props[`${hyphenate(modelName)}Modifiers`]
117+
}

0 commit comments

Comments
 (0)