1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | var _typeof = require("@babel/runtime/helpers/typeof");
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.default = void 0;
|
9 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
10 | var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
11 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
12 | var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
13 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
14 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
15 | var _react = _interopRequireWildcard(require("react"));
|
16 | var _classnames = _interopRequireDefault(require("classnames"));
|
17 | var _rcMotion = require("rc-motion");
|
18 | var _Notice = _interopRequireDefault(require("./Notice"));
|
19 | var _NotificationProvider = require("./NotificationProvider");
|
20 | var _useStack3 = _interopRequireDefault(require("./hooks/useStack"));
|
21 | var _excluded = ["className", "style", "classNames", "styles"];
|
22 | function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
23 | 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; }
|
24 | var NoticeList = function NoticeList(props) {
|
25 | var configList = props.configList,
|
26 | placement = props.placement,
|
27 | prefixCls = props.prefixCls,
|
28 | className = props.className,
|
29 | style = props.style,
|
30 | motion = props.motion,
|
31 | onAllNoticeRemoved = props.onAllNoticeRemoved,
|
32 | onNoticeClose = props.onNoticeClose,
|
33 | stackConfig = props.stack;
|
34 | var _useContext = (0, _react.useContext)(_NotificationProvider.NotificationContext),
|
35 | ctxCls = _useContext.classNames;
|
36 | var dictRef = (0, _react.useRef)({});
|
37 | var _useState = (0, _react.useState)(null),
|
38 | _useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
39 | latestNotice = _useState2[0],
|
40 | setLatestNotice = _useState2[1];
|
41 | var _useState3 = (0, _react.useState)([]),
|
42 | _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
|
43 | hoverKeys = _useState4[0],
|
44 | setHoverKeys = _useState4[1];
|
45 | var keys = configList.map(function (config) {
|
46 | return {
|
47 | config: config,
|
48 | key: String(config.key)
|
49 | };
|
50 | });
|
51 | var _useStack = (0, _useStack3.default)(stackConfig),
|
52 | _useStack2 = (0, _slicedToArray2.default)(_useStack, 2),
|
53 | stack = _useStack2[0],
|
54 | _useStack2$ = _useStack2[1],
|
55 | offset = _useStack2$.offset,
|
56 | threshold = _useStack2$.threshold,
|
57 | gap = _useStack2$.gap;
|
58 | var expanded = stack && (hoverKeys.length > 0 || keys.length <= threshold);
|
59 | var placementMotion = typeof motion === 'function' ? motion(placement) : motion;
|
60 |
|
61 |
|
62 | (0, _react.useEffect)(function () {
|
63 | if (stack && hoverKeys.length > 1) {
|
64 | setHoverKeys(function (prev) {
|
65 | return prev.filter(function (key) {
|
66 | return keys.some(function (_ref) {
|
67 | var dataKey = _ref.key;
|
68 | return key === dataKey;
|
69 | });
|
70 | });
|
71 | });
|
72 | }
|
73 | }, [hoverKeys, keys, stack]);
|
74 |
|
75 |
|
76 | (0, _react.useEffect)(function () {
|
77 | var _keys;
|
78 | if (stack && dictRef.current[(_keys = keys[keys.length - 1]) === null || _keys === void 0 ? void 0 : _keys.key]) {
|
79 | var _keys2;
|
80 | setLatestNotice(dictRef.current[(_keys2 = keys[keys.length - 1]) === null || _keys2 === void 0 ? void 0 : _keys2.key]);
|
81 | }
|
82 | }, [keys, stack]);
|
83 | return _react.default.createElement(_rcMotion.CSSMotionList, (0, _extends2.default)({
|
84 | key: placement,
|
85 | className: (0, _classnames.default)(prefixCls, "".concat(prefixCls, "-").concat(placement), ctxCls === null || ctxCls === void 0 ? void 0 : ctxCls.list, className, (0, _defineProperty2.default)((0, _defineProperty2.default)({}, "".concat(prefixCls, "-stack"), !!stack), "".concat(prefixCls, "-stack-expanded"), expanded)),
|
86 | style: style,
|
87 | keys: keys,
|
88 | motionAppear: true
|
89 | }, placementMotion, {
|
90 | onAllRemoved: function onAllRemoved() {
|
91 | onAllNoticeRemoved(placement);
|
92 | }
|
93 | }), function (_ref2, nodeRef) {
|
94 | var config = _ref2.config,
|
95 | motionClassName = _ref2.className,
|
96 | motionStyle = _ref2.style,
|
97 | motionIndex = _ref2.index;
|
98 | var _ref3 = config,
|
99 | key = _ref3.key,
|
100 | times = _ref3.times;
|
101 | var strKey = String(key);
|
102 | var _ref4 = config,
|
103 | configClassName = _ref4.className,
|
104 | configStyle = _ref4.style,
|
105 | configClassNames = _ref4.classNames,
|
106 | configStyles = _ref4.styles,
|
107 | restConfig = (0, _objectWithoutProperties2.default)(_ref4, _excluded);
|
108 | var dataIndex = keys.findIndex(function (item) {
|
109 | return item.key === strKey;
|
110 | });
|
111 |
|
112 |
|
113 |
|
114 | var stackStyle = {};
|
115 | if (stack) {
|
116 | var index = keys.length - 1 - (dataIndex > -1 ? dataIndex : motionIndex - 1);
|
117 | var transformX = placement === 'top' || placement === 'bottom' ? '-50%' : '0';
|
118 | if (index > 0) {
|
119 | var _dictRef$current$strK, _dictRef$current$strK2, _dictRef$current$strK3;
|
120 | stackStyle.height = expanded ? (_dictRef$current$strK = dictRef.current[strKey]) === null || _dictRef$current$strK === void 0 ? void 0 : _dictRef$current$strK.offsetHeight : latestNotice === null || latestNotice === void 0 ? void 0 : latestNotice.offsetHeight;
|
121 |
|
122 |
|
123 | var verticalOffset = 0;
|
124 | for (var i = 0; i < index; i++) {
|
125 | var _dictRef$current$keys;
|
126 | verticalOffset += ((_dictRef$current$keys = dictRef.current[keys[keys.length - 1 - i].key]) === null || _dictRef$current$keys === void 0 ? void 0 : _dictRef$current$keys.offsetHeight) + gap;
|
127 | }
|
128 | var transformY = (expanded ? verticalOffset : index * offset) * (placement.startsWith('top') ? 1 : -1);
|
129 | var scaleX = !expanded && latestNotice !== null && latestNotice !== void 0 && latestNotice.offsetWidth && (_dictRef$current$strK2 = dictRef.current[strKey]) !== null && _dictRef$current$strK2 !== void 0 && _dictRef$current$strK2.offsetWidth ? ((latestNotice === null || latestNotice === void 0 ? void 0 : latestNotice.offsetWidth) - offset * 2 * (index < 3 ? index : 3)) / ((_dictRef$current$strK3 = dictRef.current[strKey]) === null || _dictRef$current$strK3 === void 0 ? void 0 : _dictRef$current$strK3.offsetWidth) : 1;
|
130 | stackStyle.transform = "translate3d(".concat(transformX, ", ").concat(transformY, "px, 0) scaleX(").concat(scaleX, ")");
|
131 | } else {
|
132 | stackStyle.transform = "translate3d(".concat(transformX, ", 0, 0)");
|
133 | }
|
134 | }
|
135 | return _react.default.createElement("div", {
|
136 | ref: nodeRef,
|
137 | className: (0, _classnames.default)("".concat(prefixCls, "-notice-wrapper"), motionClassName, configClassNames === null || configClassNames === void 0 ? void 0 : configClassNames.wrapper),
|
138 | style: (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, motionStyle), stackStyle), configStyles === null || configStyles === void 0 ? void 0 : configStyles.wrapper),
|
139 | onMouseEnter: function onMouseEnter() {
|
140 | return setHoverKeys(function (prev) {
|
141 | return prev.includes(strKey) ? prev : [].concat((0, _toConsumableArray2.default)(prev), [strKey]);
|
142 | });
|
143 | },
|
144 | onMouseLeave: function onMouseLeave() {
|
145 | return setHoverKeys(function (prev) {
|
146 | return prev.filter(function (k) {
|
147 | return k !== strKey;
|
148 | });
|
149 | });
|
150 | }
|
151 | }, _react.default.createElement(_Notice.default, (0, _extends2.default)({}, restConfig, {
|
152 | ref: function ref(node) {
|
153 | if (dataIndex > -1) {
|
154 | dictRef.current[strKey] = node;
|
155 | } else {
|
156 | delete dictRef.current[strKey];
|
157 | }
|
158 | },
|
159 | prefixCls: prefixCls,
|
160 | classNames: configClassNames,
|
161 | styles: configStyles,
|
162 | className: (0, _classnames.default)(configClassName, ctxCls === null || ctxCls === void 0 ? void 0 : ctxCls.notice),
|
163 | style: configStyle,
|
164 | times: times,
|
165 | key: key,
|
166 | eventKey: key,
|
167 | onNoticeClose: onNoticeClose,
|
168 | hovering: stack && hoverKeys.length > 0
|
169 | })));
|
170 | });
|
171 | };
|
172 | if (process.env.NODE_ENV !== 'production') {
|
173 | NoticeList.displayName = 'NoticeList';
|
174 | }
|
175 | var _default = exports.default = NoticeList; |
\ | No newline at end of file |