Skip to content

Commit d006e39

Browse files
committed
docs: Add more comments to the import/export example source
1 parent aee3ae7 commit d006e39

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Diff for: examples/storybooks/tree-data-io.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,31 @@ const initialData = [
1111
{ id: '4', name: 'N4', parent: 3 },
1212
];
1313

14-
const getNodeKey = ({ node }) => node.id;
15-
1614
export default class App extends Component {
1715
constructor(props) {
1816
super(props);
1917

2018
this.state = {
2119
treeData: getTreeFromFlatData({
2220
flatData: initialData.map(node => ({ ...node, title: node.name })),
23-
getKey: node => node.id,
24-
getParentKey: node => node.parent,
25-
rootKey: null,
21+
getKey: node => node.id, // resolve a node's key
22+
getParentKey: node => node.parent, // resolve a node's parent's key
23+
rootKey: null, // The value of the parent key when there is no parent (i.e., at root level)
2624
}),
2725
};
2826
}
2927

3028
render() {
3129
const flatData = getFlatDataFromTree({
3230
treeData: this.state.treeData,
33-
getNodeKey,
34-
ignoreCollapsed: false,
31+
getNodeKey: ({ node }) => node.id, // This ensures your "id" properties are exported in the path
32+
ignoreCollapsed: false, // Makes sure you traverse every node in the tree, not just the visible ones
3533
}).map(({ node, path }) => ({
3634
id: node.id,
3735
name: node.name,
36+
37+
// The last entry in the path is this node's key
38+
// The second to last entry (accessed here) is the parent node's key
3839
parent: path.length > 1 ? path[path.length - 2] : null,
3940
}));
4041

@@ -45,7 +46,6 @@ export default class App extends Component {
4546
<SortableTree
4647
treeData={this.state.treeData}
4748
onChange={treeData => this.setState({ treeData })}
48-
getNodeKey={getNodeKey}
4949
/>
5050
</div>
5151
<hr />

0 commit comments

Comments
 (0)