UNPKG

@fruits-chain/react-native-xiaoshu

Version:
101 lines (95 loc) 2.34 kB
"use strict"; import React, { createRef } from 'react'; import Portal from "../portal/index.js"; import Toast from "./toast.js"; import { jsx as _jsx } from "react/jsx-runtime"; const parseOptions = message => { if (typeof message === 'object') { return message; } return { message }; }; const defaultOptions = { type: 'text', duration: 2000, message: '', position: 'middle', forbidPress: false, closeOnPressOverlay: false, overlay: false, loadingType: 'spinner' }; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions let defaultOptionsMap = {}; let currentOptions = { ...defaultOptions }; export const Instance = options => { let opts = typeof options === 'string' ? { message: options } : options; const type = opts.type || currentOptions.type; // 合并参数 opts = { ...currentOptions, ...(type ? defaultOptionsMap[type] : {}), ...opts }; const ToastRef = /*#__PURE__*/createRef(); const key = Portal.add(/*#__PURE__*/_jsx(Toast, { ...opts, ref: ToastRef, onClosed: () => { Portal.remove(key); opts.onClosed?.(); } })); // TODO 优化调用方法 return { close: () => { ToastRef.current?.close(); }, setMessage: m => { ToastRef.current?.setMessage(m); } }; }; export const loading = options => Instance({ type: 'loading', ...parseOptions(options) }); export const success = options => Instance({ type: 'success', ...parseOptions(options) }); export const fail = options => Instance({ type: 'fail', ...parseOptions(options) }); /** * 修改默认配置,对所有 Toast 生效。传入 type 可以修改指定类型的默认配置 */ export const setDefaultOptions = (type, options) => { if (typeof type === 'string') { defaultOptionsMap[type] = options; } else { Object.assign(currentOptions, type); } }; /** * 重置默认配置,对所有 Toast 生效。传入 type 可以重置指定类型的默认配置 */ export const resetDefaultOptions = type => { if (typeof type === 'string') { defaultOptionsMap[type] = null; } else { currentOptions = { ...defaultOptions }; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions defaultOptionsMap = {}; } }; //# sourceMappingURL=toast-instance.js.map