Skip to content

Commit 98c3d0a

Browse files
committed
feat: notification 组件插件化使用方式破坏性修改,支持 NotificationPlugin,notification 的调用,废弃 Notification.info
1 parent 94eeafa commit 98c3d0a

7 files changed

Lines changed: 98 additions & 97 deletions

File tree

src/notification/Notification.tsx

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,14 @@
11
import * as React from 'react';
2-
import { CloseIcon, InfoCircleFilledIcon, CheckCircleFilledIcon } from 'tdesign-icons-react';
2+
import { CheckCircleFilledIcon, CloseIcon, InfoCircleFilledIcon } from 'tdesign-icons-react';
33
import noop from '../_util/noop';
44
import useConfig from '../_util/useConfig';
55

6-
import {
7-
NotificationCloseMethod,
8-
NotificationErrorMethod,
9-
NotificationInfoMethod,
10-
NotificationSuccessMethod,
11-
NotificationWarningMethod,
12-
TdNotificationProps,
13-
NotificationThemeList,
14-
NotificationInfoOptions,
15-
NotificationInstance,
16-
NotificationPlacementList,
17-
NotificationCloseAllMethod,
18-
} from './type';
6+
import { NotificationInstance, TdNotificationProps } from './type';
197
import { Styles } from '../common';
20-
import { fetchListInstance, listMap } from './NotificationList';
218

229
const blockName = 'notification';
2310

24-
// 扩展接口声明的结构,用户使用时可得到 .info 的 ts 提示
25-
interface Notification extends React.FC<TdNotificationProps> {
26-
info: NotificationInfoMethod;
27-
success: NotificationSuccessMethod;
28-
warning: NotificationWarningMethod;
29-
error: NotificationErrorMethod;
30-
closeAll: NotificationCloseAllMethod;
31-
close: NotificationCloseMethod;
32-
}
33-
34-
interface NotificationProps extends TdNotificationProps {
11+
export interface NotificationProps extends TdNotificationProps {
3512
style?: Styles;
3613
}
3714

@@ -150,58 +127,4 @@ export const NotificationComponent = React.forwardRef<any, NotificationProps>((p
150127
);
151128
});
152129

153-
/**
154-
* @author kenzyyang
155-
* @date 2021-05-30 22:54:39
156-
* @desc 函数调用时的渲染函数
157-
* @param theme 主题类型
158-
* @param options 通知的参数
159-
*/
160-
const renderNotification = (theme: NotificationThemeList, options: NotificationInfoOptions) => {
161-
if (typeof options !== 'object') return;
162-
163-
const placement: NotificationPlacementList = (() => {
164-
if (['top-left', 'top-right', 'bottom-left', 'bottom-right'].indexOf(options.placement) >= 0) {
165-
return options.placement;
166-
}
167-
return 'top-right';
168-
})();
169-
170-
const attach: HTMLElement = (() => {
171-
if (options.attach && typeof options.attach === 'string') {
172-
const element: Element = document.querySelector(options.attach);
173-
if (element instanceof HTMLElement) return element;
174-
}
175-
176-
if (options.attach instanceof HTMLElement) return options.attach;
177-
178-
const containerId = `tdesign-notification-${placement}`;
179-
const container = document.querySelector(`#${containerId}`);
180-
if (container && container instanceof HTMLElement) {
181-
return container;
182-
}
183-
184-
const element: HTMLDivElement = document.createElement('div');
185-
element.setAttribute('id', containerId);
186-
document.body.appendChild(element);
187-
return element;
188-
})();
189-
190-
const zIndex = options.zIndex || 6000;
191-
192-
return fetchListInstance(placement, attach, zIndex).then((listInstance) => listInstance.push(theme, options));
193-
};
194-
195-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
196-
// @ts-ignore
197-
const Notification: Notification = NotificationComponent;
198-
199-
['info', 'success', 'warning', 'error'].forEach((theme: NotificationThemeList) => {
200-
Notification[theme] = (options) => renderNotification(theme, options);
201-
});
202-
203-
Notification.close = (promise) => promise.then((instance) => instance.close());
204-
205-
Notification.closeAll = () => listMap.forEach((list) => list.removeAll());
206-
207-
export default Notification;
130+
export default NotificationComponent;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { fetchListInstance, listMap } from 'tdesign-react/notification/NotificationList';
2+
import {
3+
NotificationCloseAllMethod,
4+
NotificationCloseMethod,
5+
NotificationErrorMethod,
6+
NotificationInfoMethod,
7+
NotificationInfoOptions,
8+
NotificationInstance,
9+
NotificationOptions,
10+
NotificationPlacementList,
11+
NotificationSuccessMethod,
12+
NotificationThemeList,
13+
NotificationWarningMethod,
14+
} from 'tdesign-react';
15+
16+
// 扩展接口声明的结构,用户使用时可得到 .info 的 ts 提示
17+
interface Notification {
18+
(theme: NotificationThemeList, options: NotificationOptions): Promise<NotificationInstance>;
19+
info: NotificationInfoMethod;
20+
success: NotificationSuccessMethod;
21+
warning: NotificationWarningMethod;
22+
error: NotificationErrorMethod;
23+
closeAll: NotificationCloseAllMethod;
24+
close: NotificationCloseMethod;
25+
}
26+
27+
/**
28+
* @desc 函数调用时的渲染函数
29+
* @param theme 主题类型
30+
* @param options 通知的参数
31+
*/
32+
const renderNotification = (theme: NotificationThemeList, options: NotificationInfoOptions) => {
33+
if (typeof options !== 'object') return;
34+
35+
const placement: NotificationPlacementList = (() => {
36+
if (['top-left', 'top-right', 'bottom-left', 'bottom-right'].indexOf(options.placement) >= 0) {
37+
return options.placement;
38+
}
39+
return 'top-right';
40+
})();
41+
42+
const attach: HTMLElement = (() => {
43+
if (options.attach && typeof options.attach === 'string') {
44+
const element: Element = document.querySelector(options.attach);
45+
if (element instanceof HTMLElement) return element;
46+
}
47+
48+
if (options.attach instanceof HTMLElement) return options.attach;
49+
50+
const containerId = `tdesign-notification-${placement}`;
51+
const container = document.querySelector(`#${containerId}`);
52+
if (container && container instanceof HTMLElement) {
53+
return container;
54+
}
55+
56+
const element: HTMLDivElement = document.createElement('div');
57+
element.setAttribute('id', containerId);
58+
document.body.appendChild(element);
59+
return element;
60+
})();
61+
62+
const zIndex = options.zIndex || 6000;
63+
64+
return fetchListInstance(placement, attach, zIndex).then((listInstance) => listInstance.push(theme, options));
65+
};
66+
67+
export const NotificationPlugin: Notification = (theme, props) => renderNotification(theme, props);
68+
NotificationPlugin.info = (options) => renderNotification('info', options);
69+
NotificationPlugin.success = (options) => renderNotification('success', options);
70+
NotificationPlugin.warning = (options) => renderNotification('warning', options);
71+
NotificationPlugin.error = (options) => renderNotification('error', options);
72+
NotificationPlugin.close = (promise) => promise.then((instance) => instance.close());
73+
NotificationPlugin.closeAll = () => listMap.forEach((list) => list.removeAll());
74+
75+
export const notification = NotificationPlugin;

