Skip to content

Commit 76e3ac3

Browse files
authored
fix: 修复多层嵌套flex布局判断 (#345)
* fix: 修复flex布局计算 * chore: 导出computeComponentBBox方法 * fix: 修复多层嵌套flex布局判断
1 parent a7527b4 commit 76e3ac3

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

packages/f-engine/src/canvas/render/computeLayout.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,22 @@ function createNodeTree(vNode: VNode) {
253253
};
254254
}
255255

256+
function hasFlexDisplay(node) {
257+
if (!node) return false;
258+
259+
const { style, children, vNode } = node;
260+
261+
if (vNode?.tag === Shape && style?.display === 'flex') {
262+
return true;
263+
}
264+
265+
if (children && children.length) {
266+
return children.some(hasFlexDisplay);
267+
}
268+
269+
return false;
270+
}
271+
256272
function fillElementLayout(node) {
257273
const { type, style, vNode, children, layout } = node;
258274
const attrs = getShapeAttrs(type, layout);
@@ -300,7 +316,7 @@ function computeComponentBBox(component: Component | VNode, newChildren?: JSX.El
300316
const { canvas } = context;
301317
const nodeTree = renderJSXElement(newChildren, context, updater);
302318

303-
if (nodeTree.style?.display === 'flex') {
319+
if (hasFlexDisplay(nodeTree)) {
304320
computeCSSLayout(nodeTree);
305321
fillElementLayout(nodeTree);
306322
}

packages/f-engine/test/canvas/render/computeComponent.test.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { jsx, Canvas, Component } from '../../../src';
2+
import { getWorkTag } from '../../../src/canvas/workTags';
23
import { computeComponentBBox } from '../../../src/canvas/render';
34
import { createContext, delay } from '@antv/f-test-utils';
45

6+
const FunctionComponent = 0;
7+
const ClassComponent = 1;
8+
59
class View extends Component {
610
willMount(): void {}
711
render() {
@@ -68,6 +72,9 @@ class ViewText extends Component {
6872
}
6973
}
7074

75+
const ViewWithDisplay = (props) => {
76+
return <ViewWithDisplayGroup {...props} />;
77+
};
7178
class GuideGroup extends Component {
7279
bbox = [];
7380

@@ -76,10 +83,16 @@ class GuideGroup extends Component {
7683
const { updater, context } = this;
7784
const childArray = Array.isArray(children) ? children : [children];
7885
childArray.map((child) => {
79-
const component = new child.type(child.props, context, updater);
80-
component.context = context;
86+
const tag = getWorkTag(child.type);
87+
let element = child;
8188

82-
const bbox = computeComponentBBox(this, component.render());
89+
if (tag === FunctionComponent) {
90+
element = child.type(child.props, context, updater);
91+
}
92+
const component = new element.type({ ...element.props }, context, updater);
93+
component.context = context;
94+
const newElement = component.render();
95+
const bbox = computeComponentBBox(this, newElement);
8396
this.bbox.push(bbox);
8497
});
8598
}
@@ -127,6 +140,7 @@ describe('computeComponent', () => {
127140
<Canvas context={context}>
128141
<GuideGroup ref={guideGroupRef}>
129142
<ViewWithDisplayGroup />
143+
<ViewWithDisplay position={[20, 20]} />
130144
</GuideGroup>
131145
</Canvas>
132146
);
@@ -141,6 +155,9 @@ describe('computeComponent', () => {
141155
expect(bbox[0].y).toEqual(0);
142156
expect(bbox[0].width).toBeGreaterThan(20);
143157
expect(bbox[0].height).toBeGreaterThan(10);
158+
159+
expect(bbox[1].x).toEqual(20);
160+
expect(bbox[1].y).toEqual(20);
144161
});
145162

146163
it('View内的group带padding样式', async () => {

0 commit comments

Comments
 (0)