victory-core
Version:
160 lines (159 loc) • 7.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.VictoryTransition = void 0;
var _react = _interopRequireDefault(require("react"));
var _defaults = _interopRequireDefault(require("lodash/defaults"));
var _pick = _interopRequireDefault(require("lodash/pick"));
var _reactFastCompare = _interopRequireDefault(require("react-fast-compare"));
var _victoryAnimation = require("../victory-animation/victory-animation");
var Collection = _interopRequireWildcard(require("../victory-util/collection"));
var Helpers = _interopRequireWildcard(require("../victory-util/helpers"));
var _timerContext = _interopRequireDefault(require("../victory-util/timer-context"));
var Transitions = _interopRequireWildcard(require("../victory-util/transitions"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
class VictoryTransition extends _react.default.Component {
static displayName = "VictoryTransition";
static contextType = _timerContext.default;
constructor(props, context) {
super(props, context);
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.timer = this.context.transitionTimer;
}
componentDidMount() {
this.setState({
nodesShouldLoad: true
});
}
shouldComponentUpdate(nextProps) {
if (!(0, _reactFastCompare.default)(this.props, nextProps)) {
this.timer.bypassAnimation();
this.setState(this.getTransitionState(this.props, nextProps), () => this.timer.resumeAnimation());
}
return true;
}
componentWillUnmount() {
this.timer.stop();
}
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
};
}
const oldChildren = _react.default.Children.toArray(props.children);
const nextChildren = _react.default.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 && Helpers.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.default.Children.toArray(child.props.children)));
}
return memo;
}, []);
};
const child = _react.default.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;
}
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 = props.animate?.parentState;
if (parentState && parentState.nodesWillExit) {
return this.continuous || 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 = this.props.animate?.getTransitions ? this.props.animate.getTransitions : Transitions.getTransitionPropsFactory(props, this.state, newState => this.setState(newState));
const child = _react.default.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 = (0, _defaults.default)({
domain,
clipWidth
}, transitionProps, child.props);
const animationWhitelist = props.animationWhitelist || [];
const whitelist = animationWhitelist.concat(["clipWidth"]);
const propsToAnimate = whitelist.length ? (0, _pick.default)(combinedProps, whitelist) : combinedProps;
return /*#__PURE__*/_react.default.createElement(_victoryAnimation.VictoryAnimation, _extends({}, combinedProps.animate, {
data: propsToAnimate
}), newProps => {
if (child.props.groupComponent) {
const groupComponent = this.continuous ? /*#__PURE__*/_react.default.cloneElement(child.props.groupComponent, {
clipWidth: newProps.clipWidth || 0
}) : child.props.groupComponent;
return /*#__PURE__*/_react.default.cloneElement(child, (0, _defaults.default)({
animate: null,
animating: true,
groupComponent
}, newProps, combinedProps));
}
return /*#__PURE__*/_react.default.cloneElement(child, (0, _defaults.default)({
animate: null,
animating: true
}, newProps, combinedProps));
});
}
}
exports.VictoryTransition = VictoryTransition;