Skip to content

Commit 1efdbe6

Browse files
feat(staticTreeSource): allow undefined children in static tree data structure
1 parent fd97a1a commit 1efdbe6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Tree.stories.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ const staticSource = staticTreeSource<Labeled>([
5353
hasChildren: false,
5454
children: [],
5555
},
56+
{
57+
id: 'pinguin',
58+
label: 'Pinguïn',
59+
hasChildren: false,
60+
},
5661
{
5762
id: 'schaap',
5863
label: 'Schaap',

src/static-tree-source.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TreeSource, TreeSourceNode } from './types';
33
export type StaticTreeSourceNode<T> = TreeSourceNode<T & StaticTreeSourceNodeData<T>>;
44

55
interface StaticTreeSourceNodeData<T> {
6-
children: Array<StaticTreeSourceNode<T>>;
6+
children?: Array<StaticTreeSourceNode<T>>;
77
}
88

99
class StaticTreeSource<T> implements TreeSource<T> {
@@ -22,9 +22,11 @@ class StaticTreeSource<T> implements TreeSource<T> {
2222
if (parentId !== null) {
2323
this.childrenData[parentId].push(child);
2424
}
25-
const childTrail = [child, ...trail];
26-
this.trailsData[child.id] = childTrail;
27-
walkTree(child.children, childTrail);
25+
if (child.children) {
26+
const childTrail = [child, ...trail];
27+
this.trailsData[child.id] = childTrail;
28+
walkTree(child.children, childTrail);
29+
}
2830
}
2931
};
3032
walkTree(data, []);

0 commit comments

Comments
 (0)