-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.tsx
45 lines (42 loc) · 1.32 KB
/
index.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
import React from 'react';
import { uniqKey } from '../utils/uniqKey';
import { createPortalDOM, PortalDOM } from '../utils/dom';
import { Toast as ToastComponent, ToastProps } from './Toast';
/**
* 显示一个全局的轻提示,这个toast不是唯一的
* @param option 可以是一个字符串,也可以是一个React组件
*/
export function showToast(option: React.ReactNode | ToastProps) {
const { mount, unmount } = createPortalDOM();
let props: ToastProps = {};
if (React.isValidElement(option) || typeof option !== 'object') {
props.content = option;
} else {
props = option as ToastProps;
}
props.onHide = unmount;
mount(<ToastComponent {...props} />);
}
/**
* 生成一个全局唯一的Toast
* @param option
*/
let portalDOM: PortalDOM | null = null;
export function showUniqToast(option: React.ReactNode | ToastProps) {
if (!portalDOM) {
portalDOM = createPortalDOM();
}
let props: ToastProps = {};
// 默认Toast是唯一的
if (React.isValidElement(option) || typeof option !== 'object') {
props.content = option;
} else {
props = option as ToastProps;
}
const onHide = () => {
portalDOM?.unmount();
portalDOM = null;
};
props.onHide = onHide;
portalDOM.mount(<ToastComponent {...props} key={uniqKey()} />);
}