Skip to content

Commit b79268d

Browse files
author
lishiwen
committed
is slots children
1 parent 49c5e8f commit b79268d

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

src/runtime-core/componentSlots.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import { isArray, isStructObject } from "../shared/type";
1+
import { ShapeFlags } from "../shared/ShapeFlags";
2+
import { isArray, isFunction, isStructObject } from "../shared/type";
23

34
export function initSlots(instance, instanceChildren) {
4-
instance.slots = normalizeObjectSlots(instanceChildren);
5+
if (instance.vnode.shapeFlag & ShapeFlags.SLOT_CHILDREN) {
6+
instance.slots = normalizeObjectSlots(instanceChildren);
7+
}
58
};
69

710
export function normalizeObjectSlots(instanceChildren) {
811
let slots = {};
912
if (isStructObject(instanceChildren)) {
1013
for (const key in instanceChildren) {
11-
slots[key] = normalizeSlot(instanceChildren[key]);
14+
// NOTE: 这是自己增加到扩展,我需要支持slot无参数的情况
15+
if (isStructObject(instanceChildren[key])) {
16+
slots[key] = (props) => normalizeSlot(instanceChildren[key]);
17+
}
18+
19+
if (isFunction(instanceChildren[key])) {
20+
slots[key] = (props) => normalizeSlot(instanceChildren[key](props));
21+
}
1222
}
1323
};
1424
return slots;

src/runtime-core/createVNode.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ShapeFlags } from "../shared/ShapeFlags";
2-
import { isArray, isString } from "../shared/type";
2+
import { isArray, isString, isStructObject } from "../shared/type";
33

44
export function createVNode(type, props?, children?) {
55
const vnode = {
@@ -28,4 +28,9 @@ function childrenShapeFlag(vNode) {
2828
if (isArray(vNode.children)) {
2929
vNode.shapeFlag |= ShapeFlags.ARRAY_CHILDREN;
3030
}
31+
32+
// slots children
33+
if (vNode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT && isStructObject(vNode.children)) {
34+
vNode.shapeFlag |= ShapeFlags.SLOT_CHILDREN;
35+
}
3136
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { hasOwnProperty } from "../../shared/index";
1+
import { hasOwnProperty, isFunction, isStructObject } from "../../shared/index";
22
import { createVNode } from "../createVNode";
33

4-
export function renderSlots(slots, renderName) {
4+
export function renderSlots(slots, renderName, props = {}) {
55
if (hasOwnProperty(slots, renderName)) {
6-
return createVNode('span', null, slots[renderName]);
6+
if (isFunction(slots[renderName])) {
7+
const realSlot = slots[renderName]?.(props);
8+
return createVNode('span', null, realSlot);
9+
}
10+
// NOTE: 这是自己增加到扩展,我需要支持slot无参数的情况
11+
if (isStructObject(slots[renderName])) {
12+
return createVNode('span', null, slots[renderName]);
13+
}
714
}
815
}

src/shared/ShapeFlags.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export const enum ShapeFlags {
33
STATEFUL_COMPONENT = 1 << 1, // 0010
44
TEXT_CHILDREN = 1 << 2, // 0100
55
ARRAY_CHILDREN = 1 << 3, // 1000
6+
SLOT_CHILDREN = 1 << 4, // 0001 0000
67
}

0 commit comments

Comments
 (0)