-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (89 loc) · 1.97 KB
/
index.js
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
import React from "react";
import Portal from "@huxin957/react-native-portal";
import ToastContainer from "./ToastContainer";
const SHORT = 1;
const toastKeyMap = {};//记录数量,便于删除
const defaultProps = {
duration: SHORT,
onClose: () => null,//关闭后的回调
mask: true,//是否显示遮罩层,防止触摸
stackable: true,//是否允许重叠显示
position: 'center',//位置
positionValue: 100,
};
function remove(key) {
Portal.remove(key);
delete toastKeyMap[key];
}
function removeAll() {
Object.keys(toastKeyMap).forEach(key => {
Portal.remove(Number.parseInt(key, 10));
});
}
/**
* @param content
* @param type
* @param duration
* @param onClose
* @returns {number}
*/
function notice(
content,
type,
duration = defaultProps.duration,
onClose = defaultProps.onClose,
) {
let props = {
...defaultProps,
content,
type,
duration,
onClose,
};
if (typeof content !== "string") {
props = {
...props,
...content,
};
}
if (!props.stackable) {
removeAll();
}
const key = Portal.add(
<ToastContainer
content={props.content}
duration={props.duration}
onClose={props.onClose}
type={props.type}
mask={props.mask}
errorCode={props.errorCode}
position={props.position}
positionValue={props.positionValue}
onAnimationEnd={() => {
remove(key);
}}
/>,
);
toastKeyMap[key] = 1;
return key;
}
export default class Toast {
static show(props, duration, onClose) {
return notice(props, "show", duration, onClose);
}
static success(props, duration, onClose) {
return notice(props, "success", duration, onClose);
}
static error(props, duration, onClose) {
return notice(props, "error", duration, onClose);
}
static loading(props, duration, onClose) {
return notice(props, "loading", 12, onClose);
}
static remove(key) {
remove(key)
}
static removeAll() {
removeAll()
}
}