Skip to content

Commit d690331

Browse files
committed
chore: merge refactoring of component
1 parent e3bfd50 commit d690331

File tree

4 files changed

+58
-60
lines changed

4 files changed

+58
-60
lines changed

packages/runtime-vapor/src/apiCreateComponent.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import {
55
} from './component'
66
import { setupComponent } from './apiRender'
77
import type { RawProps } from './componentProps'
8+
import type { Slots } from './componentSlots'
89

9-
export function createComponent(comp: Component, rawProps: RawProps = null) {
10+
export function createComponent(
11+
comp: Component,
12+
rawProps: RawProps = null,
13+
slots: Slots | null = null,
14+
) {
1015
const current = currentInstance!
11-
const instance = createComponentInstance(comp, rawProps)
16+
const instance = createComponentInstance(comp, rawProps, slots)
1217
setupComponent(instance)
1318

1419
// register sub-component with current component for lifecycle management

packages/runtime-vapor/src/component.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
emit,
1818
normalizeEmitsOptions,
1919
} from './componentEmits'
20-
import type { InternalSlots } from './componentSlots'
20+
import { type InternalSlots, type Slots, initSlots } from './componentSlots'
2121
import { VaporLifecycleHooks } from './apiLifecycle'
2222
import type { Data } from '@vue/shared'
2323

@@ -142,6 +142,7 @@ let uid = 0
142142
export function createComponentInstance(
143143
component: ObjectComponent | FunctionalComponent,
144144
rawProps: RawProps | null,
145+
slots: Slots | null,
145146
): ComponentInternalInstance {
146147
const instance: ComponentInternalInstance = {
147148
uid: uid++,
@@ -228,7 +229,7 @@ export function createComponentInstance(
228229
// [VaporLifecycleHooks.SERVER_PREFETCH]: null,
229230
}
230231
initProps(instance, rawProps, !isFunction(component))
231-
// TODO: initSlots(instance, slots)
232+
initSlots(instance, slots)
232233
instance.emit = emit.bind(null, instance)
233234

234235
return instance

packages/runtime-vapor/src/componentSlots.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { IfAny } from '@vue/shared'
2-
import type { Block } from './render'
32
import type { ComponentInternalInstance } from './component'
3+
import type { Block } from './apiRender'
44

55
export type Slot<T extends any = any> = (
66
...args: IfAny<T, any[], [T] | (T extends undefined ? [] : never)>
@@ -14,8 +14,9 @@ export type Slots = Readonly<InternalSlots>
1414

1515
export const initSlots = (
1616
instance: ComponentInternalInstance,
17-
slots: Slots,
17+
slots: Slots | null,
1818
) => {
19+
if (!slots) slots = {}
1920
// TODO: normalize?
2021
instance.slots = slots
2122
}

playground/src/slots.js

+45-54
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @ts-check
22
import {
33
children,
4-
createSlot,
4+
createComponent,
5+
createSlots,
56
defineComponent,
67
getCurrentInstance,
78
insert,
89
on,
910
ref,
10-
render as renderComponent,
1111
renderEffect,
1212
setText,
1313
template,
@@ -27,29 +27,31 @@ const t1 = template(
2727

2828
const Parent = defineComponent({
2929
vapor: true,
30-
props: undefined,
31-
setup(props) {},
32-
render(_ctx) {
33-
const n0 = /** @type {any} */ (t0())
34-
const s0 = createSlot({
35-
mySlot: scope => {
36-
const n1 = t1()
37-
const n2 = /** @type {any} */ (children(n1, 0))
38-
const n3 = /** @type {any} */ (children(n1, 1))
39-
renderEffect(() => {
40-
setText(n2, scope.message)
41-
})
42-
on(n3, 'click', scope.changeMessage)
43-
return [n1]
44-
},
45-
// e.g. default slot
46-
// default: () => {
47-
// const n1 = t1()
48-
// return [n1]
49-
// }
50-
})
51-
renderComponent(Child, {}, s0, n0)
52-
return n0
30+
31+
setup(props) {
32+
return (() => {
33+
const n0 = /** @type {any} */ (t0())
34+
const s0 = createSlots({
35+
mySlot: scope => {
36+
const n1 = t1()
37+
const n2 = /** @type {any} */ (children(n1, 0))
38+
const n3 = /** @type {any} */ (children(n1, 1))
39+
renderEffect(() => {
40+
setText(n2, scope.message())
41+
})
42+
on(n3, 'click', scope.changeMessage)
43+
return [n1]
44+
},
45+
// e.g. default slot
46+
// default: () => {
47+
// const n1 = t1()
48+
// return [n1]
49+
// }
50+
})
51+
/** @type {any} */
52+
const n1 = createComponent(Child, {}, s0)
53+
return [n0, n1]
54+
})()
5355
},
5456
})
5557

@@ -59,41 +61,30 @@ const t2 = template(
5961

6062
const Child = defineComponent({
6163
vapor: true,
62-
props: undefined,
63-
setup(props, { expose: __expose }) {
64+
setup(_, { expose: __expose }) {
6465
__expose()
6566
const message = ref('Hello World!')
6667
function changeMessage() {
6768
message.value += '!'
6869
}
69-
const __returned__ = { message, changeMessage }
70-
Object.defineProperty(__returned__, '__isScriptSetup', {
71-
enumerable: false,
72-
value: true,
73-
})
74-
return __returned__
75-
},
76-
render(_ctx) {
77-
const instance = /** @type {any} */ (getCurrentInstance())
78-
const { slots } = instance
7970

80-
// <div>
81-
// <slot name="mySlot" :message="msg" :changeMessage="changeMessage" />
82-
// <button @click="changeMessage">button in child</button>
83-
// </div>
84-
const n0 = /** @type {any} */ (t2())
85-
const n1 = /** @type {any} */ (children(n0, 0))
86-
on(n1, 'click', _ctx.changeMessage)
87-
const s0 = slots.mySlot({
88-
get message() {
89-
return _ctx.message
90-
},
91-
get changeMessage() {
92-
return _ctx.changeMessage
93-
},
94-
})
95-
insert(s0, n0, n1)
96-
return n0
71+
return (() => {
72+
const instance = /** @type {any} */ (getCurrentInstance())
73+
const { slots } = instance
74+
// <div>
75+
// <slot name="mySlot" :message="msg" :changeMessage="changeMessage" />
76+
// <button @click="changeMessage">button in child</button>
77+
// </div>
78+
const n0 = /** @type {any} */ (t2())
79+
const n1 = /** @type {any} */ (children(n0, 0))
80+
on(n1, 'click', () => changeMessage)
81+
const s0 = slots.mySlot({
82+
message: () => message.value,
83+
changeMessage: () => changeMessage,
84+
})
85+
insert(s0, n0, n1)
86+
return n0
87+
})()
9788
},
9889
})
9990

0 commit comments

Comments
 (0)