NoticeList.js 9.0 KB

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