Skip to content

Commit 128ec46

Browse files
authored
fix(v-model): built in modifiers support on component (#2348)
close #2326
1 parent 4bbb2b2 commit 128ec46

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

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

+62
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,68 @@ describe('component: emit', () => {
220220
expect(onFooOnce).toHaveBeenCalledTimes(1)
221221
})
222222

223+
test('.number modifier should work with v-model on component', () => {
224+
const Foo = defineComponent({
225+
render() {},
226+
created() {
227+
this.$emit('update:modelValue', '1')
228+
this.$emit('update:foo', '2')
229+
}
230+
})
231+
232+
const fn1 = jest.fn()
233+
const fn2 = jest.fn()
234+
235+
const Comp = () =>
236+
h(Foo, {
237+
modelValue: null,
238+
modelModifiers: { number: true },
239+
'onUpdate:modelValue': fn1,
240+
241+
foo: null,
242+
fooModifiers: { number: true },
243+
'onUpdate:foo': fn2
244+
})
245+
246+
render(h(Comp), nodeOps.createElement('div'))
247+
248+
expect(fn1).toHaveBeenCalledTimes(1)
249+
expect(fn1).toHaveBeenCalledWith(1)
250+
expect(fn2).toHaveBeenCalledTimes(1)
251+
expect(fn2).toHaveBeenCalledWith(2)
252+
})
253+
254+
test('.trim modifier should work with v-model on component', () => {
255+
const Foo = defineComponent({
256+
render() {},
257+
created() {
258+
this.$emit('update:modelValue', ' one ')
259+
this.$emit('update:foo', ' two ')
260+
}
261+
})
262+
263+
const fn1 = jest.fn()
264+
const fn2 = jest.fn()
265+
266+
const Comp = () =>
267+
h(Foo, {
268+
modelValue: null,
269+
modelModifiers: { trim: true },
270+
'onUpdate:modelValue': fn1,
271+
272+
foo: null,
273+
fooModifiers: { trim: true },
274+
'onUpdate:foo': fn2
275+
})
276+
277+
render(h(Comp), nodeOps.createElement('div'))
278+
279+
expect(fn1).toHaveBeenCalledTimes(1)
280+
expect(fn1).toHaveBeenCalledWith('one')
281+
expect(fn2).toHaveBeenCalledTimes(1)
282+
expect(fn2).toHaveBeenCalledWith('two')
283+
})
284+
223285
test('isEmitListener', () => {
224286
const options = { click: null }
225287
expect(isEmitListener(options, 'onClick')).toBe(true)

packages/runtime-core/src/componentEmits.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
hyphenate,
88
isArray,
99
isFunction,
10-
isOn
10+
isOn,
11+
toNumber
1112
} from '@vue/shared'
1213
import {
1314
ComponentInternalInstance,
@@ -45,7 +46,7 @@ export type EmitFn<
4546
export function emit(
4647
instance: ComponentInternalInstance,
4748
event: string,
48-
...args: any[]
49+
...rawArgs: any[]
4950
) {
5051
const props = instance.vnode.props || EMPTY_OBJ
5152

@@ -65,7 +66,7 @@ export function emit(
6566
} else {
6667
const validator = emitsOptions[event]
6768
if (isFunction(validator)) {
68-
const isValid = validator(...args)
69+
const isValid = validator(...rawArgs)
6970
if (!isValid) {
7071
warn(
7172
`Invalid event arguments: event validation failed for event "${event}".`
@@ -76,6 +77,23 @@ export function emit(
7677
}
7778
}
7879

80+
let args = rawArgs
81+
const isModelListener = event.startsWith('update:')
82+
83+
// for v-model update:xxx events, apply modifiers on args
84+
const modelArg = isModelListener && event.slice(7)
85+
if (modelArg && modelArg in props) {
86+
const modifiersKey = `${
87+
modelArg === 'modelValue' ? 'model' : modelArg
88+
}Modifiers`
89+
const { number, trim } = props[modifiersKey] || EMPTY_OBJ
90+
if (trim) {
91+
args = rawArgs.map(a => a.trim())
92+
} else if (number) {
93+
args = rawArgs.map(toNumber)
94+
}
95+
}
96+
7997
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
8098
devtoolsComponentEmit(instance, event, args)
8199
}
@@ -101,7 +119,7 @@ export function emit(
101119
let handler = props[handlerName]
102120
// for v-model update:xxx events, also trigger kebab-case equivalent
103121
// for props passed via kebab-case
104-
if (!handler && event.startsWith('update:')) {
122+
if (!handler && isModelListener) {
105123
handlerName = toHandlerKey(hyphenate(event))
106124
handler = props[handlerName]
107125
}

0 commit comments

Comments
 (0)