This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathvictory-transition.js
179 lines (165 loc) · 6.07 KB
/
victory-transition.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React from "react";
import PropTypes from "prop-types";
import VictoryAnimation from "../victory-animation/victory-animation";
import Collection from "../victory-util/collection";
import Helpers from "../victory-util/helpers";
import Timer from "../victory-util/timer";
import Transitions from "../victory-util/transitions";
import { defaults, isFunction, pick, isObject } from "lodash";
export default class VictoryTransition extends React.Component {
static displayName = "VictoryTransition";
static propTypes = {
animate: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
animationWhitelist: PropTypes.array,
children: PropTypes.node
};
constructor(props) {
super(props);
this.state = {
nodesShouldLoad: false,
nodesDoneLoad: false
};
const child = this.props.children;
const polar = child.props.polar;
this.continuous = !polar && child.type && child.type.continuous === true;
this.getTransitionState = this.getTransitionState.bind(this);
this.getTimer = this.getTimer.bind(this);
}
componentDidMount() {
this.setState({ nodesShouldLoad: true }); //eslint-disable-line react/no-did-mount-set-state
}
componentWillReceiveProps(nextProps) {
this.getTimer().bypassAnimation();
this.setState(
this.getTransitionState(this.props, nextProps), () => this.getTimer().resumeAnimation()
);
}
componentWillUnmount() {
this.getTimer().stop();
}
getTimer() {
if (this.context.getTimer) {
return this.context.getTimer();
}
if (!this.timer) {
this.timer = new Timer();
}
return this.timer;
}
getTransitionState(props, nextProps) {
const { animate } = props;
if (!animate) {
return {};
} else if (animate.parentState) {
const state = animate.parentState;
const oldProps = state.nodesWillExit ? props : null;
return { oldProps, nextProps };
} else {
const oldChildren = React.Children.toArray(props.children);
const nextChildren = React.Children.toArray(nextProps.children);
const {
nodesWillExit,
nodesWillEnter,
childrenTransitions,
nodesShouldEnter
} = Transitions.getInitialTransitionState(oldChildren, nextChildren);
return {
nodesWillExit,
nodesWillEnter,
childrenTransitions,
nodesShouldEnter,
oldProps: nodesWillExit ? props : null,
nextProps
};
}
}
getDomainFromChildren(props, axis) {
const getChildDomains = (children) => {
return children.reduce((memo, child) => {
if (child.type && isFunction(child.type.getDomain)) {
const childDomain = child.props && child.type.getDomain(child.props, axis);
return childDomain ? memo.concat(childDomain) : memo;
} else if (child.props && child.props.children) {
return memo.concat(getChildDomains(React.Children.toArray(child.props.children)));
}
return memo;
}, []);
};
const child = React.Children.toArray(props.children)[0];
const childProps = child.props || {};
const domain = Array.isArray(childProps.domain) ?
childProps.domain : childProps.domain && childProps.domain[axis];
if (!childProps.children && domain) {
return domain;
} else {
const childDomains = getChildDomains([child]);
return childDomains.length === 0 ?
[0, 1] : [Collection.getMinValue(childDomains), Collection.getMaxValue(childDomains)];
}
}
pickProps() {
if (!this.state) {
return this.props;
}
return this.state.nodesWillExit ? this.state.oldProps || this.props : this.props;
}
pickDomainProps(props) {
const parentState = isObject(props.animate) && props.animate.parentState;
if (parentState && parentState.nodesWillExit) {
return this.continous || parentState.continuous ?
parentState.nextProps || this.state.nextProps || props : props;
}
return this.continuous && this.state.nodesWillExit ? this.state.nextProps || props : props;
}
getClipWidth(props, child) {
const getDefaultClipWidth = () => {
const range = Helpers.getRange(child.props, "x");
return range ? Math.abs(range[1] - range[0]) : props.width;
};
const clipWidth = this.transitionProps ? this.transitionProps.clipWidth : undefined;
return clipWidth !== undefined ? clipWidth : getDefaultClipWidth();
}
render() {
const props = this.pickProps();
const getTransitionProps = isObject(this.props.animate) && this.props.animate.getTransitions ?
this.props.animate.getTransitions :
Transitions.getTransitionPropsFactory(
props,
this.state,
(newState) => this.setState(newState)
);
const child = React.Children.toArray(props.children)[0];
const transitionProps = getTransitionProps(child);
this.transitionProps = transitionProps;
const domain = {
x: this.getDomainFromChildren(this.pickDomainProps(props), "x"),
y: this.getDomainFromChildren(props, "y")
};
const clipWidth = this.getClipWidth(props, child);
const combinedProps = defaults({ domain, clipWidth }, transitionProps, child.props);
const animationWhitelist = props.animationWhitelist || [];
const whitelist = animationWhitelist.concat(["clipWidth"]);
const propsToAnimate = whitelist.length ? pick(combinedProps, whitelist) : combinedProps;
return (
<VictoryAnimation {...combinedProps.animate} data={propsToAnimate}>
{(newProps) => {
if (child.props.groupComponent) {
const groupComponent = this.continuous ?
React.cloneElement(
child.props.groupComponent,
{ clipWidth: newProps.clipWidth || 0 }
) :
child.props.groupComponent;
return React.cloneElement(
child,
defaults({ animate: null, animating: true, groupComponent }, newProps, combinedProps)
);
}
return React.cloneElement(
child, defaults({ animate: null, animating: true }, newProps, combinedProps)
);
}}
</VictoryAnimation>
);
}
}