UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

370 lines (310 loc) 11.6 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; var _excluded = ["animation", "transitionName", "transitionEnter", "transitionAppear", "transitionLeave", "component", "componentProps"]; import { cloneElement, PureComponent, createElement, isValidElement } from 'react'; import omit from 'lodash/omit'; import noop from 'lodash/noop'; import { findChildInChildrenByKey, findShownChildInChildrenByKey, isSameChildren, mergeChildren, toArrayChildren } from './ChildrenUtils'; import AnimateChild from './AnimateChild'; import animUtil from './util'; var defaultKey = "animate_".concat(Date.now()); function getChildrenFromProps(props) { var children = props.children; if ( /*#__PURE__*/isValidElement(children)) { if (!children.key) { return /*#__PURE__*/cloneElement(children, { key: defaultKey }); } } return children; } var Animate = /*#__PURE__*/function (_PureComponent) { _inherits(Animate, _PureComponent); var _super = _createSuper(Animate); function Animate() { var _this; _classCallCheck(this, Animate); _this = _super.apply(this, arguments); _this.currentlyAnimatingKeys = {}; _this.keysToEnter = []; _this.keysToLeave = []; _this.state = { children: toArrayChildren(getChildrenFromProps(_this.props)) }; _this.childrenRefs = {}; _this.performEnter = function (key) { var childRef = _this.childrenRefs[key]; if (childRef) { _this.currentlyAnimatingKeys[key] = true; childRef.componentWillEnter(_this.handleDoneAdding.bind(_assertThisInitialized(_this), key, 'enter')); } }; _this.performAppear = function (key) { var childRef = _this.childrenRefs[key]; if (childRef) { _this.currentlyAnimatingKeys[key] = true; childRef.componentWillAppear(_this.handleDoneAdding.bind(_assertThisInitialized(_this), key, 'appear')); } }; _this.handleDoneAdding = function (key, type, childRef) { var _assertThisInitialize = _assertThisInitialized(_this), props = _assertThisInitialize.props; var exclusive = props.exclusive, _props$onAppear = props.onAppear, onAppear = _props$onAppear === void 0 ? noop : _props$onAppear, _props$onEnd = props.onEnd, onEnd = _props$onEnd === void 0 ? noop : _props$onEnd, _props$onEnter = props.onEnter, onEnter = _props$onEnter === void 0 ? noop : _props$onEnter; delete _this.currentlyAnimatingKeys[key]; if (exclusive && props !== _this.nextProps) { return; } if (!_this.isValidChildByKey(toArrayChildren(getChildrenFromProps(props)), key)) { _this.performLeave(key); } else if (type === 'appear') { if (animUtil.allowAppearCallback(props)) { onAppear(key, childRef); onEnd(key, true, childRef); } } else if (animUtil.allowEnterCallback(props)) { onEnter(key, childRef); onEnd(key, true, childRef); } }; _this.performLeave = function (key) { var childRef = _this.childrenRefs[key]; if (childRef) { _this.currentlyAnimatingKeys[key] = true; childRef.componentWillLeave(_this.handleDoneLeaving.bind(_assertThisInitialized(_this), key)); } }; _this.handleDoneLeaving = function (key, childRef) { var _assertThisInitialize2 = _assertThisInitialized(_this), props = _assertThisInitialize2.props, children = _assertThisInitialize2.state.children; var exclusive = props.exclusive, _props$onEnd2 = props.onEnd, onEnd = _props$onEnd2 === void 0 ? noop : _props$onEnd2, _props$onLeave = props.onLeave, onLeave = _props$onLeave === void 0 ? noop : _props$onLeave, hiddenProp = props.hiddenProp; delete _this.currentlyAnimatingKeys[key]; if (exclusive && props !== _this.nextProps) { return; } var currentChildren = toArrayChildren(getChildrenFromProps(props)); if (_this.isValidChildByKey(currentChildren, key)) { _this.performEnter(key); } else { var end = function end() { if (animUtil.allowLeaveCallback(props)) { onLeave(key, childRef); onEnd(key, false, childRef); } }; if (!isSameChildren(children, currentChildren, hiddenProp)) { _this.setState({ children: currentChildren }, end); } else { end(); } } }; return _this; } _createClass(Animate, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; var hiddenProp = this.props.hiddenProp; var children = this.state.children; if (hiddenProp) { children = children.filter(function (child) { return !child.props[hiddenProp]; }); } children.forEach(function (child) { if (child && child.key) { _this2.performAppear(child.key); } }); } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var _this3 = this; this.nextProps = nextProps; var nextChildren = toArrayChildren(getChildrenFromProps(nextProps)); var _this$props = this.props, exclusive = _this$props.exclusive, hiddenProp = _this$props.hiddenProp; var children = this.state.children; var currentlyAnimatingKeys = this.currentlyAnimatingKeys; if (exclusive) { Object.keys(currentlyAnimatingKeys).forEach(function (key) { return _this3.stop(key); }); } var currentChildren = exclusive ? toArrayChildren(getChildrenFromProps(this.props)) : children; var newChildren = []; if (hiddenProp) { nextChildren.forEach(function (nextChild) { if (nextChild) { var newChild; var currentChild = findChildInChildrenByKey(currentChildren, nextChild.key); if (nextChild.props[hiddenProp] && currentChild && !currentChild.props[hiddenProp]) { newChild = /*#__PURE__*/cloneElement(nextChild, _defineProperty({}, hiddenProp, false)); } else { newChild = nextChild; } if (newChild) { newChildren.push(newChild); } } }); newChildren = mergeChildren(currentChildren, newChildren); } else { newChildren = mergeChildren(currentChildren, nextChildren); } this.setState({ children: newChildren }); nextChildren.forEach(function (child) { var key = child && child.key; if (key) { if (child && currentlyAnimatingKeys[key]) { return; } var hasPrev = child && findChildInChildrenByKey(currentChildren, key); if (hiddenProp) { var showInNext = !child.props[hiddenProp]; if (hasPrev) { var showInNow = findShownChildInChildrenByKey(currentChildren, key, hiddenProp); if (!showInNow && showInNext) { _this3.keysToEnter.push(key); } } else if (showInNext) { _this3.keysToEnter.push(key); } } else if (!hasPrev) { _this3.keysToEnter.push(key); } } }); currentChildren.forEach(function (child) { var key = child && child.key; if (key) { if (child && currentlyAnimatingKeys[key]) { return; } var hasNext = child && findChildInChildrenByKey(nextChildren, key); if (hiddenProp) { var showInNow = !child.props[hiddenProp]; if (hasNext) { var showInNext = findShownChildInChildrenByKey(nextChildren, key, hiddenProp); if (!showInNext && showInNow) { _this3.keysToLeave.push(key); } } else if (showInNow) { _this3.keysToLeave.push(key); } } else if (!hasNext) { _this3.keysToLeave.push(key); } } }); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { var keysToEnter = this.keysToEnter; this.keysToEnter = []; keysToEnter.forEach(this.performEnter); var keysToLeave = this.keysToLeave; this.keysToLeave = []; keysToLeave.forEach(this.performLeave); } }, { key: "isValidChildByKey", value: function isValidChildByKey(currentChildren, key) { var hiddenProp = this.props.hiddenProp; if (hiddenProp) { return !!findShownChildInChildrenByKey(currentChildren, key, hiddenProp); } return !!findChildInChildrenByKey(currentChildren, key); } }, { key: "stop", value: function stop(key) { delete this.currentlyAnimatingKeys[key]; var component = this.childrenRefs[key]; if (component) { component.stop(); } } }, { key: "render", value: function render() { var _this4 = this; var props = this.props; this.nextProps = props; var animation = props.animation, transitionName = props.transitionName, transitionEnter = props.transitionEnter, transitionAppear = props.transitionAppear, transitionLeave = props.transitionLeave, Cmp = props.component, componentProps = props.componentProps, otherProps = _objectWithoutProperties(props, _excluded); var stateChildren = this.state.children; var children = []; if (stateChildren) { children = stateChildren.map(function (child) { if (child === null || child === undefined) { return child; } if (!child.key) { throw new Error('must set key for animate children'); } return /*#__PURE__*/createElement(AnimateChild, { key: child.key, ref: function ref(node) { if (child.key) { _this4.childrenRefs[child.key] = node; } }, animation: animation, transitionName: transitionName, transitionEnter: transitionEnter, transitionAppear: transitionAppear, transitionLeave: transitionLeave }, child); }); } if (Cmp) { var passedProps = omit(otherProps, ['exclusive', 'onEnd', 'onEnter', 'onLeave', 'onAppear', 'hiddenProp']); return /*#__PURE__*/createElement(Cmp, _objectSpread(_objectSpread({}, passedProps), componentProps), children); } return children[0] || null; } }]); return Animate; }(PureComponent); export { Animate as default }; Animate.displayName = 'Animate'; Animate.defaultProps = { component: 'span', transitionEnter: true, transitionLeave: true, transitionAppear: false }; //# sourceMappingURL=Animate.js.map