src/notification/_example/close-all.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import React from 'react';
2-
import { Notification, Button } from 'tdesign-react';
2+
import { NotificationPlugin, Button } from 'tdesign-react';
33

44
export default function NotificationExample() {
55
const openMore = React.useCallback(() => {
6-
Notification.info({
6+
NotificationPlugin.info({
77
title: '标题名称',
88
content: '这是一条需要手动关闭的消息通知',
99
duration: 0,
1010
});
11-
Notification.warning({
11+
NotificationPlugin.warning({
1212
title: '标题名称',
1313
content: '这是第二条通知',
1414
duration: 0,
1515
});
16-
Notification.error({
16+
NotificationPlugin.error({
1717
title: '标题名称',
1818
content: '这是第三条通知',
1919
duration: 0,
2020
});
2121
}, []);
2222

2323
const closeAll = React.useCallback(() => {
24-
Notification.closeAll();
24+
NotificationPlugin.closeAll();
2525
}, []);
2626

2727
return (
28-
<div className="tdesign-demo-block-row">
28+
<div className='tdesign-demo-block-row'>
2929
<Button onClick={openMore}>点击打开多个消息</Button>
3030
<Button onClick={closeAll}>点击关闭多个消息</Button>
3131
</div>

src/notification/_example/placement.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React, { useState } from 'react';
2-
import { Notification, Button, Input } from 'tdesign-react';
2+
import { NotificationPlugin, Button, Input } from 'tdesign-react';
33

44
export default function NotificationExample() {
55
const [offsetY, setOffsetY] = useState('');
66
const [offsetX, setOffsetX] = useState('');
77

88
const openNotification = React.useCallback(
99
(placement) => {
10-
Notification.info({
10+
NotificationPlugin.info({
1111
title: '标题名称',
1212
content: '这是一条可以自动关闭的消息通知',
1313
placement,

src/notification/_example/plugin.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import React from 'react';
2-
import { Notification, Button } from 'tdesign-react';
2+
import { NotificationPlugin, Button } from 'tdesign-react';
33

44
export default function NotificationExample() {
55
const openInfoNotification = React.useCallback(() => {
6-
Notification.info({
6+
NotificationPlugin.info({
77
title: '信息',
88
content: '这是一条可以自动关闭的消息通知',
99
duration: 3000,
1010
});
1111
}, []);
1212

1313
const openSuccessNotification = React.useCallback(() => {
14-
Notification.success({
14+
NotificationPlugin.success({
1515
title: '信息',
1616
content: '这是一条可以自动关闭的消息通知',
1717
duration: 3000,
1818
});
1919
}, []);
2020

2121
const openWarningNotification = React.useCallback(() => {
22-
Notification.warning({
22+
NotificationPlugin.warning({
2323
title: '信息',
2424
content: '这是一条可以自动关闭的消息通知',
2525
duration: 3000,
2626
});
2727
}, []);
2828

2929
const openErrorNotification = React.useCallback(() => {
30-
Notification.error({
30+
NotificationPlugin.error({
3131
title: '信息',
3232
content: '这是一条可以自动关闭的消息通知',
3333
duration: 3000,

src/notification/_example/toggle.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react';
2-
import { Notification, Button } from 'tdesign-react';
2+
import { NotificationPlugin, Button } from 'tdesign-react';
33

44
export default function NotificationExample() {
55
const openNotification = React.useCallback(() => {
6-
const notification = Notification.info({
6+
const notification = NotificationPlugin.info({
77
title: '信息',
88
content: '这是一条可以自动关闭的消息通知',
99
onCloseBtnClick: () => {
1010
notification.then((instance) => instance.close());
11-
Notification.close(notification);
11+
NotificationPlugin.close(notification);
1212
},
1313
});
1414
}, []);

src/notification/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ import './style/index.js';
55
export * from './type';
66

77
export const Notification = _Notification;
8+
9+
export { NotificationPlugin, notification } from './NotificationPlugin';
10+
811
export default Notification;

0 commit comments

Comments
 (0)