File tree Expand file tree Collapse file tree 4 files changed +30
-7
lines changed Expand file tree Collapse file tree 4 files changed +30
-7
lines changed Original file line number Diff line number Diff line change 1- import { isArray , isStructObject } from "../shared/type" ;
1+ import { ShapeFlags } from "../shared/ShapeFlags" ;
2+ import { isArray , isFunction , isStructObject } from "../shared/type" ;
23
34export 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
710export 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 ;
Original file line number Diff line number Diff line change 11import { ShapeFlags } from "../shared/ShapeFlags" ;
2- import { isArray , isString } from "../shared/type" ;
2+ import { isArray , isString , isStructObject } from "../shared/type" ;
33
44export 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}
Original file line number Diff line number Diff line change 1- import { hasOwnProperty } from "../../shared/index" ;
1+ import { hasOwnProperty , isFunction , isStructObject } from "../../shared/index" ;
22import { 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments