Skip to content

Commit a9d47d9

Browse files
committed
fix: tree layout quirks
Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
1 parent a1a1ff1 commit a9d47d9

File tree

3 files changed

+3262
-4
lines changed

3 files changed

+3262
-4
lines changed

src/layouts.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,20 @@ const _inPlaceEvenSpacingUpdate = (
128128
const tm = tree.meta
129129
const tmp = tm.pos
130130
if (numChildren === 0) {
131-
tmp.x += shift
131+
tmp.x = M(offsets[depth] ?? 0, tmp.x + shift)
132132
delete tm.m
133133
} else if (numChildren === 1) {
134134
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
135-
tmp.x = _getPosX(tree[C]![0]!)
136-
delete tm.m
135+
const c = _getPosX(tree[C]![0]!)
136+
tmp.x = M(offsets[depth] ?? 0, c + shift)
137+
tm.m = tmp.x - c
137138
} else {
138139
const c = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
139140
(_getPosX(tree[C]![0]!) +
140141
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
141142
_getPosX(tree[C]![numChildren - 1]!)) *
142143
0.5
143-
tmp.x = M(offsets[depth] ?? 0, c)
144+
tmp.x = M(offsets[depth] ?? 0, c + shift)
144145
tm.m = tmp.x - c
145146
}
146147
tracer.mX = M(tracer.mX, tmp.x)
@@ -199,6 +200,13 @@ const _siblingsEvenSpacing = (
199200
node.meta.pos.x += accShift
200201
node.meta.m = (node.meta.m ?? 0) + accShift
201202
}
203+
if (numChildren > 0) {
204+
offsets[depth + 1] = M(
205+
offsets[depth + 1] ?? 0,
206+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
207+
tc![numChildren - 1]!.node.meta.pos.x + 1,
208+
)
209+
}
202210

203211
_inPlaceEvenSpacingUpdate(numChildren, tree, 0, offsets, depth, tracer)
204212
}

src/tests/BeautifulTree.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
wideTree_Bm,
1414
wideTree_C,
1515
wideTree_D,
16+
wideTree_E,
1617
} from '../stories/treeFixtures'
1718
import { describe, expect, it } from 'vitest'
1819
import { render } from '@testing-library/react'
@@ -183,4 +184,24 @@ describe('BeautifulTree : Smart Layout', () => {
183184

184185
expect(rendered).toMatchSnapshot()
185186
})
187+
188+
it('renders wide tree E, with rect shape, node content display, and dynamic css classes', () => {
189+
const rendered = render(
190+
<BeautifulTree
191+
id="smart-wide-e-tree"
192+
svgProps={{
193+
width: 400,
194+
height: 400,
195+
}}
196+
tree={wideTree_E}
197+
computeLayout={computeSmartLayout}
198+
nodeShape="rect"
199+
getNodeContent={(data): string => data?.['v']?.toString() ?? ''}
200+
getNodeClass={getCssFromNodeData}
201+
getEdgeClass={getCssFromEdgeData}
202+
/>,
203+
)
204+
205+
expect(rendered).toMatchSnapshot()
206+
})
186207
})

0 commit comments

Comments
 (0)