feat(renderer-core): optimize the judgment of whether leaf hoc has children

This commit is contained in:
liujuping 2023-05-31 18:01:00 +08:00 committed by 林熠
parent 0e90ea81bb
commit f10b694c42
2 changed files with 43 additions and 9 deletions

View File

@ -521,16 +521,11 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
}
get hasChildren(): boolean {
let { children } = this.props;
if (this.state.childrenInState) {
children = this.state.nodeChildren;
if (!this.state.childrenInState) {
return 'children' in this.props;
}
if (Array.isArray(children)) {
return Boolean(children && children.length);
}
return Boolean(children);
return true;
}
get children(): any {
@ -576,7 +571,11 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
delete compProps.__inner__;
return engine.createElement(Comp, compProps, this.hasChildren ? this.children : null);
if (this.hasChildren) {
return engine.createElement(Comp, compProps, this.children);
}
return engine.createElement(Comp, compProps);
}
}

View File

@ -504,6 +504,41 @@ describe('onChildrenChange', () => {
DivNode.emitChildrenChange();
makeSnapshot(component);
});
it('children is 0', () => {
DivNode.schema.children = 0
DivNode.emitChildrenChange();
const componentInstance = component.root;
expect(componentInstance.findByType(components.Div).props.children).toEqual(0);
});
it('children is false', () => {
DivNode.schema.children = false
DivNode.emitChildrenChange();
const componentInstance = component.root;
expect(componentInstance.findByType(components.Div).props.children).toEqual(false);
});
it('children is []', () => {
DivNode.schema.children = []
DivNode.emitChildrenChange();
const componentInstance = component.root;
expect(componentInstance.findByType(components.Div).props.children).toEqual([]);
});
it('children is null', () => {
DivNode.schema.children = null
DivNode.emitChildrenChange();
const componentInstance = component.root;
expect(componentInstance.findByType(components.Div).props.children).toEqual(null);
});
it('children is undefined', () => {
DivNode.schema.children = undefined;
DivNode.emitChildrenChange();
const componentInstance = component.root;
expect(componentInstance.findByType(components.Div).props.children).toEqual(undefined);
});
});
describe('not render leaf', () => {