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
2 changed files with 84 additions and 4 deletions

View File

@@ -7,7 +7,8 @@ import {
hyphenate,
isArray,
isFunction,
isOn
isOn,
toNumber
} from '@vue/shared'
import {
ComponentInternalInstance,
@@ -45,7 +46,7 @@ export type EmitFn<
export function emit(
instance: ComponentInternalInstance,
event: string,
...args: any[]
...rawArgs: any[]
) {
const props = instance.vnode.props || EMPTY_OBJ
@@ -65,7 +66,7 @@ export function emit(
} else {
const validator = emitsOptions[event]
if (isFunction(validator)) {
const isValid = validator(...args)
const isValid = validator(...rawArgs)
if (!isValid) {
warn(
`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__) {
devtoolsComponentEmit(instance, event, args)
}
@@ -101,7 +119,7 @@ export function emit(
let handler = props[handlerName]
// for v-model update:xxx events, also trigger kebab-case equivalent
// for props passed via kebab-case
if (!handler && event.startsWith('update:')) {
if (!handler && isModelListener) {
handlerName = toHandlerKey(hyphenate(event))
handler = props[handlerName]
}