2023-09-11 17:28:04 +08:00
|
|
|
import type { ReactElement } from 'react';
|
|
|
|
import * as React from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
2020-03-02 12:09:38 +08:00
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
|
|
|
import InfoCircleFilled from '@ant-design/icons/InfoCircleFilled';
|
2015-11-29 16:55:43 +08:00
|
|
|
import classNames from 'classnames';
|
2022-06-22 14:57:09 +08:00
|
|
|
import CSSMotion from 'rc-motion';
|
2024-05-14 13:56:18 +08:00
|
|
|
import { composeRef } from 'rc-util/lib/ref';
|
2023-05-28 21:32:45 +08:00
|
|
|
import pickAttrs from 'rc-util/lib/pickAttrs';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2024-04-08 14:04:08 +08:00
|
|
|
import type { ClosableType } from '../_util/hooks/useClosable';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { replaceElement } from '../_util/reactNode';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-05-28 21:32:45 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-03-04 19:09:55 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
2024-04-09 10:24:17 +08:00
|
|
|
export interface AlertRef {
|
|
|
|
nativeElement: HTMLDivElement;
|
|
|
|
}
|
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface AlertProps {
|
2020-12-28 15:30:18 +08:00
|
|
|
/** Type of Alert styles, options:`success`, `info`, `warning`, `error` */
|
2016-08-03 14:54:10 +08:00
|
|
|
type?: 'success' | 'info' | 'warning' | 'error';
|
2016-07-14 13:29:50 +08:00
|
|
|
/** Whether Alert can be closed */
|
2024-03-06 11:53:05 +08:00
|
|
|
closable?: ClosableType;
|
2023-06-13 14:38:18 +08:00
|
|
|
/**
|
2024-02-19 13:40:19 +08:00
|
|
|
* @deprecated please use `closable.closeIcon` instead.
|
2023-06-13 14:38:18 +08:00
|
|
|
* Close text to show
|
|
|
|
*/
|
2016-07-14 13:29:50 +08:00
|
|
|
closeText?: React.ReactNode;
|
|
|
|
/** Content of Alert */
|
2021-09-07 13:07:41 +08:00
|
|
|
message?: React.ReactNode;
|
2016-07-14 13:29:50 +08:00
|
|
|
/** Additional content of Alert */
|
|
|
|
description?: React.ReactNode;
|
|
|
|
/** Callback when close Alert */
|
2019-09-16 11:23:26 +08:00
|
|
|
onClose?: React.MouseEventHandler<HTMLButtonElement>;
|
2018-03-09 15:02:13 +08:00
|
|
|
/** Trigger when animation ending of Alert */
|
2018-03-13 11:40:11 +08:00
|
|
|
afterClose?: () => void;
|
2016-07-14 13:29:50 +08:00
|
|
|
/** Whether to show icon */
|
|
|
|
showIcon?: boolean;
|
2020-04-22 18:16:03 +08:00
|
|
|
/** https://www.w3.org/TR/2014/REC-html5-20141028/dom.html#aria-role-attribute */
|
|
|
|
role?: string;
|
2016-07-14 13:29:50 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
prefixCls?: string;
|
2016-10-28 16:42:26 +08:00
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2016-08-03 14:54:10 +08:00
|
|
|
banner?: boolean;
|
2018-09-14 23:54:54 +08:00
|
|
|
icon?: React.ReactNode;
|
2024-01-11 15:11:55 +08:00
|
|
|
closeIcon?: React.ReactNode;
|
2020-11-30 11:04:56 +08:00
|
|
|
action?: React.ReactNode;
|
2020-03-25 16:50:43 +08:00
|
|
|
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
|
|
|
|
onMouseLeave?: React.MouseEventHandler<HTMLDivElement>;
|
|
|
|
onClick?: React.MouseEventHandler<HTMLDivElement>;
|
2024-04-09 10:24:17 +08:00
|
|
|
|
|
|
|
id?: string;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
const iconMapFilled = {
|
|
|
|
success: CheckCircleFilled,
|
|
|
|
info: InfoCircleFilled,
|
|
|
|
error: CloseCircleFilled,
|
|
|
|
warning: ExclamationCircleFilled,
|
|
|
|
};
|
2019-08-02 22:50:11 +08:00
|
|
|
|
2022-06-22 21:01:00 +08:00
|
|
|
interface IconNodeProps {
|
|
|
|
type: AlertProps['type'];
|
|
|
|
icon: AlertProps['icon'];
|
|
|
|
prefixCls: AlertProps['prefixCls'];
|
|
|
|
description: AlertProps['description'];
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const IconNode: React.FC<IconNodeProps> = (props) => {
|
2022-09-08 09:51:32 +08:00
|
|
|
const { icon, prefixCls, type } = props;
|
|
|
|
const iconType = iconMapFilled[type!] || null;
|
2022-06-22 21:01:00 +08:00
|
|
|
if (icon) {
|
|
|
|
return replaceElement(icon, <span className={`${prefixCls}-icon`}>{icon}</span>, () => ({
|
|
|
|
className: classNames(`${prefixCls}-icon`, {
|
|
|
|
[(icon as ReactElement).props.className]: (icon as ReactElement).props.className,
|
|
|
|
}),
|
|
|
|
})) as ReactElement;
|
|
|
|
}
|
|
|
|
return React.createElement(iconType, { className: `${prefixCls}-icon` });
|
|
|
|
};
|
|
|
|
|
2024-02-19 13:40:19 +08:00
|
|
|
type CloseIconProps = {
|
2022-06-22 21:01:00 +08:00
|
|
|
isClosable: boolean;
|
|
|
|
prefixCls: AlertProps['prefixCls'];
|
|
|
|
closeIcon: AlertProps['closeIcon'];
|
|
|
|
handleClose: AlertProps['onClose'];
|
2024-02-19 13:40:19 +08:00
|
|
|
ariaProps: React.AriaAttributes;
|
|
|
|
};
|
2022-06-22 21:01:00 +08:00
|
|
|
|
2024-01-30 16:47:42 +08:00
|
|
|
const CloseIconNode: React.FC<CloseIconProps> = (props) => {
|
2024-02-19 13:40:19 +08:00
|
|
|
const { isClosable, prefixCls, closeIcon, handleClose, ariaProps } = props;
|
2023-06-13 14:38:18 +08:00
|
|
|
const mergedCloseIcon =
|
|
|
|
closeIcon === true || closeIcon === undefined ? <CloseOutlined /> : closeIcon;
|
2022-06-22 21:01:00 +08:00
|
|
|
return isClosable ? (
|
2024-02-19 13:40:19 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={handleClose}
|
|
|
|
className={`${prefixCls}-close-icon`}
|
|
|
|
tabIndex={0}
|
|
|
|
{...ariaProps}
|
|
|
|
>
|
2023-06-13 14:38:18 +08:00
|
|
|
{mergedCloseIcon}
|
2022-06-22 21:01:00 +08:00
|
|
|
</button>
|
|
|
|
) : null;
|
|
|
|
};
|
|
|
|
|
2024-04-09 10:24:17 +08:00
|
|
|
const Alert = React.forwardRef<AlertRef, AlertProps>((props, ref) => {
|
2023-07-11 23:01:17 +08:00
|
|
|
const {
|
|
|
|
description,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
message,
|
|
|
|
banner,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
style,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
|
|
|
onClick,
|
|
|
|
afterClose,
|
|
|
|
showIcon,
|
|
|
|
closable,
|
|
|
|
closeText,
|
|
|
|
closeIcon,
|
|
|
|
action,
|
2024-04-09 10:24:17 +08:00
|
|
|
id,
|
2023-07-11 23:01:17 +08:00
|
|
|
...otherProps
|
|
|
|
} = props;
|
|
|
|
|
2020-05-19 20:18:13 +08:00
|
|
|
const [closed, setClosed] = React.useState(false);
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2023-06-13 14:38:18 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Alert');
|
2024-02-19 13:40:19 +08:00
|
|
|
warning.deprecated(!closeText, 'closeText', 'closable.closeIcon');
|
2023-06-13 14:38:18 +08:00
|
|
|
}
|
2023-09-13 22:07:33 +08:00
|
|
|
|
2024-04-09 10:24:17 +08:00
|
|
|
const internalRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
|
|
nativeElement: internalRef.current!,
|
|
|
|
}));
|
|
|
|
|
2023-07-06 09:33:54 +08:00
|
|
|
const { getPrefixCls, direction, alert } = React.useContext(ConfigContext);
|
2020-05-19 20:18:13 +08:00
|
|
|
const prefixCls = getPrefixCls('alert', customizePrefixCls);
|
2023-11-13 17:12:17 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2020-05-19 20:18:13 +08:00
|
|
|
|
|
|
|
const handleClose = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
setClosed(true);
|
2020-08-11 17:04:42 +08:00
|
|
|
props.onClose?.(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-01 06:37:14 +08:00
|
|
|
|
2023-07-11 23:01:17 +08:00
|
|
|
const type = React.useMemo<AlertProps['type']>(() => {
|
|
|
|
if (props.type !== undefined) {
|
|
|
|
return props.type;
|
2020-05-09 19:32:02 +08:00
|
|
|
}
|
2023-04-07 23:27:50 +08:00
|
|
|
// banner mode defaults to 'warning'
|
2020-05-09 19:32:02 +08:00
|
|
|
return banner ? 'warning' : 'info';
|
2023-07-11 23:01:17 +08:00
|
|
|
}, [props.type, banner]);
|
2020-05-09 19:32:02 +08:00
|
|
|
|
2023-06-13 14:38:18 +08:00
|
|
|
// closeable when closeText or closeIcon is assigned
|
2024-01-30 16:47:42 +08:00
|
|
|
const isClosable = React.useMemo<boolean>(() => {
|
2024-02-19 13:40:19 +08:00
|
|
|
if (typeof closable === 'object' && closable.closeIcon) return true;
|
2023-06-13 14:38:18 +08:00
|
|
|
if (closeText) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (typeof closable === 'boolean') {
|
|
|
|
return closable;
|
|
|
|
}
|
|
|
|
// should be true when closeIcon is 0 or ''
|
2024-02-19 13:40:19 +08:00
|
|
|
if (closeIcon !== false && closeIcon !== null && closeIcon !== undefined) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!alert?.closable;
|
|
|
|
}, [closeText, closeIcon, closable, alert?.closable]);
|
2023-06-13 14:38:18 +08:00
|
|
|
|
2023-04-07 23:27:50 +08:00
|
|
|
// banner mode defaults to Icon
|
2020-05-19 20:18:13 +08:00
|
|
|
const isShowIcon = banner && showIcon === undefined ? true : showIcon;
|
|
|
|
|
|
|
|
const alertCls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
`${prefixCls}-${type}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-with-description`]: !!description,
|
|
|
|
[`${prefixCls}-no-icon`]: !isShowIcon,
|
|
|
|
[`${prefixCls}-banner`]: !!banner,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
2023-07-06 09:33:54 +08:00
|
|
|
alert?.className,
|
2020-05-19 20:18:13 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2022-03-04 19:09:55 +08:00
|
|
|
hashId,
|
2020-05-19 20:18:13 +08:00
|
|
|
);
|
|
|
|
|
2023-07-11 23:01:17 +08:00
|
|
|
const restProps = pickAttrs(otherProps, { aria: true, data: true });
|
2020-05-19 20:18:13 +08:00
|
|
|
|
2024-02-19 13:40:19 +08:00
|
|
|
const mergedCloseIcon = React.useMemo(() => {
|
|
|
|
if (typeof closable === 'object' && closable.closeIcon) {
|
|
|
|
return closable.closeIcon;
|
|
|
|
}
|
|
|
|
if (closeText) {
|
|
|
|
return closeText;
|
|
|
|
}
|
|
|
|
if (closeIcon !== undefined) {
|
|
|
|
return closeIcon;
|
|
|
|
}
|
|
|
|
if (typeof alert?.closable === 'object' && alert?.closable?.closeIcon) {
|
|
|
|
return alert?.closable?.closeIcon;
|
|
|
|
}
|
|
|
|
return alert?.closeIcon;
|
|
|
|
}, [closeIcon, closable, closeText, alert?.closeIcon]);
|
|
|
|
|
2024-03-24 10:56:28 +08:00
|
|
|
const mergedAriaProps = React.useMemo<React.AriaAttributes>(() => {
|
2024-02-19 13:40:19 +08:00
|
|
|
const merged = closable ?? alert?.closable;
|
|
|
|
if (typeof merged === 'object') {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
const { closeIcon: _, ...ariaProps } = merged;
|
|
|
|
return ariaProps;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}, [closable, alert?.closable]);
|
|
|
|
|
2023-11-13 17:12:17 +08:00
|
|
|
return wrapCSSVar(
|
2020-08-11 17:04:42 +08:00
|
|
|
<CSSMotion
|
|
|
|
visible={!closed}
|
|
|
|
motionName={`${prefixCls}-motion`}
|
|
|
|
motionAppear={false}
|
|
|
|
motionEnter={false}
|
2023-07-11 23:01:17 +08:00
|
|
|
onLeaveStart={(node) => ({ maxHeight: node.offsetHeight })}
|
2020-08-11 17:04:42 +08:00
|
|
|
onLeaveEnd={afterClose}
|
2020-05-19 20:18:13 +08:00
|
|
|
>
|
2024-05-10 19:18:54 +08:00
|
|
|
{({ className: motionClassName, style: motionStyle }, setRef) => (
|
2020-08-11 17:04:42 +08:00
|
|
|
<div
|
2024-04-09 10:24:17 +08:00
|
|
|
id={id}
|
2024-05-10 19:18:54 +08:00
|
|
|
ref={composeRef(internalRef, setRef)}
|
2020-08-11 17:04:42 +08:00
|
|
|
data-show={!closed}
|
|
|
|
className={classNames(alertCls, motionClassName)}
|
2023-07-06 09:33:54 +08:00
|
|
|
style={{ ...alert?.style, ...style, ...motionStyle }}
|
2020-08-11 17:04:42 +08:00
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
onClick={onClick}
|
|
|
|
role="alert"
|
2023-07-11 23:01:17 +08:00
|
|
|
{...restProps}
|
2020-08-11 17:04:42 +08:00
|
|
|
>
|
2022-06-22 21:01:00 +08:00
|
|
|
{isShowIcon ? (
|
|
|
|
<IconNode
|
|
|
|
description={description}
|
|
|
|
icon={props.icon}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
type={type}
|
|
|
|
/>
|
|
|
|
) : null}
|
2020-11-30 11:04:56 +08:00
|
|
|
<div className={`${prefixCls}-content`}>
|
2021-09-07 13:07:41 +08:00
|
|
|
{message ? <div className={`${prefixCls}-message`}>{message}</div> : null}
|
|
|
|
{description ? <div className={`${prefixCls}-description`}>{description}</div> : null}
|
2020-11-30 11:04:56 +08:00
|
|
|
</div>
|
|
|
|
{action ? <div className={`${prefixCls}-action`}>{action}</div> : null}
|
2024-01-30 16:47:42 +08:00
|
|
|
<CloseIconNode
|
2023-06-13 14:38:18 +08:00
|
|
|
isClosable={isClosable}
|
2022-06-22 21:01:00 +08:00
|
|
|
prefixCls={prefixCls}
|
2024-02-19 13:40:19 +08:00
|
|
|
closeIcon={mergedCloseIcon}
|
2022-06-22 21:01:00 +08:00
|
|
|
handleClose={handleClose}
|
2024-03-24 10:56:28 +08:00
|
|
|
ariaProps={mergedAriaProps}
|
2022-06-22 21:01:00 +08:00
|
|
|
/>
|
2020-08-11 17:04:42 +08:00
|
|
|
</div>
|
|
|
|
)}
|
2022-03-04 19:09:55 +08:00
|
|
|
</CSSMotion>,
|
2020-05-19 20:18:13 +08:00
|
|
|
);
|
2024-04-09 10:24:17 +08:00
|
|
|
});
|
2020-05-19 20:18:13 +08:00
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Alert.displayName = 'Alert';
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:18:13 +08:00
|
|
|
export default Alert;
|