1
1
import React from 'react' ;
2
- import PropTypes from 'prop-types' ;
2
+ import T from 'prop-types' ;
3
3
import { select } from 'd3' ;
4
4
5
- import './style.css' ;
6
5
import SvgTextElement from './SvgTextElement' ;
7
6
import ForeignObjectElement from './ForeignObjectElement' ;
7
+ import './style.css' ;
8
8
9
9
export default class Node extends React . Component {
10
- constructor ( props ) {
11
- super ( props ) ;
12
- const { nodeData : { parent } , orientation } = props ;
13
- const originX = parent ? parent . x : 0 ;
14
- const originY = parent ? parent . y : 0 ;
15
-
16
- this . state = {
17
- transform : this . setTransformOrientation ( originX , originY , orientation ) ,
18
- initialStyle : {
19
- opacity : 0 ,
20
- } ,
21
- } ;
22
-
23
- this . handleClick = this . handleClick . bind ( this ) ;
24
- this . handleOnMouseOver = this . handleOnMouseOver . bind ( this ) ;
25
- this . handleOnMouseOut = this . handleOnMouseOut . bind ( this ) ;
26
- }
10
+ state = {
11
+ transform : this . setTransform ( this . props . nodeData , this . props . orientation , true ) ,
12
+ initialStyle : {
13
+ opacity : 0 ,
14
+ } ,
15
+ } ;
27
16
28
17
componentDidMount ( ) {
29
- const { nodeData : { x , y } , orientation, transitionDuration } = this . props ;
30
- const transform = this . setTransformOrientation ( x , y , orientation ) ;
18
+ const { nodeData, orientation, transitionDuration } = this . props ;
19
+ const transform = this . setTransform ( nodeData , orientation ) ;
31
20
32
21
this . applyTransform ( transform , transitionDuration ) ;
33
22
}
34
23
35
24
componentWillUpdate ( nextProps ) {
36
- const transform = this . setTransformOrientation (
37
- nextProps . nodeData . x ,
38
- nextProps . nodeData . y ,
39
- nextProps . orientation ,
40
- ) ;
25
+ const transform = this . setTransform ( nextProps . nodeData , nextProps . orientation ) ;
41
26
this . applyTransform ( transform , nextProps . transitionDuration ) ;
42
27
}
43
28
44
29
shouldComponentUpdate ( nextProps ) {
45
30
return this . shouldNodeTransform ( this . props , nextProps ) ;
46
31
}
47
32
48
- shouldNodeTransform ( ownProps , nextProps ) {
49
- return (
50
- nextProps . subscriptions !== ownProps . subscriptions ||
51
- nextProps . nodeData . x !== ownProps . nodeData . x ||
52
- nextProps . nodeData . y !== ownProps . nodeData . y ||
53
- nextProps . orientation !== ownProps . orientation
54
- ) ;
55
- }
56
-
57
- setTransformOrientation ( x , y , orientation ) {
33
+ shouldNodeTransform = ( ownProps , nextProps ) =>
34
+ nextProps . subscriptions !== ownProps . subscriptions ||
35
+ nextProps . nodeData . x !== ownProps . nodeData . x ||
36
+ nextProps . nodeData . y !== ownProps . nodeData . y ||
37
+ nextProps . orientation !== ownProps . orientation ;
38
+
39
+ setTransform ( nodeData , orientation , shouldTranslateToOrigin = false ) {
40
+ const { x, y, parent } = nodeData ;
41
+ if ( shouldTranslateToOrigin ) {
42
+ const originX = parent ? parent . x : 0 ;
43
+ const originY = parent ? parent . y : 0 ;
44
+ return orientation === 'horizontal'
45
+ ? `translate(${ originY } ,${ originX } )`
46
+ : `translate(${ originX } ,${ originY } )` ;
47
+ }
58
48
return orientation === 'horizontal' ? `translate(${ y } ,${ x } )` : `translate(${ x } ,${ y } )` ;
59
49
}
60
50
@@ -74,7 +64,7 @@ export default class Node extends React.Component {
74
64
}
75
65
}
76
66
77
- renderNodeElement ( nodeStyle ) {
67
+ renderNodeElement = nodeStyle => {
78
68
const { circleRadius, nodeSvgShape } = this . props ;
79
69
/* TODO: DEPRECATE <circle /> */
80
70
if ( circleRadius ) {
@@ -87,26 +77,23 @@ export default class Node extends React.Component {
87
77
...nodeStyle . circle ,
88
78
...nodeSvgShape . shapeProps ,
89
79
} ) ;
90
- }
80
+ } ;
91
81
92
- handleClick ( evt ) {
82
+ handleClick = evt => {
93
83
this . props . onClick ( this . props . nodeData . id , evt ) ;
94
- }
84
+ } ;
95
85
96
- handleOnMouseOver ( evt ) {
86
+ handleOnMouseOver = evt => {
97
87
this . props . onMouseOver ( this . props . nodeData . id , evt ) ;
98
- }
88
+ } ;
99
89
100
- handleOnMouseOut ( evt ) {
90
+ handleOnMouseOut = evt => {
101
91
this . props . onMouseOut ( this . props . nodeData . id , evt ) ;
102
- }
92
+ } ;
103
93
104
94
componentWillLeave ( done ) {
105
- const { nodeData : { parent } , orientation, transitionDuration } = this . props ;
106
- const originX = parent ? parent . x : 0 ;
107
- const originY = parent ? parent . y : 0 ;
108
- const transform = this . setTransformOrientation ( originX , originY , orientation ) ;
109
-
95
+ const { nodeData, orientation, transitionDuration } = this . props ;
96
+ const transform = this . setTransform ( nodeData , orientation , true ) ;
110
97
this . applyTransform ( transform , transitionDuration , 0 , done ) ;
111
98
}
112
99
@@ -157,20 +144,20 @@ Node.defaultProps = {
157
144
} ;
158
145
159
146
Node . propTypes = {
160
- nodeData : PropTypes . object . isRequired ,
161
- nodeSvgShape : PropTypes . object . isRequired ,
162
- nodeLabelComponent : PropTypes . object ,
163
- nodeSize : PropTypes . object . isRequired ,
164
- orientation : PropTypes . oneOf ( [ 'horizontal' , 'vertical' ] ) . isRequired ,
165
- transitionDuration : PropTypes . number . isRequired ,
166
- onClick : PropTypes . func . isRequired ,
167
- onMouseOver : PropTypes . func . isRequired ,
168
- onMouseOut : PropTypes . func . isRequired ,
169
- name : PropTypes . string . isRequired ,
170
- attributes : PropTypes . object ,
171
- textLayout : PropTypes . object . isRequired ,
172
- subscriptions : PropTypes . object . isRequired , // eslint-disable-line react/no-unused-prop-types
173
- allowForeignObjects : PropTypes . bool . isRequired ,
174
- circleRadius : PropTypes . number ,
175
- styles : PropTypes . object ,
147
+ nodeData : T . object . isRequired ,
148
+ nodeSvgShape : T . object . isRequired ,
149
+ nodeLabelComponent : T . object ,
150
+ nodeSize : T . object . isRequired ,
151
+ orientation : T . oneOf ( [ 'horizontal' , 'vertical' ] ) . isRequired ,
152
+ transitionDuration : T . number . isRequired ,
153
+ onClick : T . func . isRequired ,
154
+ onMouseOver : T . func . isRequired ,
155
+ onMouseOut : T . func . isRequired ,
156
+ name : T . string . isRequired ,
157
+ attributes : T . object ,
158
+ textLayout : T . object . isRequired ,
159
+ subscriptions : T . object . isRequired , // eslint-disable-line react/no-unused-prop-types
160
+ allowForeignObjects : T . bool . isRequired ,
161
+ circleRadius : T . number ,
162
+ styles : T . object ,
176
163
} ;
0 commit comments