@@ -19,6 +19,7 @@ type TreeState = {
19
19
d3 : { translate : Point ; scale : number } ;
20
20
isTransitioning : boolean ;
21
21
isInitialRenderForDataset : boolean ;
22
+ dataKey : string ;
22
23
} ;
23
24
24
25
class Tree extends React . Component < TreeProps , TreeState > {
@@ -54,6 +55,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
54
55
hasInteractiveNodes : false ,
55
56
dimensions : undefined ,
56
57
centeringTransitionDuration : 800 ,
58
+ dataKey : undefined ,
57
59
} ;
58
60
59
61
state : TreeState = {
@@ -62,6 +64,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
62
64
d3 : Tree . calculateD3Geometry ( this . props ) ,
63
65
isTransitioning : false ,
64
66
isInitialRenderForDataset : true ,
67
+ dataKey : this . props . dataKey ,
65
68
} ;
66
69
67
70
private internalState = {
@@ -75,11 +78,14 @@ class Tree extends React.Component<TreeProps, TreeState> {
75
78
static getDerivedStateFromProps ( nextProps : TreeProps , prevState : TreeState ) {
76
79
let derivedState : Partial < TreeState > = null ;
77
80
// Clone new data & assign internal properties if `data` object reference changed.
78
- if ( nextProps . data !== prevState . dataRef ) {
81
+ // If the dataKey was present but didn't change, then we don't need to re-render the tree
82
+ const dataKeyChanged = ! nextProps . dataKey || prevState . dataKey !== nextProps . dataKey ;
83
+ if ( nextProps . data !== prevState . dataRef && dataKeyChanged ) {
79
84
derivedState = {
80
85
dataRef : nextProps . data ,
81
86
data : Tree . assignInternalProperties ( clone ( nextProps . data ) ) ,
82
87
isInitialRenderForDataset : true ,
88
+ dataKey : nextProps . dataKey ,
83
89
} ;
84
90
}
85
91
const d3 = Tree . calculateD3Geometry ( nextProps ) ;
@@ -322,6 +328,23 @@ class Tree extends React.Component<TreeProps, TreeState> {
322
328
}
323
329
} ;
324
330
331
+ handleAddChildrenToNode = ( nodeId : string , childrenData : RawNodeDatum [ ] ) => {
332
+ const data = clone ( this . state . data ) ;
333
+ const matches = this . findNodesById ( nodeId , data , [ ] ) ;
334
+
335
+ if ( matches . length > 0 ) {
336
+ const targetNodeDatum = matches [ 0 ] ;
337
+
338
+ const depth = targetNodeDatum . __rd3t . depth ;
339
+ const formattedChildren = clone ( childrenData ) . map ( ( node : RawNodeDatum ) =>
340
+ Tree . assignInternalProperties ( [ node ] , depth + 1 )
341
+ ) ;
342
+ targetNodeDatum . children . push ( ...formattedChildren . flat ( ) ) ;
343
+
344
+ this . setState ( { data } ) ;
345
+ }
346
+ } ;
347
+
325
348
/**
326
349
* Handles the user-defined `onNodeClick` function.
327
350
*/
@@ -576,6 +599,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
576
599
onNodeClick = { this . handleOnNodeClickCb }
577
600
onNodeMouseOver = { this . handleOnNodeMouseOverCb }
578
601
onNodeMouseOut = { this . handleOnNodeMouseOutCb }
602
+ handleAddChildrenToNode = { this . handleAddChildrenToNode }
579
603
subscriptions = { subscriptions }
580
604
centerNode = { this . centerNode }
581
605
/>
0 commit comments