UNPKG

7.56 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
3import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
5import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
6import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
7var _excluded = ["className", "style", "classNames", "styles"];
8import React, { useContext, useEffect, useRef, useState } from 'react';
9import clsx from 'classnames';
10import { CSSMotionList } from 'rc-motion';
11import Notice from "./Notice";
12import { NotificationContext } from "./NotificationProvider";
13import useStack from "./hooks/useStack";
14var NoticeList = function NoticeList(props) {
15 var configList = props.configList,
16 placement = props.placement,
17 prefixCls = props.prefixCls,
18 className = props.className,
19 style = props.style,
20 motion = props.motion,
21 onAllNoticeRemoved = props.onAllNoticeRemoved,
22 onNoticeClose = props.onNoticeClose,
23 stackConfig = props.stack;
24 var _useContext = useContext(NotificationContext),
25 ctxCls = _useContext.classNames;
26 var dictRef = useRef({});
27 var _useState = useState(null),
28 _useState2 = _slicedToArray(_useState, 2),
29 latestNotice = _useState2[0],
30 setLatestNotice = _useState2[1];
31 var _useState3 = useState([]),
32 _useState4 = _slicedToArray(_useState3, 2),
33 hoverKeys = _useState4[0],
34 setHoverKeys = _useState4[1];
35 var keys = configList.map(function (config) {
36 return {
37 config: config,
38 key: String(config.key)
39 };
40 });
41 var _useStack = useStack(stackConfig),
42 _useStack2 = _slicedToArray(_useStack, 2),
43 stack = _useStack2[0],
44 _useStack2$ = _useStack2[1],
45 offset = _useStack2$.offset,
46 threshold = _useStack2$.threshold,
47 gap = _useStack2$.gap;
48 var expanded = stack && (hoverKeys.length > 0 || keys.length <= threshold);
49 var placementMotion = typeof motion === 'function' ? motion(placement) : motion;
50
51 // Clean hover key
52 useEffect(function () {
53 if (stack && hoverKeys.length > 1) {
54 setHoverKeys(function (prev) {
55 return prev.filter(function (key) {
56 return keys.some(function (_ref) {
57 var dataKey = _ref.key;
58 return key === dataKey;
59 });
60 });
61 });
62 }
63 }, [hoverKeys, keys, stack]);
64
65 // Force update latest notice
66 useEffect(function () {
67 var _keys;
68 if (stack && dictRef.current[(_keys = keys[keys.length - 1]) === null || _keys === void 0 ? void 0 : _keys.key]) {
69 var _keys2;
70 setLatestNotice(dictRef.current[(_keys2 = keys[keys.length - 1]) === null || _keys2 === void 0 ? void 0 : _keys2.key]);
71 }
72 }, [keys, stack]);
73 return /*#__PURE__*/React.createElement(CSSMotionList, _extends({
74 key: placement,
75 className: clsx(prefixCls, "".concat(prefixCls, "-").concat(placement), ctxCls === null || ctxCls === void 0 ? void 0 : ctxCls.list, className, _defineProperty(_defineProperty({}, "".concat(prefixCls, "-stack"), !!stack), "".concat(prefixCls, "-stack-expanded"), expanded)),
76 style: style,
77 keys: keys,
78 motionAppear: true
79 }, placementMotion, {
80 onAllRemoved: function onAllRemoved() {
81 onAllNoticeRemoved(placement);
82 }
83 }), function (_ref2, nodeRef) {
84 var config = _ref2.config,
85 motionClassName = _ref2.className,
86 motionStyle = _ref2.style,
87 motionIndex = _ref2.index;
88 var _ref3 = config,
89 key = _ref3.key,
90 times = _ref3.times;
91 var strKey = String(key);
92 var _ref4 = config,
93 configClassName = _ref4.className,
94 configStyle = _ref4.style,
95 configClassNames = _ref4.classNames,
96 configStyles = _ref4.styles,
97 restConfig = _objectWithoutProperties(_ref4, _excluded);
98 var dataIndex = keys.findIndex(function (item) {
99 return item.key === strKey;
100 });
101
102 // If dataIndex is -1, that means this notice has been removed in data, but still in dom
103 // Should minus (motionIndex - 1) to get the correct index because keys.length is not the same as dom length
104 var stackStyle = {};
105 if (stack) {
106 var index = keys.length - 1 - (dataIndex > -1 ? dataIndex : motionIndex - 1);
107 var transformX = placement === 'top' || placement === 'bottom' ? '-50%' : '0';
108 if (index > 0) {
109 var _dictRef$current$strK, _dictRef$current$strK2, _dictRef$current$strK3;
110 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;
111
112 // Transform
113 var verticalOffset = 0;
114 for (var i = 0; i < index; i++) {
115 var _dictRef$current$keys;
116 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;
117 }
118 var transformY = (expanded ? verticalOffset : index * offset) * (placement.startsWith('top') ? 1 : -1);
119 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;
120 stackStyle.transform = "translate3d(".concat(transformX, ", ").concat(transformY, "px, 0) scaleX(").concat(scaleX, ")");
121 } else {
122 stackStyle.transform = "translate3d(".concat(transformX, ", 0, 0)");
123 }
124 }
125 return /*#__PURE__*/React.createElement("div", {
126 ref: nodeRef,
127 className: clsx("".concat(prefixCls, "-notice-wrapper"), motionClassName, configClassNames === null || configClassNames === void 0 ? void 0 : configClassNames.wrapper),
128 style: _objectSpread(_objectSpread(_objectSpread({}, motionStyle), stackStyle), configStyles === null || configStyles === void 0 ? void 0 : configStyles.wrapper),
129 onMouseEnter: function onMouseEnter() {
130 return setHoverKeys(function (prev) {
131 return prev.includes(strKey) ? prev : [].concat(_toConsumableArray(prev), [strKey]);
132 });
133 },
134 onMouseLeave: function onMouseLeave() {
135 return setHoverKeys(function (prev) {
136 return prev.filter(function (k) {
137 return k !== strKey;
138 });
139 });
140 }
141 }, /*#__PURE__*/React.createElement(Notice, _extends({}, restConfig, {
142 ref: function ref(node) {
143 if (dataIndex > -1) {
144 dictRef.current[strKey] = node;
145 } else {
146 delete dictRef.current[strKey];
147 }
148 },
149 prefixCls: prefixCls,
150 classNames: configClassNames,
151 styles: configStyles,
152 className: clsx(configClassName, ctxCls === null || ctxCls === void 0 ? void 0 : ctxCls.notice),
153 style: configStyle,
154 times: times,
155 key: key,
156 eventKey: key,
157 onNoticeClose: onNoticeClose,
158 hovering: stack && hoverKeys.length > 0
159 })));
160 });
161};
162if (process.env.NODE_ENV !== 'production') {
163 NoticeList.displayName = 'NoticeList';
164}
165export default NoticeList;
\No newline at end of file