Skip to content

Commit da8f3cd

Browse files
committed
Enables transitioning of mounted nodes between coordinates
1 parent 2a540f9 commit da8f3cd

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

src/Node/index.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,55 @@ export default class Node extends React.Component {
88

99
constructor(props) {
1010
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+
1119
this.handleClick = this.handleClick.bind(this);
1220
}
1321

1422
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);
1825

19-
select(this.node)
20-
.attr('transform', this.setTransformOrientation(originX, originY));
26+
this.applyTransform(transform);
27+
}
2128

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+
}
2635
}
2736

28-
// FIXME unstable re-rendering when regressing to parent coordinates
2937
setTransformOrientation(x, y) {
3038
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})`;
4542
return transform;
4643
}
4744

45+
applyTransform(transform, done) {
46+
select(this.node)
47+
.transition()
48+
.duration(500)
49+
.attr('transform', transform)
50+
.each('end', done);
51+
}
52+
4853
componentWillLeave(done) {
4954
const { parent } = this.props.nodeData;
5055
const originX = parent ? parent.x : 0;
5156
const originY = parent ? parent.y : 0;
57+
const transform = this.setTransformOrientation(originX, originY);
5258

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);
5860
}
5961

6062
handleClick() {
@@ -72,7 +74,7 @@ export default class Node extends React.Component {
7274
id={nodeData.id}
7375
ref={(n) => { this.node = n; }}
7476
className={nodeData._children ? 'nodeBase' : 'leafNodeBase'}
75-
transform={this.setTransformOrientation(nodeData.x, nodeData.y)}
77+
transform={this.state.transform}
7678
onClick={this.handleClick}
7779
>
7880
<text

0 commit comments

Comments
 (0)