import React, { ReactNode, useEffect, useRef } from 'react'; import classNames from 'classnames'; import Icon from '../icon'; import { BaseProps } from '../_utils/props'; export type NotificationType = 'success' | 'error' | 'warning' | 'info' | undefined; export interface NotificationProps extends BaseProps { type: NotificationType; title?: ReactNode; description?: ReactNode; footer?: ReactNode; onClick?: (e: React.MouseEvent) => void; onClose?: (e: React.MouseEvent) => void; icon?: ReactNode | boolean; willUnmount: (height: number) => void; didMount: () => void; duration?: number; children?: React.ReactNode; } const IconType: { [type: string]: { name: string; color: string } } = { success: { name: 'check-fill', color: '#52c41a' }, info: { name: 'info-fill', color: '#1890ff' }, warning: { name: 'warn-fill', color: '#faad14' }, error: { name: 'close-fill', color: '#f5222d' }, }; const Notification = (props: NotificationProps) => { const { prefixCls = 'ty-notification', duration = 4500, title, description, footer, type, icon, willUnmount, didMount, onClick, onClose, className, style, } = props; const cls = classNames(prefixCls, className); const ref = useRef(null); let timer: number | null = null; let height = 0; const closeBtnOnClick = (e: React.MouseEvent) => { timer && clearTimeout(timer); willUnmount(height); onClose && onClose(e); }; const renderIcon = () => { if (React.isValidElement(icon)) { return icon; } else if (typeof type === 'string') { return ( ); } return null; }; useEffect(() => { didMount(); height = (ref.current && ref.current!.offsetHeight) || 0; if (duration !== 0) { timer = window.setTimeout(() => { willUnmount(height); }, duration); } }, []); return (
{renderIcon()}
{title}
{description} {footer}
); }; export default Notification;