import React, { useState, useRef } from 'react'; import classNames from 'classnames'; import { CSSTransition } from 'react-transition-group'; import Icon from '../icon'; import { BaseProps } from '../_utils/props'; export type AlertType = 'success' | 'info' | 'warning' | 'error'; export interface AlertProps extends BaseProps { title?: string; type?: AlertType; icon?: boolean | React.ReactNode; iconSize?: number; /** Whether Alert can be closed */ closable?: boolean; /** Close text to show */ closeText?: React.ReactNode; /** Trigger when animation ending of Alert */ afterClose?: () => void; onClose?: React.MouseEventHandler; children?: React.ReactNode; } const IconType = Object.freeze({ success: 'check-fill', info: 'info-fill', warning: 'warn-fill', error: 'close-fill', }); const setClosedStyle = (node: HTMLElement) => { node.style.borderTopWidth = '0'; node.style.paddingTop = '0'; node.style.marginTop = '0'; node.style.height = '0'; node.style.paddingBottom = '0'; node.style.borderBottomWidth = '0'; node.style.marginBottom = '0'; }; const Alert = (props: AlertProps) => { const { prefixCls = 'ty-alert', type = 'info', iconSize = 14, title, icon, closeText, closable, afterClose, onClose, children, className, style, } = props; const [isShow, setShow] = useState(true); const ref = useRef(null); const cls = classNames(prefixCls, className, [`${prefixCls}_${type}`]); const closeBtnOnClick = (e: React.MouseEvent) => { ref.current && setClosedStyle(ref.current!); setShow(false); onClose && onClose(e); }; // Setting close text attribute also allows to be closable const closeIcon = (closable || closeText) && ( {closeText || '✕'} ); const renderIcon = (): React.ReactNode => { if (typeof icon === 'boolean') { return ; } return icon; }; return (
{icon && renderIcon()}
{title &&

{title}

} {children}
{closeIcon}
); }; export default Alert;