-
Notifications
You must be signed in to change notification settings - Fork 1
/
TransitionableComponent.js
61 lines (47 loc) · 1.51 KB
/
TransitionableComponent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React, { Component } from 'react';
import * as d3 from 'd3';
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
class TransitionableComponent extends Component {
constructor(props) {
super(props);
let { easing, duration } = props;
this.state = props;
this._defineEasing(easing);
this._defineDuration(duration);
}
_defineEasing(easing) {
if (!this.easing || easing !== this.state.easing) {
if (!easing) {
easing = 'linear';
}
this.easing = d3[`ease${capitalizeFirstLetter(easing)}`];
}
}
_defineDuration(duration) {
if (this.duration === undefined) {
this.duration = duration;
}
}
componentWillReceiveProps(newProps) {
this._defineEasing(newProps.easing);
this._defineDuration(newProps.duration);
const node = d3.select(this.refs.node);
let transition = node.transition()
.ease(this.easing);
if (this.duration !== undefined) {
transition.duration(this.duration);
}
Object.keys(newProps)
.forEach((k) => {
if (typeof newProps[k] != 'object') {
transition.attr(k, newProps[k]);
}
});
transition.on('end', () => {
this.setState(newProps);
});
}
}
export default TransitionableComponent;