@@ -8,12 +8,12 @@ import './style.css';
88export default class Node extends React . Component {
99 constructor ( props ) {
1010 super ( props ) ;
11- const { parent } = props . nodeData ;
11+ const { nodeData : { parent } , orientation } = props ;
1212 const originX = parent ? parent . x : 0 ;
1313 const originY = parent ? parent . y : 0 ;
1414
1515 this . state = {
16- transform : this . setTransformOrientation ( originX , originY ) ,
16+ transform : this . setTransformOrientation ( originX , originY , orientation ) ,
1717 initialStyle : {
1818 opacity : 0 ,
1919 } ,
@@ -23,48 +23,73 @@ export default class Node extends React.Component {
2323 }
2424
2525 componentDidMount ( ) {
26- const { x, y } = this . props . nodeData ;
27- const transform = this . setTransformOrientation ( x , y ) ;
26+ const { nodeData : { x, y } , orientation , transitionDuration } = this . props ;
27+ const transform = this . setTransformOrientation ( x , y , orientation ) ;
2828
29- this . applyTransform ( transform ) ;
29+ this . applyTransform ( transform , transitionDuration ) ;
3030 }
3131
3232 componentWillUpdate ( nextProps ) {
33- const transform = this . setTransformOrientation (
34- nextProps . nodeData . x ,
35- nextProps . nodeData . y ,
33+ const shouldTransform = this . shouldNodeTransform ( this . props , nextProps ) ;
34+ if ( shouldTransform ) {
35+ const transform = this . setTransformOrientation (
36+ nextProps . nodeData . x ,
37+ nextProps . nodeData . y ,
38+ nextProps . orientation ,
39+ ) ;
40+ this . applyTransform ( transform , nextProps . transitionDuration ) ;
41+ }
42+ }
43+
44+ shouldNodeTransform ( ownProps , nextProps ) {
45+ return (
46+ nextProps . nodeData . x !== ownProps . nodeData . x ||
47+ nextProps . nodeData . y !== ownProps . nodeData . y ||
48+ nextProps . orientation !== ownProps . orientation
3649 ) ;
37- this . applyTransform ( transform ) ;
3850 }
3951
40- setTransformOrientation ( x , y ) {
41- return this . props . orientation === 'horizontal'
52+ setTransformOrientation ( x , y , orientation ) {
53+ return orientation === 'horizontal'
4254 ? `translate(${ y } ,${ x } )`
4355 : `translate(${ x } ,${ y } )` ;
4456 }
4557
46- applyTransform ( transform , opacity = 1 , done = ( ) => { } ) {
47- const { transitionDuration } = this . props ;
48-
49- select ( this . node )
50- . transition ( )
51- . duration ( transitionDuration )
52- . attr ( 'transform' , transform )
53- . style ( 'opacity' , opacity )
54- . each ( 'end' , done ) ;
58+ applyTransform ( transform , transitionDuration , opacity = 1 , done = ( ) => { } ) {
59+ if ( transitionDuration === 0 ) {
60+ select ( this . node )
61+ . attr ( 'transform' , transform )
62+ . style ( 'opacity' , opacity ) ;
63+ done ( ) ;
64+ } else {
65+ select ( this . node )
66+ . transition ( )
67+ . duration ( transitionDuration )
68+ . attr ( 'transform' , transform )
69+ . style ( 'opacity' , opacity )
70+ . each ( 'end' , done ) ;
71+ }
5572 }
5673
5774 handleClick ( ) {
5875 this . props . onClick ( this . props . nodeData . id ) ;
5976 }
6077
6178 componentWillLeave ( done ) {
62- const { parent } = this . props . nodeData ;
79+ const {
80+ nodeData : { parent } ,
81+ orientation,
82+ transitionDuration,
83+ } = this . props ;
6384 const originX = parent ? parent . x : 0 ;
6485 const originY = parent ? parent . y : 0 ;
65- const transform = this . setTransformOrientation ( originX , originY ) ;
86+ const transform = this . setTransformOrientation (
87+ originX ,
88+ originY ,
89+ orientation ,
90+ ) ;
6691
67- this . applyTransform ( transform , 0 , done ) ;
92+ this . applyTransform ( transform , transitionDuration , 0 , done ) ;
6893 }
6994
7095 render ( ) {
0 commit comments