Skip to content

fix(runtime-vapor): component emits vdom interop #13498

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

Open
wants to merge 1 commit into
base: vapor
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion packages/runtime-vapor/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
// ./rendererAttrsFallthrough.spec.ts.

import {
createApp,
h,
isEmitListener,
nextTick,
onBeforeUnmount,
toHandlers,
} from '@vue/runtime-dom'
import { createComponent, defineVaporComponent } from '../src'
import {
createComponent,
defineVaporComponent,
vaporInteropPlugin,
} from '../src'
import { makeRender } from './_utils'

const define = makeRender()
Expand Down Expand Up @@ -425,3 +431,28 @@ describe('component: emit', () => {
expect(fn).not.toHaveBeenCalled()
})
})

describe('vdom interop', () => {
test('vdom parent > vapor child', () => {
const VaporChild = defineVaporComponent({
emits: ['click'],
setup(_, { emit }) {
emit('click')
return []
},
})

const fn = vi.fn()
const App = {
setup() {
return () => h(VaporChild as any, { onClick: fn })
},
}

const root = document.createElement('div')
createApp(App).use(vaporInteropPlugin).mount(root)

// fn should be called once
expect(fn).toHaveBeenCalledTimes(1)
})
})
6 changes: 5 additions & 1 deletion packages/runtime-vapor/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ function propGetter(rawProps: Record<string, any>, key: string) {
let i = dynamicSources.length
while (i--) {
const source = resolveSource(dynamicSources[i])
if (hasOwn(source, key)) return resolveSource(source[key])
if (hasOwn(source, key))
// for props passed from VDOM component, no need to resolve
return dynamicSources.__interop
? source[key]
: resolveSource(source[key])
}
}
return rawProps[key] && resolveSource(rawProps[key])
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-vapor/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { renderEffect } from './renderEffect'

export type RawProps = Record<string, () => unknown> & {
// generated by compiler for :[key]="x" or v-bind="x"
$?: DynamicPropsSource[]
$?: DynamicPropsSource[] & { __interop?: boolean }
}

export type DynamicPropsSource =
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-vapor/src/vdomInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const vaporInteropImpl: Omit<
const instance = (vnode.component = createComponent(
vnode.type as any as VaporComponent,
{
$: [() => propsRef.value],
$: extend([() => propsRef.value], { __interop: true }),
Copy link
Member Author

Choose a reason for hiding this comment

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

Add __interop flag to the $ to mark props that are passed from vdom component

} as RawProps,
{
_: slotsRef, // pass the slots ref
Expand Down