@@ -19,6 +19,7 @@ type TreeState = {
1919 d3 : { translate : Point ; scale : number } ;
2020 isTransitioning : boolean ;
2121 isInitialRenderForDataset : boolean ;
22+ dataKey : string ;
2223} ;
2324
2425class Tree extends React . Component < TreeProps , TreeState > {
@@ -54,6 +55,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
5455 hasInteractiveNodes : false ,
5556 dimensions : undefined ,
5657 centeringTransitionDuration : 800 ,
58+ dataKey : undefined ,
5759 } ;
5860
5961 state : TreeState = {
@@ -62,6 +64,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
6264 d3 : Tree . calculateD3Geometry ( this . props ) ,
6365 isTransitioning : false ,
6466 isInitialRenderForDataset : true ,
67+ dataKey : this . props . dataKey ,
6568 } ;
6669
6770 private internalState = {
@@ -75,11 +78,14 @@ class Tree extends React.Component<TreeProps, TreeState> {
7578 static getDerivedStateFromProps ( nextProps : TreeProps , prevState : TreeState ) {
7679 let derivedState : Partial < TreeState > = null ;
7780 // 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 ) {
7984 derivedState = {
8085 dataRef : nextProps . data ,
8186 data : Tree . assignInternalProperties ( clone ( nextProps . data ) ) ,
8287 isInitialRenderForDataset : true ,
88+ dataKey : nextProps . dataKey ,
8389 } ;
8490 }
8591 const d3 = Tree . calculateD3Geometry ( nextProps ) ;
@@ -322,6 +328,23 @@ class Tree extends React.Component<TreeProps, TreeState> {
322328 }
323329 } ;
324330
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+
325348 /**
326349 * Handles the user-defined `onNodeClick` function.
327350 */
@@ -576,6 +599,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
576599 onNodeClick = { this . handleOnNodeClickCb }
577600 onNodeMouseOver = { this . handleOnNodeMouseOverCb }
578601 onNodeMouseOut = { this . handleOnNodeMouseOutCb }
602+ handleAddChildrenToNode = { this . handleAddChildrenToNode }
579603 subscriptions = { subscriptions }
580604 centerNode = { this . centerNode }
581605 />
0 commit comments