Skip to content

feat(runtime-core): support templateRef added event handler #2184

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

Closed
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
45 changes: 44 additions & 1 deletion packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Note: emits and listener fallthrough is tested in
// ./rendererAttrsFallthrough.spec.ts.

import { render, defineComponent, h, nodeOps } from '@vue/runtime-test'
import { render, defineComponent, ref, h, nodeOps } from '@vue/runtime-test'
import { isEmitListener } from '../src/componentEmits'

describe('component: emit', () => {
Expand All @@ -28,6 +28,49 @@ describe('component: emit', () => {
expect(onBaz).toHaveBeenCalled()
})

test('trigger handlers by templateRef added', () => {
const root = nodeOps.createElement('div')
let innerCtx = {} as any
const CustomButton = defineComponent({
setup(_, ctx) {
innerCtx = ctx
return {
refKey: ref(null)
}
},
emits: ['click'],
render() {
return h('div')
}
})

const button = ref(null)
const Wrapper = defineComponent({
setup(_, ctx) {
innerCtx = ctx
return {
buttonRef: button
}
},
render() {
return h(CustomButton, { ref: 'buttonRef' })
}
})

render(h(Wrapper), root)
const clickHandler = jest.fn()
// @ts-ignore
button.value.onClick = clickHandler
innerCtx.emit('click')
expect(clickHandler).toHaveBeenCalled()

// @ts-ignore
button.value.onclick = clickHandler
expect(
`Attempting to add handler "onclick". but it is neither declared by Component`
).toHaveBeenWarned()
})

// for v-model:foo-bar usage in DOM templates
test('trigger hyphenated events for update:xxx events', () => {
const Foo = defineComponent({
Expand Down
25 changes: 23 additions & 2 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
resolveMergedOptions,
isInBeforeCreate
} from './componentOptions'
import { EmitsOptions, EmitFn } from './componentEmits'
import { EmitsOptions, EmitFn, isEmitListener } from './componentEmits'
import { Slots } from './componentSlots'
import {
currentRenderingInstance,
Expand Down Expand Up @@ -348,7 +348,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
key: string,
value: any
): boolean {
const { data, setupState, ctx } = instance
const { data, setupState, ctx, vnode, emitsOptions } = instance
if (setupState !== EMPTY_OBJ && hasOwn(setupState, key)) {
setupState[key] = value
} else if (data !== EMPTY_OBJ && hasOwn(data, key)) {
Expand Down Expand Up @@ -378,6 +378,27 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
})
} else {
ctx[key] = value
/**
* #2155: allow add event handler by templateRef
* e.g.
* buttonRef.value.onClick = clickHandler
* listen <el-button ref="buttonRef"> component emitted click event
*/
if (key.indexOf('on') === 0) {
if (isEmitListener(emitsOptions, key)) {
/* istanbul ignore else */
if (vnode.props) {
vnode.props[key] = value
}
} else {
__DEV__ &&
warn(
`Attempting to add handler "${key}". ` +
`but it is neither declared by Component`,
instance
)
}
}
}
}
return true
Expand Down