11import React from 'react' ;
2- import PropTypes from 'prop-types' ;
2+ import T from 'prop-types' ;
33import { select } from 'd3' ;
44
5- import './style.css' ;
65import SvgTextElement from './SvgTextElement' ;
76import ForeignObjectElement from './ForeignObjectElement' ;
7+ import './style.css' ;
88
99export 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+ } ;
2716
2817 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 ) ;
3120
3221 this . applyTransform ( transform , transitionDuration ) ;
3322 }
3423
3524 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 ) ;
4126 this . applyTransform ( transform , nextProps . transitionDuration ) ;
4227 }
4328
4429 shouldComponentUpdate ( nextProps ) {
4530 return this . shouldNodeTransform ( this . props , nextProps ) ;
4631 }
4732
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+ }
5848 return orientation === 'horizontal' ? `translate(${ y } ,${ x } )` : `translate(${ x } ,${ y } )` ;
5949 }
6050
@@ -74,7 +64,7 @@ export default class Node extends React.Component {
7464 }
7565 }
7666
77- renderNodeElement ( nodeStyle ) {
67+ renderNodeElement = nodeStyle => {
7868 const { circleRadius, nodeSvgShape } = this . props ;
7969 /* TODO: DEPRECATE <circle /> */
8070 if ( circleRadius ) {
@@ -87,26 +77,23 @@ export default class Node extends React.Component {
8777 ...nodeStyle . circle ,
8878 ...nodeSvgShape . shapeProps ,
8979 } ) ;
90- }
80+ } ;
9181
92- handleClick ( evt ) {
82+ handleClick = evt => {
9383 this . props . onClick ( this . props . nodeData . id , evt ) ;
94- }
84+ } ;
9585
96- handleOnMouseOver ( evt ) {
86+ handleOnMouseOver = evt => {
9787 this . props . onMouseOver ( this . props . nodeData . id , evt ) ;
98- }
88+ } ;
9989
100- handleOnMouseOut ( evt ) {
90+ handleOnMouseOut = evt => {
10191 this . props . onMouseOut ( this . props . nodeData . id , evt ) ;
102- }
92+ } ;
10393
10494 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 ) ;
11097 this . applyTransform ( transform , transitionDuration , 0 , done ) ;
11198 }
11299
@@ -157,20 +144,20 @@ Node.defaultProps = {
157144} ;
158145
159146Node . 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 ,
176163} ;
0 commit comments