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 _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
11 | var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
12 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
13 | var React = _interopRequireWildcard(require("react"));
|
14 | var _reactDom = require("react-dom");
|
15 | var _rcMotion = require("rc-motion");
|
16 | var _classnames = _interopRequireDefault(require("classnames"));
|
17 | var _Notice = _interopRequireDefault(require("./Notice"));
|
18 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
19 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
20 |
|
21 | var Notifications = React.forwardRef(function (props, ref) {
|
22 | var _props$prefixCls = props.prefixCls,
|
23 | prefixCls = _props$prefixCls === void 0 ? 'rc-notification' : _props$prefixCls,
|
24 | container = props.container,
|
25 | motion = props.motion,
|
26 | maxCount = props.maxCount,
|
27 | className = props.className,
|
28 | style = props.style,
|
29 | onAllRemoved = props.onAllRemoved;
|
30 | var _React$useState = React.useState([]),
|
31 | _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
|
32 | configList = _React$useState2[0],
|
33 | setConfigList = _React$useState2[1];
|
34 |
|
35 | var onNoticeClose = function onNoticeClose(key) {
|
36 | var _config$onClose;
|
37 |
|
38 | var config = configList.find(function (item) {
|
39 | return item.key === key;
|
40 | });
|
41 | config === null || config === void 0 ? void 0 : (_config$onClose = config.onClose) === null || _config$onClose === void 0 ? void 0 : _config$onClose.call(config);
|
42 | setConfigList(function (list) {
|
43 | return list.filter(function (item) {
|
44 | return item.key !== key;
|
45 | });
|
46 | });
|
47 | };
|
48 |
|
49 | React.useImperativeHandle(ref, function () {
|
50 | return {
|
51 | open: function open(config) {
|
52 | setConfigList(function (list) {
|
53 | var clone = (0, _toConsumableArray2.default)(list);
|
54 |
|
55 | var index = clone.findIndex(function (item) {
|
56 | return item.key === config.key;
|
57 | });
|
58 | var innerConfig = (0, _objectSpread2.default)({}, config);
|
59 | if (index >= 0) {
|
60 | var _list$index;
|
61 | innerConfig.times = (((_list$index = list[index]) === null || _list$index === void 0 ? void 0 : _list$index.times) || 0) + 1;
|
62 | clone[index] = innerConfig;
|
63 | } else {
|
64 | innerConfig.times = 0;
|
65 | clone.push(innerConfig);
|
66 | }
|
67 | if (maxCount > 0 && clone.length > maxCount) {
|
68 | clone = clone.slice(-maxCount);
|
69 | }
|
70 | return clone;
|
71 | });
|
72 | },
|
73 | close: function close(key) {
|
74 | onNoticeClose(key);
|
75 | },
|
76 | destroy: function destroy() {
|
77 | setConfigList([]);
|
78 | }
|
79 | };
|
80 | });
|
81 |
|
82 | var _React$useState3 = React.useState({}),
|
83 | _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
|
84 | placements = _React$useState4[0],
|
85 | setPlacements = _React$useState4[1];
|
86 | React.useEffect(function () {
|
87 | var nextPlacements = {};
|
88 | configList.forEach(function (config) {
|
89 | var _config$placement = config.placement,
|
90 | placement = _config$placement === void 0 ? 'topRight' : _config$placement;
|
91 | if (placement) {
|
92 | nextPlacements[placement] = nextPlacements[placement] || [];
|
93 | nextPlacements[placement].push(config);
|
94 | }
|
95 | });
|
96 |
|
97 | Object.keys(placements).forEach(function (placement) {
|
98 | nextPlacements[placement] = nextPlacements[placement] || [];
|
99 | });
|
100 | setPlacements(nextPlacements);
|
101 | }, [configList]);
|
102 |
|
103 | var onAllNoticeRemoved = function onAllNoticeRemoved(placement) {
|
104 | setPlacements(function (originPlacements) {
|
105 | var clone = (0, _objectSpread2.default)({}, originPlacements);
|
106 | var list = clone[placement] || [];
|
107 | if (!list.length) {
|
108 | delete clone[placement];
|
109 | }
|
110 | return clone;
|
111 | });
|
112 | };
|
113 |
|
114 | var emptyRef = React.useRef(false);
|
115 | React.useEffect(function () {
|
116 | if (Object.keys(placements).length > 0) {
|
117 | emptyRef.current = true;
|
118 | } else if (emptyRef.current) {
|
119 |
|
120 | onAllRemoved === null || onAllRemoved === void 0 ? void 0 : onAllRemoved();
|
121 | emptyRef.current = false;
|
122 | }
|
123 | }, [placements]);
|
124 |
|
125 | if (!container) {
|
126 | return null;
|
127 | }
|
128 | var placementList = Object.keys(placements);
|
129 | return (0, _reactDom.createPortal)( React.createElement(React.Fragment, null, placementList.map(function (placement) {
|
130 | var placementConfigList = placements[placement];
|
131 | var keys = placementConfigList.map(function (config) {
|
132 | return {
|
133 | config: config,
|
134 | key: config.key
|
135 | };
|
136 | });
|
137 | var placementMotion = typeof motion === 'function' ? motion(placement) : motion;
|
138 | return React.createElement(_rcMotion.CSSMotionList, (0, _extends2.default)({
|
139 | key: placement,
|
140 | className: (0, _classnames.default)(prefixCls, "".concat(prefixCls, "-").concat(placement), className === null || className === void 0 ? void 0 : className(placement)),
|
141 | style: style === null || style === void 0 ? void 0 : style(placement),
|
142 | keys: keys,
|
143 | motionAppear: true
|
144 | }, placementMotion, {
|
145 | onAllRemoved: function onAllRemoved() {
|
146 | onAllNoticeRemoved(placement);
|
147 | }
|
148 | }), function (_ref, nodeRef) {
|
149 | var config = _ref.config,
|
150 | motionClassName = _ref.className,
|
151 | motionStyle = _ref.style;
|
152 | var key = config.key,
|
153 | times = config.times;
|
154 | var configClassName = config.className,
|
155 | configStyle = config.style;
|
156 | return React.createElement(_Notice.default, (0, _extends2.default)({}, config, {
|
157 | ref: nodeRef,
|
158 | prefixCls: prefixCls,
|
159 | className: (0, _classnames.default)(motionClassName, configClassName),
|
160 | style: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, motionStyle), configStyle),
|
161 | times: times,
|
162 | key: key,
|
163 | eventKey: key,
|
164 | onNoticeClose: onNoticeClose
|
165 | }));
|
166 | });
|
167 | })), container);
|
168 | });
|
169 | if (process.env.NODE_ENV !== 'production') {
|
170 | Notifications.displayName = 'Notifications';
|
171 | }
|
172 | var _default = Notifications;
|
173 | exports.default = _default; |
\ | No newline at end of file |