Skip to content

Commit a2b319d

Browse files
committed
fix: arithmetic operations on undefined
Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
1 parent f94802e commit a2b319d

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

src/BeautifulTree.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { edgesIterator, postOrderIterator } from './traversal'
2+
import { Fragment } from 'react'
23
import type { Tree } from './types'
34
import type { WrappedTreeWithLayout } from './layouts'
45
export { computeNaiveLayout, computeSmartLayout } from './layouts'
@@ -107,10 +108,10 @@ export function BeautifulTree({
107108
}${runClassesGetter(getNodeClass, node.data)}`
108109

109110
return (
110-
<>
111+
<Fragment key={`${id}-node-w-${idx}`}>
111112
{_nodeShape === 'rect' ? (
112113
<rect
113-
key={`${id}-node-${idx}`}
114+
key={`${id}-node-r-${idx}`}
114115
className={`beautiful-tree-node${
115116
nm.isRoot ? ' beautiful-tree-root' : ''
116117
}${nm.isLeaf ? ' beautiful-tree-leaf' : ''}${runClassesGetter(
@@ -124,7 +125,7 @@ export function BeautifulTree({
124125
/>
125126
) : (
126127
<circle
127-
key={`${id}-node-${idx}`}
128+
key={`${id}-node-c-${idx}`}
128129
className={`beautiful-tree-node${_nodeClass}`}
129130
cx={(nm.pos.x + 1) * xCoef}
130131
cy={(nm.pos.y + 1) * yCoef}
@@ -133,18 +134,21 @@ export function BeautifulTree({
133134
)}
134135
{getNodeContent ? (
135136
<foreignObject
136-
key={`${id}-node-content-${idx}`}
137+
key={`${id}-node-fo-${idx}`}
137138
x={(nm.pos.x + 1) * xCoef - widthCenterShift}
138139
y={(nm.pos.y + 1) * yCoef - heightCenterShift}
139140
width={maxNodeWidth}
140141
height={maxNodeHeight}
141142
>
142-
<div className={`beautiful-tree-node-content${_nodeClass}`}>
143+
<div
144+
key={`${id}-node-div-${idx}`}
145+
className={`beautiful-tree-node-content${_nodeClass}`}
146+
>
143147
{getNodeContent(node.data)}
144148
</div>
145149
</foreignObject>
146150
) : undefined}
147-
</>
151+
</Fragment>
148152
)
149153
})}
150154
</svg>

src/layouts.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ const _addMods = (
5757
): void => {
5858
meta.pos.x += modsum
5959
tracer.mX = M(tracer.mX, meta.pos.x)
60-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61-
modsum += meta.m! // We know it's defined because we control when it's called
60+
modsum += meta.m ?? 0
6261
for (const child of children ?? []) {
6362
_addMods(child.node, modsum, tracer)
6463
}
64+
delete meta.m
6565
}
6666

6767
const _getPosX = (v: { readonly node: Readonly<TreeWithLayout> }): number => {
@@ -129,18 +129,20 @@ const _inPlaceEvenSpacingUpdate = (
129129
const tmp = tm.pos
130130
if (numChildren === 0) {
131131
tmp.x += shift
132+
delete tm.m
132133
} else if (numChildren === 1) {
133134
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
134135
tmp.x = _getPosX(tree[C]![0]!)
136+
delete tm.m
135137
} else {
136138
const c = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
137139
(_getPosX(tree[C]![0]!) +
138140
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
139141
_getPosX(tree[C]![numChildren - 1]!)) *
140142
0.5
141143
tmp.x = M(offsets[depth] ?? 0, c)
144+
tm.m = tmp.x - c
142145
}
143-
delete tm.m
144146
tracer.mX = M(tracer.mX, tmp.x)
145147
offsets[depth] = 1 + tmp.x
146148
}
@@ -251,7 +253,10 @@ export const computeSmartLayout = (
251253
_addMods(t, 0, tracer)
252254

253255
_cousinsEvenSpacing(t, [], tracer)
256+
_addMods(t, 0, tracer)
257+
254258
_siblingsEvenSpacing(t, [], tracer)
259+
_addMods(t, 0, tracer)
255260

256261
return {
257262
tree: t,

src/stories/BeautifulTree.stories.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,34 @@ const wideTree_D: Tree = {
288288
],
289289
}
290290

291+
const wideTree_E: Tree = {
292+
children: [
293+
{
294+
node: {
295+
children: [
296+
{ node: {} },
297+
{
298+
node: {
299+
children: [
300+
{ node: {} },
301+
{ node: { children: [{ node: {} }, { node: {} }] } },
302+
{
303+
node: {
304+
children: [{ node: {} }, { node: {} }, { node: {} }],
305+
},
306+
},
307+
],
308+
},
309+
},
310+
{ node: {} },
311+
],
312+
},
313+
},
314+
{ node: { children: [{ node: {} }] } },
315+
{ node: {} },
316+
],
317+
}
318+
291319
const getCssFromNodeData = (
292320
data?: Readonly<Record<string, unknown>>,
293321
): string[] => {
@@ -423,6 +451,20 @@ export const Centered3_Wide_Tree_D: Story = {
423451
},
424452
}
425453

454+
export const Centered3_Wide_Tree_E: Story = {
455+
args: {
456+
id: 'centered3-wide-tree-e',
457+
svgProps: {
458+
width: 400,
459+
height: 400,
460+
},
461+
tree: wideTree_E,
462+
computeLayout: computeSmartLayout,
463+
getNodeClass: getCssFromNodeData,
464+
getEdgeClass: getCssFromEdgeData,
465+
},
466+
}
467+
426468
const buildRandomTree = (maxDepth = 4): Readonly<Tree> => {
427469
const numChildren = maxDepth > 0 ? Math.floor(Math.random() * 4) : 0
428470
const tree: Tree = {

0 commit comments

Comments
 (0)