@@ -8,53 +8,55 @@ export default class Node extends React.Component {
8
8
9
9
constructor ( props ) {
10
10
super ( props ) ;
11
+ const { parent } = props . nodeData ;
12
+ const originX = parent ? parent . x : 0 ;
13
+ const originY = parent ? parent . y : 0 ;
14
+
15
+ this . state = {
16
+ transform : this . setTransformOrientation ( originX , originY ) ,
17
+ } ;
18
+
11
19
this . handleClick = this . handleClick . bind ( this ) ;
12
20
}
13
21
14
22
componentDidMount ( ) {
15
- const { x, y, parent } = this . props . nodeData ;
16
- const originX = parent ? parent . x : 0 ;
17
- const originY = parent ? parent . y : 0 ;
23
+ const { x, y } = this . props . nodeData ;
24
+ const transform = this . setTransformOrientation ( x , y ) ;
18
25
19
- select ( this . node )
20
- . attr ( 'transform' , this . setTransformOrientation ( originX , originY ) ) ;
26
+ this . applyTransform ( transform ) ;
27
+ }
21
28
22
- select ( this . node )
23
- . transition ( )
24
- . duration ( 500 )
25
- . attr ( 'transform' , this . setTransformOrientation ( x , y ) ) ;
29
+ componentWillReceiveProps ( nextProps ) {
30
+ if ( nextProps . nodeData . x !== this . props . nodeData . x ||
31
+ nextProps . nodeData . y !== this . props . nodeData . y ) {
32
+ const transform = this . setTransformOrientation ( nextProps . nodeData . x , nextProps . nodeData . y ) ;
33
+ this . applyTransform ( transform ) ;
34
+ }
26
35
}
27
36
28
- // FIXME unstable re-rendering when regressing to parent coordinates
29
37
setTransformOrientation ( x , y ) {
30
38
const { orientation } = this . props ;
31
- let transform ;
32
-
33
- if ( orientation === 'horizontal' ) {
34
- // transform = nodeData.parent !== 'null' ?
35
- // `translate(${nodeData.parent.y},${nodeData.parent.x})` :
36
- // `translate(${nodeData.y},${nodeData.x})`;
37
- transform = `translate(${ y } ,${ x } )` ;
38
- } else {
39
- // transform = nodeData.parent !== 'null' ?
40
- // `translate(${nodeData.parent.x},${nodeData.parent.y})` :
41
- // `translate(${nodeData.x},${nodeData.y})`;
42
- transform = `translate(${ x } ,${ y } )` ;
43
- }
44
-
39
+ const transform = orientation === 'horizontal' ?
40
+ `translate(${ y } ,${ x } )` :
41
+ `translate(${ x } ,${ y } )` ;
45
42
return transform ;
46
43
}
47
44
45
+ applyTransform ( transform , done ) {
46
+ select ( this . node )
47
+ . transition ( )
48
+ . duration ( 500 )
49
+ . attr ( 'transform' , transform )
50
+ . each ( 'end' , done ) ;
51
+ }
52
+
48
53
componentWillLeave ( done ) {
49
54
const { parent } = this . props . nodeData ;
50
55
const originX = parent ? parent . x : 0 ;
51
56
const originY = parent ? parent . y : 0 ;
57
+ const transform = this . setTransformOrientation ( originX , originY ) ;
52
58
53
- select ( this . node )
54
- . transition ( )
55
- . duration ( 500 )
56
- . attr ( 'transform' , this . setTransformOrientation ( originX , originY ) )
57
- . each ( 'end' , done ) ;
59
+ this . applyTransform ( transform , done ) ;
58
60
}
59
61
60
62
handleClick ( ) {
@@ -72,7 +74,7 @@ export default class Node extends React.Component {
72
74
id = { nodeData . id }
73
75
ref = { ( n ) => { this . node = n ; } }
74
76
className = { nodeData . _children ? 'nodeBase' : 'leafNodeBase' }
75
- transform = { this . setTransformOrientation ( nodeData . x , nodeData . y ) }
77
+ transform = { this . state . transform }
76
78
onClick = { this . handleClick }
77
79
>
78
80
< text
0 commit comments