1 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
2 | import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
3 | import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
4 | import * as React from 'react';
|
5 | import { createPortal } from 'react-dom';
|
6 | import NoticeList from "./NoticeList";
|
7 |
|
8 | var Notifications = React.forwardRef(function (props, ref) {
|
9 | var _props$prefixCls = props.prefixCls,
|
10 | prefixCls = _props$prefixCls === void 0 ? 'rc-notification' : _props$prefixCls,
|
11 | container = props.container,
|
12 | motion = props.motion,
|
13 | maxCount = props.maxCount,
|
14 | className = props.className,
|
15 | style = props.style,
|
16 | onAllRemoved = props.onAllRemoved,
|
17 | stack = props.stack,
|
18 | renderNotifications = props.renderNotifications;
|
19 | var _React$useState = React.useState([]),
|
20 | _React$useState2 = _slicedToArray(_React$useState, 2),
|
21 | configList = _React$useState2[0],
|
22 | setConfigList = _React$useState2[1];
|
23 |
|
24 |
|
25 | var onNoticeClose = function onNoticeClose(key) {
|
26 | var _config$onClose;
|
27 |
|
28 | var config = configList.find(function (item) {
|
29 | return item.key === key;
|
30 | });
|
31 | config === null || config === void 0 || (_config$onClose = config.onClose) === null || _config$onClose === void 0 || _config$onClose.call(config);
|
32 | setConfigList(function (list) {
|
33 | return list.filter(function (item) {
|
34 | return item.key !== key;
|
35 | });
|
36 | });
|
37 | };
|
38 |
|
39 |
|
40 | React.useImperativeHandle(ref, function () {
|
41 | return {
|
42 | open: function open(config) {
|
43 | setConfigList(function (list) {
|
44 | var clone = _toConsumableArray(list);
|
45 |
|
46 |
|
47 | var index = clone.findIndex(function (item) {
|
48 | return item.key === config.key;
|
49 | });
|
50 | var innerConfig = _objectSpread({}, config);
|
51 | if (index >= 0) {
|
52 | var _list$index;
|
53 | innerConfig.times = (((_list$index = list[index]) === null || _list$index === void 0 ? void 0 : _list$index.times) || 0) + 1;
|
54 | clone[index] = innerConfig;
|
55 | } else {
|
56 | innerConfig.times = 0;
|
57 | clone.push(innerConfig);
|
58 | }
|
59 | if (maxCount > 0 && clone.length > maxCount) {
|
60 | clone = clone.slice(-maxCount);
|
61 | }
|
62 | return clone;
|
63 | });
|
64 | },
|
65 | close: function close(key) {
|
66 | onNoticeClose(key);
|
67 | },
|
68 | destroy: function destroy() {
|
69 | setConfigList([]);
|
70 | }
|
71 | };
|
72 | });
|
73 |
|
74 |
|
75 | var _React$useState3 = React.useState({}),
|
76 | _React$useState4 = _slicedToArray(_React$useState3, 2),
|
77 | placements = _React$useState4[0],
|
78 | setPlacements = _React$useState4[1];
|
79 | React.useEffect(function () {
|
80 | var nextPlacements = {};
|
81 | configList.forEach(function (config) {
|
82 | var _config$placement = config.placement,
|
83 | placement = _config$placement === void 0 ? 'topRight' : _config$placement;
|
84 | if (placement) {
|
85 | nextPlacements[placement] = nextPlacements[placement] || [];
|
86 | nextPlacements[placement].push(config);
|
87 | }
|
88 | });
|
89 |
|
90 |
|
91 | Object.keys(placements).forEach(function (placement) {
|
92 | nextPlacements[placement] = nextPlacements[placement] || [];
|
93 | });
|
94 | setPlacements(nextPlacements);
|
95 | }, [configList]);
|
96 |
|
97 |
|
98 | var onAllNoticeRemoved = function onAllNoticeRemoved(placement) {
|
99 | setPlacements(function (originPlacements) {
|
100 | var clone = _objectSpread({}, originPlacements);
|
101 | var list = clone[placement] || [];
|
102 | if (!list.length) {
|
103 | delete clone[placement];
|
104 | }
|
105 | return clone;
|
106 | });
|
107 | };
|
108 |
|
109 |
|
110 | var emptyRef = React.useRef(false);
|
111 | React.useEffect(function () {
|
112 | if (Object.keys(placements).length > 0) {
|
113 | emptyRef.current = true;
|
114 | } else if (emptyRef.current) {
|
115 |
|
116 | onAllRemoved === null || onAllRemoved === void 0 || onAllRemoved();
|
117 | emptyRef.current = false;
|
118 | }
|
119 | }, [placements]);
|
120 |
|
121 | if (!container) {
|
122 | return null;
|
123 | }
|
124 | var placementList = Object.keys(placements);
|
125 | return createPortal( React.createElement(React.Fragment, null, placementList.map(function (placement) {
|
126 | var placementConfigList = placements[placement];
|
127 | var list = React.createElement(NoticeList, {
|
128 | key: placement,
|
129 | configList: placementConfigList,
|
130 | placement: placement,
|
131 | prefixCls: prefixCls,
|
132 | className: className === null || className === void 0 ? void 0 : className(placement),
|
133 | style: style === null || style === void 0 ? void 0 : style(placement),
|
134 | motion: motion,
|
135 | onNoticeClose: onNoticeClose,
|
136 | onAllNoticeRemoved: onAllNoticeRemoved,
|
137 | stack: stack
|
138 | });
|
139 | return renderNotifications ? renderNotifications(list, {
|
140 | prefixCls: prefixCls,
|
141 | key: placement
|
142 | }) : list;
|
143 | })), container);
|
144 | });
|
145 | if (process.env.NODE_ENV !== 'production') {
|
146 | Notifications.displayName = 'Notifications';
|
147 | }
|
148 | export default Notifications; |
\ | No newline at end of file |