Toast.js
1.09 KB
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 RootSiblings from 'react-native-root-siblings'
import ToastView from "./ToastView"
import {ToastDuration,ToastInOutDuration,ToastPosition,ToastDefaultOpt} from '../data/Constants'
let rootSiblings = undefined
let liftCycleManage = undefined
export default class Toast {
static duration = ToastDuration
static position = ToastPosition
static show = (toastOpts) => {
const data = toastOpts.data
if (typeof data !== 'string' || data.length <= 0) {
return
}
toastOpts = Object.assign({},ToastDefaultOpt,toastOpts)
Toast.hide();
rootSiblings = new RootSiblings(
<ToastView {...toastOpts}/>
)
liftCycleManage = setTimeout(()=>{
Toast.hide()
}, toastOpts.duration+ToastInOutDuration*2)
}
static hide = () => {
if (liftCycleManage) {
clearTimeout(liftCycleManage)
liftCycleManage = undefined
}
if (rootSiblings) {
rootSiblings.destroy()
rootSiblings = undefined
}
}
}