import { forwardRef, useMemo, useRef } from 'react'; import cx from 'classnames'; import { AlertTypes } from '../types'; import Icon, { IconType } from '../../icon'; import InlineLoading from '../../loading/InlineLoading'; import { IAlertProps } from '../Alert'; const iconTypeMap: { [key in Exclude]: IconType; } = { info: 'info-circle', warning: 'warning', success: 'check-circle', error: 'close-circle', }; type IAlertItemProps = Omit< IAlertProps, 'outline' | 'closed' | 'background' > & { onAlertItemClose?: () => void; classItemName?: string; }; export const AlertItem = forwardRef( (props, ref) => { const { extraContent, classItemName, title, description, children, loading, type, closable, icon, closeIconColor, closeContent, onAlertItemClose, } = props; const propsRef = useRef(props); propsRef.current = props; const renderContent = useMemo(() => { return children ? ( children ) : ( <> {title &&

{title}

} {description && (

{description}

)} ); }, [children, description, title]); const renderCloseNode = useMemo(() => { const { onClose } = propsRef.current; const colorStyle = closeIconColor ? { color: closeIconColor } : {}; return closable ? (
{ onClose?.(); onAlertItemClose && onAlertItemClose(); e.stopPropagation(); }} > {closeContent ?? ( )}
) : null; }, [closable, closeContent, closeIconColor, onAlertItemClose]); const renderIcon = useMemo(() => { if (loading) { return ( ); } if (icon === null || icon === false) return null; if (icon) { return {icon}; } if (type in iconTypeMap) { return ( ); } return null; }, [loading, type, icon]); return (
{renderIcon}
{renderContent}
{extraContent && (
{extraContent}
)} {renderCloseNode}
); } ); AlertItem.displayName = 'AlertItem'; export default AlertItem;