|
| 1 | +import React, { useEffect, useState, useCallback } from 'react'; |
| 2 | +import type { ToastType, ToastOptions, ToastProps, ToastInstance, ToastReturnType } from './types'; |
| 3 | +import Transition from '../transition'; |
| 4 | +import { Toast as BaseToast } from './toast'; |
| 5 | +import { isObject } from '../../utils'; |
| 6 | +import { render, unmount } from '../../utils/dom/render'; |
| 7 | +import { resolveContainer } from '../../utils/dom/getContainer'; |
| 8 | + |
| 9 | +const currentOptions: { [key: string]: ToastOptions } = {}; |
| 10 | + |
| 11 | +function parseOptions(message: string | ToastOptions): ToastOptions { |
| 12 | + return isObject(message) ? message : { message }; |
| 13 | +} |
| 14 | + |
| 15 | +const toastArray: (() => void)[] = []; |
| 16 | + |
| 17 | +// 同步的销毁 |
| 18 | +function syncClear() { |
| 19 | + let fn = toastArray.pop(); |
| 20 | + while (fn) { |
| 21 | + fn(); |
| 22 | + fn = toastArray.pop(); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// 针对 toast 还没弹出来就立刻销毁的情况,将销毁放到下一个 event loop 中,避免销毁失败。 |
| 27 | +function nextTickClear() { |
| 28 | + setTimeout(syncClear); |
| 29 | +} |
| 30 | + |
| 31 | +const show = (p: ToastProps | string) => { |
| 32 | + const props = parseOptions(p); |
| 33 | + const update: ToastReturnType = { |
| 34 | + config: () => {}, |
| 35 | + clear: () => null, |
| 36 | + }; |
| 37 | + // 创建父亲节点 |
| 38 | + const userContainer = resolveContainer(props.teleport); |
| 39 | + const container = document.createElement('div'); |
| 40 | + userContainer.appendChild(container); |
| 41 | + |
| 42 | + // 定时器 |
| 43 | + let timer = 0; |
| 44 | + |
| 45 | + const TempNotify = () => { |
| 46 | + const options = { |
| 47 | + ...props, |
| 48 | + }; |
| 49 | + const [state, setState] = useState<ToastProps>({ ...options }); |
| 50 | + const [visible, setVisible] = useState(false); |
| 51 | + |
| 52 | + const destroy = useCallback(() => { |
| 53 | + setVisible(false); |
| 54 | + if (props.onClose) props.onClose(); |
| 55 | + }, []); |
| 56 | + |
| 57 | + const internalOnClosed = useCallback(() => { |
| 58 | + const unmountResult = unmount(container); |
| 59 | + if (unmountResult && container.parentNode) { |
| 60 | + container.parentNode.removeChild(container); |
| 61 | + } |
| 62 | + }, [container]); |
| 63 | + |
| 64 | + update.clear = internalOnClosed; |
| 65 | + |
| 66 | + update.config = useCallback( |
| 67 | + nextState => { |
| 68 | + setState(prev => |
| 69 | + typeof nextState === 'function' |
| 70 | + ? { ...prev, ...nextState(prev) } |
| 71 | + : { ...prev, ...nextState } |
| 72 | + ); |
| 73 | + }, |
| 74 | + [setState] |
| 75 | + ); |
| 76 | + |
| 77 | + useEffect(() => { |
| 78 | + setVisible(true); |
| 79 | + syncClear(); |
| 80 | + toastArray.push(internalOnClosed); |
| 81 | + |
| 82 | + if (state.duration && +state.duration > 0) { |
| 83 | + timer = window.setTimeout(destroy, state.duration); |
| 84 | + } |
| 85 | + |
| 86 | + return () => { |
| 87 | + if (timer !== 0) { |
| 88 | + window.clearTimeout(timer); |
| 89 | + } |
| 90 | + }; |
| 91 | + }, []); |
| 92 | + |
| 93 | + return ( |
| 94 | + <Transition in={visible} onEntered={props.onOpened} onExited={props.onClose}> |
| 95 | + <BaseToast {...state} /> |
| 96 | + </Transition> |
| 97 | + ); |
| 98 | + }; |
| 99 | + |
| 100 | + render(<TempNotify />, container); |
| 101 | + |
| 102 | + return update; |
| 103 | +}; |
| 104 | + |
| 105 | +const defaultOptions: ToastOptions = { |
| 106 | + message: '', |
| 107 | + duration: 3000, |
| 108 | + direction: 'vertical', |
| 109 | + loadingType: 'gap', |
| 110 | + position: 'center', |
| 111 | +}; |
| 112 | + |
| 113 | +['info', 'loading', 'success', 'fail'].forEach(method => { |
| 114 | + currentOptions[method] = defaultOptions; |
| 115 | +}); |
| 116 | + |
| 117 | +const setDefaultOptions = (type: ToastType, options: ToastOptions) => { |
| 118 | + currentOptions[type] = Object.assign(currentOptions[type], options); |
| 119 | +}; |
| 120 | + |
| 121 | +// 重置配置 |
| 122 | +const resetDefaultOptions = (type: ToastType) => { |
| 123 | + currentOptions[type] = { ...defaultOptions }; |
| 124 | +}; |
| 125 | + |
| 126 | +const clear = nextTickClear; |
| 127 | + |
| 128 | +const ToastDefault = (options: ToastProps | string) => { |
| 129 | + let type: ToastType = 'info'; |
| 130 | + if (typeof options !== 'string') { |
| 131 | + type = options.type || 'info'; |
| 132 | + } |
| 133 | + return show({ |
| 134 | + type: type, |
| 135 | + ...currentOptions[type], |
| 136 | + ...parseOptions(options), |
| 137 | + }); |
| 138 | +}; |
| 139 | + |
| 140 | +const info = (options: ToastOptions | string) => |
| 141 | + show({ |
| 142 | + ...currentOptions['info'], |
| 143 | + ...parseOptions(options), |
| 144 | + type: 'info', |
| 145 | + }); |
| 146 | + |
| 147 | +const fail = (options: ToastOptions | string) => |
| 148 | + show({ |
| 149 | + ...currentOptions['fail'], |
| 150 | + ...parseOptions(options), |
| 151 | + type: 'fail', |
| 152 | + }); |
| 153 | + |
| 154 | +const success = (options: ToastOptions | string) => |
| 155 | + show({ |
| 156 | + ...currentOptions['success'], |
| 157 | + ...parseOptions(options), |
| 158 | + type: 'success', |
| 159 | + }); |
| 160 | + |
| 161 | +const loading = (options: ToastOptions | string) => |
| 162 | + show({ |
| 163 | + ...currentOptions['loading'], |
| 164 | + ...parseOptions(options), |
| 165 | + type: 'loading', |
| 166 | + }); |
| 167 | + |
| 168 | +const ToastController: ToastInstance = Object.assign(ToastDefault, { |
| 169 | + setDefaultOptions, |
| 170 | + resetDefaultOptions, |
| 171 | + clear, |
| 172 | + info, |
| 173 | + fail, |
| 174 | + loading, |
| 175 | + success, |
| 176 | +}); |
| 177 | + |
| 178 | +export default ToastController; |
0 commit comments