/
Notification.tsx
277 lines (245 loc) · 7.82 KB
/
Notification.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import * as React from 'react';
import { Component, ReactText } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import { CSSMotionList } from 'rc-motion';
import Notice, { NoticeProps } from './Notice';
import useNotification from './useNotification';
let seed = 0;
const now = Date.now();
function getUuid() {
const id = seed;
seed += 1;
return `rcNotification_${now}_${id}`;
}
export interface NoticeContent
extends Omit<NoticeProps, 'prefixCls' | 'children' | 'noticeKey' | 'onClose'> {
prefixCls?: string;
key?: React.Key;
updateMark?: string;
content?: React.ReactNode;
onClose?: () => void;
}
export type NoticeFunc = (noticeProps: NoticeContent) => void;
export type HolderReadyCallback = (
div: HTMLDivElement,
noticeProps: NoticeProps & { key: React.Key },
) => void;
export interface NotificationInstance {
notice: NoticeFunc;
removeNotice: (key: React.Key) => void;
destroy: () => void;
component: Notification;
useNotification: () => [NoticeFunc, React.ReactElement];
}
export interface NotificationProps {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
transitionName?: string;
animation?: string | object;
maxCount?: number;
closeIcon?: React.ReactNode;
}
interface NotificationState {
notices: {
notice: NoticeContent & {
userPassKey?: React.Key;
};
holderCallback?: HolderReadyCallback;
}[];
}
class Notification extends Component<NotificationProps, NotificationState> {
static newInstance: (
properties: NotificationProps & { getContainer?: () => HTMLElement },
callback: (instance: NotificationInstance) => void,
) => void;
static defaultProps = {
prefixCls: 'rc-notification',
animation: 'fade',
style: {
top: 65,
left: '50%',
},
};
state: NotificationState = {
notices: [],
};
private hookRefs = new Map<React.Key, HTMLDivElement>();
getTransitionName() {
const { prefixCls, animation } = this.props;
let { transitionName } = this.props;
if (!transitionName && animation) {
transitionName = `${prefixCls}-${animation}`;
}
return transitionName;
}
add = (originNotice: NoticeContent, holderCallback?: HolderReadyCallback) => {
const key = originNotice.key || getUuid();
const notice: NoticeContent & { key: React.Key; userPassKey?: React.Key } = {
...originNotice,
key,
};
const { maxCount } = this.props;
this.setState(previousState => {
const { notices } = previousState;
const noticeIndex = notices.map(v => v.notice.key).indexOf(key);
const updatedNotices = notices.concat();
if (noticeIndex !== -1) {
updatedNotices.splice(noticeIndex, 1, { notice, holderCallback });
} else {
if (maxCount && notices.length >= maxCount) {
// XXX, use key of first item to update new added (let React to move exsiting
// instead of remove and mount). Same key was used before for both a) external
// manual control and b) internal react 'key' prop , which is not that good.
// eslint-disable-next-line no-param-reassign
// zombieJ: Not know why use `updateKey`. This makes Notice infinite loop in jest.
// Change to `updateMark` for compare instead.
// https://github.com/react-component/notification/commit/32299e6be396f94040bfa82517eea940db947ece
notice.key = updatedNotices[0].notice.key;
notice.updateMark = getUuid();
// zombieJ: That's why. User may close by key directly.
// We need record this but not re-render to avoid upper issue
// https://github.com/react-component/notification/issues/129
notice.userPassKey = key;
updatedNotices.shift();
}
updatedNotices.push({ notice, holderCallback });
}
return {
notices: updatedNotices,
};
});
};
remove = (removeKey: React.Key) => {
this.setState(({ notices }) => ({
notices: notices.filter(({ notice: { key, userPassKey } }) => {
const mergedKey = userPassKey || key;
return mergedKey !== removeKey;
}),
}));
};
noticePropsMap: Record<
React.Key,
{
props: NoticeProps & {
key: ReactText;
};
holderCallback?: HolderReadyCallback;
}
> = {};
render() {
const { notices } = this.state;
const { prefixCls, className, closeIcon, style } = this.props;
const noticeKeys: React.Key[] = [];
notices.forEach(({ notice, holderCallback }, index) => {
const updateMark = index === notices.length - 1 ? notice.updateMark : undefined;
const { key, userPassKey } = notice;
const noticeProps = {
prefixCls,
closeIcon,
...notice,
...notice.props,
key,
noticeKey: userPassKey || key,
updateMark,
onClose: (noticeKey: React.Key) => {
this.remove(noticeKey);
notice.onClose?.();
},
onClick: notice.onClick,
children: notice.content,
} as NoticeProps & { key: ReactText };
// Give to motion
noticeKeys.push(key);
this.noticePropsMap[key] = { props: noticeProps, holderCallback };
});
return (
<div className={classNames(prefixCls, className)} style={style}>
<CSSMotionList
keys={noticeKeys}
motionName={this.getTransitionName()}
onVisibleChanged={(changedVisible, { key }) => {
if (!changedVisible) {
delete this.noticePropsMap[key];
}
}}
>
{({ key, className: motionClassName, style: motionStyle }) => {
const { props: noticeProps, holderCallback } = this.noticePropsMap[key];
if (holderCallback) {
return (
<div
key={key}
className={classNames(motionClassName, `${prefixCls}-hook-holder`)}
style={{ ...motionStyle }}
ref={div => {
if (typeof key === 'undefined') {
return;
}
if (div) {
this.hookRefs.set(key, div);
holderCallback(div, noticeProps);
} else {
this.hookRefs.delete(key);
}
}}
/>
);
}
return (
<Notice
{...noticeProps}
className={classNames(motionClassName, noticeProps?.className)}
style={{ ...motionStyle, ...noticeProps?.style }}
/>
);
}}
</CSSMotionList>
</div>
);
}
}
Notification.newInstance = function newNotificationInstance(properties, callback) {
const { getContainer, ...props } = properties || {};
const div = document.createElement('div');
if (getContainer) {
const root = getContainer();
root.appendChild(div);
} else {
document.body.appendChild(div);
}
let called = false;
function ref(notification: Notification) {
if (called) {
return;
}
called = true;
callback({
notice(noticeProps) {
notification.add(noticeProps);
},
removeNotice(key) {
notification.remove(key);
},
component: notification,
destroy() {
ReactDOM.unmountComponentAtNode(div);
if (div.parentNode) {
div.parentNode.removeChild(div);
}
},
// Hooks
useNotification() {
return useNotification(notification);
},
});
}
// Only used for test case usage
if (process.env.NODE_ENV === 'test' && (properties as any).TEST_RENDER) {
(properties as any).TEST_RENDER(<Notification {...props} ref={ref} />);
return;
}
ReactDOM.render(<Notification {...props} ref={ref} />, div);
};
export default Notification;