Skip to content

Commit 1f524cc

Browse files
authored
Merge pull request #2 from LennardWesterveld/feature/children-not-required-for-static-loader
feat(staticTreeSource): allow undefined children in static tree data …
2 parents fd97a1a + 79a7e6a commit 1f524cc

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Type `TreeSourceNode<T>` contains all the properties of `T` (which is a type tha
8787
If you have the full content of the tree available in an object and you don't need lazy loading, you can use `staticTreeSource(data)` to turn it into a `TreeSource`. Your `data` must be an array of root nodes which conform to `StaticTreeSourceNode<T>`:
8888

8989
* All properties from `TreeSourceNode<T>`
90-
* `children: Array<StaticTreeSourceNode<T>>`
90+
* `children?: Array<StaticTreeSourceNode<T>>`
9191

9292
### interface TreeState
9393
This interface describes the current display state of a tree. It contains two properties:

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)