NoticeList.js 7.4 KB

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