UNPKG

5.11 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
5import _createClass from "@babel/runtime/helpers/esm/createClass";
6import _inherits from "@babel/runtime/helpers/esm/inherits";
7import _createSuper from "@babel/runtime/helpers/esm/createSuper";
8var _excluded = ["component", "children", "onVisibleChanged"],
9 _excluded2 = ["status"];
10
11/* eslint react/prop-types: 0 */
12import * as React from 'react';
13import OriginCSSMotion from './CSSMotion';
14import { supportTransition } from './util/motion';
15import { STATUS_ADD, STATUS_KEEP, STATUS_REMOVE, STATUS_REMOVED, diffKeys, parseKeys } from './util/diff';
16var MOTION_PROP_NAMES = ['eventProps', 'visible', 'children', 'motionName', 'motionAppear', 'motionEnter', 'motionLeave', 'motionLeaveImmediately', 'motionDeadline', 'removeOnLeave', 'leavedClassName', 'onAppearStart', 'onAppearActive', 'onAppearEnd', 'onEnterStart', 'onEnterActive', 'onEnterEnd', 'onLeaveStart', 'onLeaveActive', 'onLeaveEnd'];
17/**
18 * Generate a CSSMotionList component with config
19 * @param transitionSupport No need since CSSMotionList no longer depends on transition support
20 * @param CSSMotion CSSMotion component
21 */
22
23export function genCSSMotionList(transitionSupport) {
24 var CSSMotion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : OriginCSSMotion;
25
26 var CSSMotionList = /*#__PURE__*/function (_React$Component) {
27 _inherits(CSSMotionList, _React$Component);
28
29 var _super = _createSuper(CSSMotionList);
30
31 function CSSMotionList() {
32 var _this;
33
34 _classCallCheck(this, CSSMotionList);
35
36 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
37 args[_key] = arguments[_key];
38 }
39
40 _this = _super.call.apply(_super, [this].concat(args));
41 _this.state = {
42 keyEntities: []
43 };
44
45 _this.removeKey = function (removeKey) {
46 _this.setState(function (_ref) {
47 var keyEntities = _ref.keyEntities;
48 return {
49 keyEntities: keyEntities.map(function (entity) {
50 if (entity.key !== removeKey) return entity;
51 return _objectSpread(_objectSpread({}, entity), {}, {
52 status: STATUS_REMOVED
53 });
54 })
55 };
56 });
57 };
58
59 return _this;
60 }
61
62 _createClass(CSSMotionList, [{
63 key: "render",
64 value: function render() {
65 var _this2 = this;
66
67 var keyEntities = this.state.keyEntities;
68
69 var _this$props = this.props,
70 component = _this$props.component,
71 children = _this$props.children,
72 _onVisibleChanged = _this$props.onVisibleChanged,
73 restProps = _objectWithoutProperties(_this$props, _excluded);
74
75 var Component = component || React.Fragment;
76 var motionProps = {};
77 MOTION_PROP_NAMES.forEach(function (prop) {
78 motionProps[prop] = restProps[prop];
79 delete restProps[prop];
80 });
81 delete restProps.keys;
82 return /*#__PURE__*/React.createElement(Component, restProps, keyEntities.map(function (_ref2) {
83 var status = _ref2.status,
84 eventProps = _objectWithoutProperties(_ref2, _excluded2);
85
86 var visible = status === STATUS_ADD || status === STATUS_KEEP;
87 return /*#__PURE__*/React.createElement(CSSMotion, _extends({}, motionProps, {
88 key: eventProps.key,
89 visible: visible,
90 eventProps: eventProps,
91 onVisibleChanged: function onVisibleChanged(changedVisible) {
92 _onVisibleChanged === null || _onVisibleChanged === void 0 ? void 0 : _onVisibleChanged(changedVisible, {
93 key: eventProps.key
94 });
95
96 if (!changedVisible) {
97 _this2.removeKey(eventProps.key);
98 }
99 }
100 }), children);
101 }));
102 }
103 }], [{
104 key: "getDerivedStateFromProps",
105 value: function getDerivedStateFromProps(_ref3, _ref4) {
106 var keys = _ref3.keys;
107 var keyEntities = _ref4.keyEntities;
108 var parsedKeyObjects = parseKeys(keys);
109 var mixedKeyEntities = diffKeys(keyEntities, parsedKeyObjects);
110 return {
111 keyEntities: mixedKeyEntities.filter(function (entity) {
112 var prevEntity = keyEntities.find(function (_ref5) {
113 var key = _ref5.key;
114 return entity.key === key;
115 }); // Remove if already mark as removed
116
117 if (prevEntity && prevEntity.status === STATUS_REMOVED && entity.status === STATUS_REMOVE) {
118 return false;
119 }
120
121 return true;
122 })
123 };
124 }
125 }]);
126
127 return CSSMotionList;
128 }(React.Component);
129
130 CSSMotionList.defaultProps = {
131 component: 'div'
132 };
133 return CSSMotionList;
134}
135export default genCSSMotionList(supportTransition);
\No newline at end of file