Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit a7ced55

Browse files
committed
feat: dynamic props
1 parent 546319a commit a7ced55

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

packages/runtime-vapor/__tests__/componentEmits.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ describe('component: emit', () => {
3838
expect(onBaz).toHaveBeenCalled()
3939
})
4040

41+
test('trigger dynamic emits', () => {
42+
const { render } = define({
43+
setup(_, { emit }) {
44+
emit('foo')
45+
emit('bar')
46+
emit('!baz')
47+
},
48+
})
49+
const onFoo = vi.fn()
50+
const onBar = vi.fn()
51+
const onBaz = vi.fn()
52+
render(() => ({
53+
onfoo: onFoo,
54+
onBar,
55+
['on!baz']: onBaz,
56+
}))
57+
58+
expect(onFoo).not.toHaveBeenCalled()
59+
expect(onBar).toHaveBeenCalled()
60+
expect(onBaz).toHaveBeenCalled()
61+
})
62+
4163
test('trigger camelCase handler', () => {
4264
const { render } = define({
4365
setup(_, { emit }) {

packages/runtime-vapor/src/apiCreateVaporApp.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isObject } from '@vue/shared'
1+
import { isFunction, isObject } from '@vue/shared'
22
import {
33
type Component,
44
type ComponentInternalInstance,
@@ -14,8 +14,9 @@ export function createVaporApp(
1414
rootComponent: Component,
1515
rootProps: RawProps | null = null,
1616
): App {
17-
if (rootProps != null && !isObject(rootProps)) {
18-
__DEV__ && warn(`root props passed to app.mount() must be an object.`)
17+
if (rootProps != null && !isObject(rootProps) && !isFunction(rootProps)) {
18+
__DEV__ &&
19+
warn(`root props passed to app.mount() must be an object or function.`)
1920
rootProps = null
2021
}
2122

packages/runtime-vapor/src/componentEmits.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@vue/shared'
1414
import type { Component, ComponentInternalInstance } from './component'
1515
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
16-
import type { StaticProps } from './componentProps'
16+
import { type StaticProps, getDynamicPropValue } from './componentProps'
1717
import { warn } from './warning'
1818

1919
export type ObjectEmitsOptions = Record<
@@ -86,12 +86,7 @@ export function emit(
8686

8787
// has v-bind or :[eventName]
8888
if (hasDynamicProps) {
89-
// TODO
90-
// getDynamicPropValue(rawProps, 'key')
91-
// for (const key in rawProps) {
92-
// const getter = () =>
93-
// registerProp(instance, props, key, getter, true)
94-
// }
89+
tryGet(key => getDynamicPropValue(rawProps, key)[0])
9590
} else {
9691
const staticProps = rawProps[0] as StaticProps
9792
tryGet(key => staticProps[key] && staticProps[key]())

packages/runtime-vapor/src/componentProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export type NormalizedPropsOptions =
7575
export type StaticProps = Record<string, () => unknown>
7676
type DynamicProps = () => Data
7777
export type NormalizedRawProps = Array<StaticProps | DynamicProps>
78-
export type RawProps = NormalizedRawProps | StaticProps | null
78+
export type RawProps = NormalizedRawProps | StaticProps | DynamicProps | null
7979

8080
export function initProps(
8181
instance: ComponentInternalInstance,

0 commit comments

Comments
 (0)