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';
|
2022-06-22 21:01:00 +08:00
|
|
|
import type { ReactElement } from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2020-05-19 20:18:13 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2018-06-15 03:08:51 +08:00
|
|
|
import getDataOrAriaProps from '../_util/getDataOrAriaProps';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { replaceElement } from '../_util/reactNode';
|
2022-06-22 14:57:09 +08:00
|
|
|
import ErrorBoundary from './ErrorBoundary';
|
2015-07-27 20:53:08 +08:00
|
|
|
|
2022-03-04 19:09:55 +08:00
|
|
|
// CSSINJS
|
|
|
|
import useStyle from './style';
|
|
|
|
|
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 */
|
|
|
|
closable?: boolean;
|
|
|
|
/** Close text to show */
|
|
|
|
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;
|
2016-08-03 14:54:10 +08:00
|
|
|
banner?: boolean;
|
2018-09-14 23:54:54 +08:00
|
|
|
icon?: React.ReactNode;
|
2022-09-30 16:59:29 +08:00
|
|
|
/** Custom closeIcon */
|
2021-10-13 18:58:30 +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>;
|
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` });
|
|
|
|
};
|
|
|
|
|
|
|
|
interface CloseIconProps {
|
|
|
|
isClosable: boolean;
|
|
|
|
prefixCls: AlertProps['prefixCls'];
|
|
|
|
closeText: AlertProps['closeText'];
|
|
|
|
closeIcon: AlertProps['closeIcon'];
|
|
|
|
handleClose: AlertProps['onClose'];
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const CloseIcon: React.FC<CloseIconProps> = (props) => {
|
2022-06-22 21:01:00 +08:00
|
|
|
const { isClosable, closeText, prefixCls, closeIcon, handleClose } = props;
|
|
|
|
return isClosable ? (
|
|
|
|
<button type="button" onClick={handleClose} className={`${prefixCls}-close-icon`} tabIndex={0}>
|
|
|
|
{closeText ? <span className={`${prefixCls}-close-text`}>{closeText}</span> : closeIcon}
|
|
|
|
</button>
|
|
|
|
) : null;
|
|
|
|
};
|
|
|
|
|
2022-11-19 16:56:23 +08:00
|
|
|
type CompoundedComponent = React.FC<AlertProps> & {
|
2020-05-19 20:18:13 +08:00
|
|
|
ErrorBoundary: typeof ErrorBoundary;
|
2022-11-19 16:56:23 +08:00
|
|
|
};
|
2018-11-01 06:37:14 +08:00
|
|
|
|
2022-11-19 16:56:23 +08:00
|
|
|
const Alert: CompoundedComponent = ({
|
2020-05-19 20:18:13 +08:00
|
|
|
description,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
message,
|
|
|
|
banner,
|
|
|
|
className = '',
|
|
|
|
style,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
|
|
|
onClick,
|
2020-08-11 17:04:42 +08:00
|
|
|
afterClose,
|
2020-05-19 20:18:13 +08:00
|
|
|
showIcon,
|
|
|
|
closable,
|
|
|
|
closeText,
|
2021-10-13 18:58:30 +08:00
|
|
|
closeIcon = <CloseOutlined />,
|
2020-11-30 11:04:56 +08:00
|
|
|
action,
|
2020-05-19 20:18:13 +08:00
|
|
|
...props
|
|
|
|
}) => {
|
|
|
|
const [closed, setClosed] = React.useState(false);
|
|
|
|
|
|
|
|
const ref = React.useRef<HTMLElement>();
|
2022-04-07 21:29:50 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-05-19 20:18:13 +08:00
|
|
|
const prefixCls = getPrefixCls('alert', customizePrefixCls);
|
2022-04-07 21:29:50 +08:00
|
|
|
const [wrapSSR, hashId] = 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
|
|
|
|
2020-05-19 20:18:13 +08:00
|
|
|
const getType = () => {
|
|
|
|
const { type } = props;
|
2020-05-09 19:32:02 +08:00
|
|
|
if (type !== undefined) {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
// banner 模式默认为警告
|
|
|
|
return banner ? 'warning' : 'info';
|
2020-05-19 20:18:13 +08:00
|
|
|
};
|
2020-05-09 19:32:02 +08:00
|
|
|
|
2020-05-19 20:18:13 +08:00
|
|
|
// closeable when closeText is assigned
|
|
|
|
const isClosable = closeText ? true : closable;
|
|
|
|
const type = getType();
|
2020-05-09 19:32:02 +08:00
|
|
|
|
2020-05-19 20:18:13 +08:00
|
|
|
// banner 模式默认有 Icon
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
className,
|
2022-03-04 19:09:55 +08:00
|
|
|
hashId,
|
2020-05-19 20:18:13 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const dataOrAriaProps = getDataOrAriaProps(props);
|
|
|
|
|
2022-03-04 19:09:55 +08:00
|
|
|
return wrapSSR(
|
2020-08-11 17:04:42 +08:00
|
|
|
<CSSMotion
|
|
|
|
visible={!closed}
|
|
|
|
motionName={`${prefixCls}-motion`}
|
|
|
|
motionAppear={false}
|
|
|
|
motionEnter={false}
|
2022-11-19 13:47:33 +08:00
|
|
|
onLeaveStart={(node) => ({
|
2020-08-11 17:04:42 +08:00
|
|
|
maxHeight: node.offsetHeight,
|
|
|
|
})}
|
|
|
|
onLeaveEnd={afterClose}
|
2020-05-19 20:18:13 +08:00
|
|
|
>
|
2020-08-11 17:04:42 +08:00
|
|
|
{({ className: motionClassName, style: motionStyle }) => (
|
|
|
|
<div
|
|
|
|
ref={ref}
|
|
|
|
data-show={!closed}
|
|
|
|
className={classNames(alertCls, motionClassName)}
|
|
|
|
style={{ ...style, ...motionStyle }}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
onClick={onClick}
|
|
|
|
role="alert"
|
|
|
|
{...dataOrAriaProps}
|
|
|
|
>
|
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}
|
2022-06-22 21:01:00 +08:00
|
|
|
<CloseIcon
|
|
|
|
isClosable={!!isClosable}
|
|
|
|
closeText={closeText}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
closeIcon={closeIcon}
|
|
|
|
handleClose={handleClose}
|
|
|
|
/>
|
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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Alert.ErrorBoundary = ErrorBoundary;
|
|
|
|
|
|
|
|
export default Alert;
|