fix(v-model): built in modifiers support on component (#2348)

close #2326
This commit is contained in:
Hunter 2020-10-20 21:59:27 +08:00 committed by GitHub
parent 4bbb2b2ee6
commit 128ec460ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 4 deletions

View File

@ -220,6 +220,68 @@ describe('component: emit', () => {
expect(onFooOnce).toHaveBeenCalledTimes(1) expect(onFooOnce).toHaveBeenCalledTimes(1)
}) })
test('.number modifier should work with v-model on component', () => {
const Foo = defineComponent({
render() {},
created() {
this.$emit('update:modelValue', '1')
this.$emit('update:foo', '2')
}
})
const fn1 = jest.fn()
const fn2 = jest.fn()
const Comp = () =>
h(Foo, {
modelValue: null,
modelModifiers: { number: true },
'onUpdate:modelValue': fn1,
foo: null,
fooModifiers: { number: true },
'onUpdate:foo': fn2
})
render(h(Comp), nodeOps.createElement('div'))
expect(fn1).toHaveBeenCalledTimes(1)
expect(fn1).toHaveBeenCalledWith(1)
expect(fn2).toHaveBeenCalledTimes(1)
expect(fn2).toHaveBeenCalledWith(2)
})
test('.trim modifier should work with v-model on component', () => {
const Foo = defineComponent({
render() {},
created() {
this.$emit('update:modelValue', ' one ')
this.$emit('update:foo', ' two ')
}
})
const fn1 = jest.fn()
const fn2 = jest.fn()
const Comp = () =>
h(Foo, {
modelValue: null,
modelModifiers: { trim: true },
'onUpdate:modelValue': fn1,
foo: null,
fooModifiers: { trim: true },
'onUpdate:foo': fn2
})
render(h(Comp), nodeOps.createElement('div'))
expect(fn1).toHaveBeenCalledTimes(1)
expect(fn1).toHaveBeenCalledWith('one')
expect(fn2).toHaveBeenCalledTimes(1)
expect(fn2).toHaveBeenCalledWith('two')
})
test('isEmitListener', () => { test('isEmitListener', () => {
const options = { click: null } const options = { click: null }
expect(isEmitListener(options, 'onClick')).toBe(true) expect(isEmitListener(options, 'onClick')).toBe(true)

View File

@ -7,7 +7,8 @@ import {
hyphenate, hyphenate,
isArray, isArray,
isFunction, isFunction,
isOn isOn,
toNumber
} from '@vue/shared' } from '@vue/shared'
import { import {
ComponentInternalInstance, ComponentInternalInstance,
@ -45,7 +46,7 @@ export type EmitFn<
export function emit( export function emit(
instance: ComponentInternalInstance, instance: ComponentInternalInstance,
event: string, event: string,
...args: any[] ...rawArgs: any[]
) { ) {
const props = instance.vnode.props || EMPTY_OBJ const props = instance.vnode.props || EMPTY_OBJ
@ -65,7 +66,7 @@ export function emit(
} else { } else {
const validator = emitsOptions[event] const validator = emitsOptions[event]
if (isFunction(validator)) { if (isFunction(validator)) {
const isValid = validator(...args) const isValid = validator(...rawArgs)
if (!isValid) { if (!isValid) {
warn( warn(
`Invalid event arguments: event validation failed for event "${event}".` `Invalid event arguments: event validation failed for event "${event}".`
@ -76,6 +77,23 @@ export function emit(
} }
} }
let args = rawArgs
const isModelListener = event.startsWith('update:')
// for v-model update:xxx events, apply modifiers on args
const modelArg = isModelListener && event.slice(7)
if (modelArg && modelArg in props) {
const modifiersKey = `${
modelArg === 'modelValue' ? 'model' : modelArg
}Modifiers`
const { number, trim } = props[modifiersKey] || EMPTY_OBJ
if (trim) {
args = rawArgs.map(a => a.trim())
} else if (number) {
args = rawArgs.map(toNumber)
}
}
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
devtoolsComponentEmit(instance, event, args) devtoolsComponentEmit(instance, event, args)
} }
@ -101,7 +119,7 @@ export function emit(
let handler = props[handlerName] let handler = props[handlerName]
// for v-model update:xxx events, also trigger kebab-case equivalent // for v-model update:xxx events, also trigger kebab-case equivalent
// for props passed via kebab-case // for props passed via kebab-case
if (!handler && event.startsWith('update:')) { if (!handler && isModelListener) {
handlerName = toHandlerKey(hyphenate(event)) handlerName = toHandlerKey(hyphenate(event))
handler = props[handlerName] handler = props[handlerName]
} }