Skip to content

Commit e01f010

Browse files
committed
feat(runtime-vapor): functional component props
1 parent e9b7388 commit e01f010

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

Diff for: packages/runtime-vapor/__tests__/_utils.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Data, isFunction } from '@vue/shared'
1+
import type { Data } from '@vue/shared'
22
import {
33
type ComponentInternalInstance,
44
type ObjectComponent,
@@ -24,13 +24,7 @@ export function makeRender<Component = ObjectComponent | SetupFn>(
2424
})
2525

2626
const define = (comp: Component) => {
27-
const component = defineComponent(
28-
isFunction(comp)
29-
? {
30-
setup: comp,
31-
}
32-
: comp,
33-
)
27+
const component = defineComponent(comp)
3428
let instance: ComponentInternalInstance
3529
const render = (
3630
props: Data = {},

Diff for: packages/runtime-vapor/__tests__/componentProps.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe('component props (vapor)', () => {
143143
})
144144

145145
// FIXME:
146-
test.todo('functional without declaration', () => {
146+
test('functional without declaration', () => {
147147
let props: any
148148
let attrs: any
149149

Diff for: packages/runtime-vapor/src/componentProps.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export type NormalizedPropsOptions = [NormalizedProps, string[]] | []
7575
export function initProps(
7676
instance: ComponentInternalInstance,
7777
rawProps: Data | null,
78+
isStateful: boolean,
7879
) {
7980
const props: Data = {}
8081
const attrs: Data = {}
@@ -164,7 +165,15 @@ export function initProps(
164165
validateProps(rawProps || {}, props, instance)
165166
}
166167

167-
instance.props = shallowReactive(props)
168+
if (isStateful) {
169+
instance.props = shallowReactive(props)
170+
} else {
171+
if (instance.propsOptions === EMPTY_ARR) {
172+
instance.props = attrs
173+
} else {
174+
instance.props = props
175+
}
176+
}
168177
instance.attrs = attrs
169178

170179
return hasAttrsChanged

Diff for: packages/runtime-vapor/src/render.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { proxyRefs } from '@vue/reactivity'
2-
import { type Data, invokeArrayFns, isArray, isObject } from '@vue/shared'
2+
import {
3+
type Data,
4+
invokeArrayFns,
5+
isArray,
6+
isFunction,
7+
isObject,
8+
} from '@vue/shared'
39
import {
410
type Component,
511
type ComponentInternalInstance,
@@ -28,7 +34,7 @@ export function render(
2834
container: string | ParentNode,
2935
): ComponentInternalInstance {
3036
const instance = createComponentInstance(comp, props)
31-
initProps(instance, props)
37+
initProps(instance, props, !isFunction(instance.component))
3238
return mountComponent(instance, (container = normalizeContainer(container)))
3339
}
3440

@@ -49,8 +55,7 @@ export function mountComponent(
4955
const { component, props, emit, attrs } = instance
5056
const ctx = { expose: () => {}, emit, attrs }
5157

52-
const setupFn =
53-
typeof component === 'function' ? component : component.setup
58+
const setupFn = isFunction(component) ? component : component.setup
5459
const stateOrNode = setupFn && setupFn(props, ctx)
5560

5661
let block: Block | undefined

0 commit comments

Comments
 (0)