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

feat(runtime-core): allows v-model built in modifiers on component #2348

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
62 changes: 62 additions & 0 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,68 @@ describe('component: emit', () => {
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', () => {
const options = { click: null }
expect(isEmitListener(options, 'onClick')).toBe(true)
Expand Down
26 changes: 22 additions & 4 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
hyphenate,
isArray,
isFunction,
isOn
isOn,
toNumber
} from '@vue/shared'
import {
ComponentInternalInstance,
Expand Down Expand Up @@ -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

Expand All @@ -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}".`
Expand All @@ -76,6 +77,23 @@ export function emit(
}
}

let args = rawArgs
Copy link
Member

Choose a reason for hiding this comment

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

maybe we should use rawArgs directly here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can you explain more on how to use rawArgs here?

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)
}
Expand All @@ -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]
}
Expand Down