1
- import React , { useRef , useEffect , useContext } from " react" ;
2
- import { select , hierarchy , tree , linkHorizontal } from "d3" ;
1
+ import React , { useRef , useEffect , useContext } from ' react' ;
2
+ import { select , hierarchy , tree , linkHorizontal } from 'd3' ;
3
3
import cloneDeep from 'lodash/cloneDeep' ;
4
- import useResizeObserver from " ./useResizeObserver" ;
4
+ import useResizeObserver from ' ./useResizeObserver' ;
5
5
import StateContext from '../context/context' ;
6
6
7
7
function usePrevious ( value ) {
@@ -12,7 +12,7 @@ function usePrevious(value) {
12
12
return ref . current ;
13
13
}
14
14
15
- function TreeChart ( { data } ) {
15
+ function TreeChart ( { data } ) { // data is components from state - passed in from BottomTabs
16
16
const [ state , dispatch ] = useContext ( StateContext ) ;
17
17
const canvasId = state . canvasFocus . componentId ;
18
18
@@ -32,17 +32,25 @@ function TreeChart({ data }) {
32
32
// loop over array
33
33
for ( let i = 0 ; i < arr . length ; i ++ ) {
34
34
// if element is separator, remove it
35
- if ( arr [ i ] . name === 'separator' ) arr . splice ( i , 1 ) ;
35
+ if ( arr [ i ] . name === 'separator' ) {
36
+ arr . splice ( i , 1 ) ;
37
+ i -= 1 ;
38
+ }
36
39
// if element has a children array and that array has length, recursive call
37
- if ( arr [ i ] . name === 'div' && arr [ i ] . children . length ) removeSeparators ( arr [ i ] . children ) ;
40
+ else if ( ( arr [ i ] . name === 'div' || arr [ i ] . type === 'Component' ) && arr [ i ] . children . length ) {
41
+ // if element is a component, replace it with deep clone of latest version (to update with new HTML elements)
42
+ if ( arr [ i ] . type === 'Component' ) arr [ i ] = cloneDeep ( data . find ( component => component . name === arr [ i ] . name ) ) ;
43
+ removeSeparators ( arr [ i ] . children ) ;
44
+ }
38
45
}
39
46
// return mutated array
40
47
return arr ;
41
48
} ;
42
49
43
50
// create a deep clone of data to avoid mutating the actual children array in removing separators
44
51
const dataDeepClone = cloneDeep ( data ) ;
45
- dataDeepClone . forEach ( ( component ) => {
52
+ // remove separators and update components to current versions
53
+ dataDeepClone . forEach ( component => {
46
54
removeSeparators ( component . children ) ;
47
55
} ) ;
48
56
@@ -53,7 +61,7 @@ function TreeChart({ data }) {
53
61
// but use getBoundingClientRect on initial render
54
62
// (dimensions are null for the first render)
55
63
const { width, height } =
56
- dimensions || wrapperRef . current . getBoundingClientRect ( ) ;
64
+ dimensions || wrapperRef . current . getBoundingClientRect ( ) ;
57
65
// transform hierarchical data
58
66
const root = hierarchy ( dataDeepClone [ canvasId - 1 ] ) ; // pass in clone here instead of data
59
67
const treeLayout = tree ( ) . size ( [ height , width - 125 ] ) ;
0 commit comments