UNPKG

antd-mobile-taro-ui

Version:

以antd-mobile为设计标准,基于taro框架的微信小程序组件库

59 lines (51 loc) 1.46 kB
import React from 'react'; import { mergeProps } from 'antd-mobile/es/utils/with-default-props'; import { renderImperatively } from '../../utils/render-imperatively'; import { InternalToast } from './toast'; let currentHandler = null; let currentTimeout; const defaultProps = { duration: 2000, position: 'center', maskClickable: true }; const ToastInner = props => React.createElement(InternalToast, Object.assign({}, props)); export function show(p) { const props = mergeProps(defaultProps, typeof p === 'string' ? { content: p } : p); const element = React.createElement(ToastInner, Object.assign({}, props, { onClose: () => { currentHandler = null; } })); if (currentHandler) { currentHandler.replace(element); } else { currentHandler = renderImperatively(element); } if (currentTimeout) { clearTimeout(currentTimeout); } if (props.duration !== 0) { currentTimeout = setTimeout(() => { clear(); }, props.duration); } return currentHandler; } export function clear() { currentHandler === null || currentHandler === void 0 ? void 0 : currentHandler.close(); currentHandler = null; } export function config(val) { if (val.duration !== undefined) { defaultProps.duration = val.duration; } if (val.position !== undefined) { defaultProps.position = val.position; } if (val.maskClickable !== undefined) { defaultProps.maskClickable = val.maskClickable; } }