import React, { FC, useEffect } from 'react' import _ from 'lodash' import { TipProps, TipStatic } from './types' import { Flex } from '../flex' import { LayoutRoot } from '../layout_root' import SVGSuccess from '../../svg/success-circle.svg' import SVGDanger from '../../svg/close-circle.svg' import SVGRemove from '../../svg/remove.svg' const Tip: FC & TipStatic = ({ type, time = 3000, onClose = _.noop, children, }) => { useEffect(() => { let timer: any if (time) { timer = setTimeout(() => { onClose() }, time) } return () => { if (timer) { clearTimeout(timer) } } }, []) return ( {type === 'success' && } {type === 'danger' && } {children} {!time && ( { onClose() }} > )} ) } Tip.tip = (options, type) => { if (typeof options === 'string') { options = { children: options, } } options.type = type const id = +new Date() + '' + Math.random() const _onClose = options.onClose options.onClose = () => { LayoutRoot.removeComponentArray(LayoutRoot.Type.TIP, id) if (_onClose) { _onClose() } } LayoutRoot.setComponentArray(LayoutRoot.Type.TIP, id, ) return id } Tip.success = (options) => { return Tip.tip(options, 'success') } Tip.danger = (options) => { return Tip.tip(options, 'danger') } Tip.clear = (id) => { LayoutRoot.removeComponentArray(LayoutRoot.Type.TIP, id) } Tip.clearAll = () => { LayoutRoot.clearComponentArray(LayoutRoot.Type.TIP) } export default Tip