2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
|
|
|
import CheckCircleOutlined from '@ant-design/icons/CheckCircleOutlined';
|
|
|
|
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined';
|
|
|
|
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
|
|
|
|
import CloseCircleOutlined from '@ant-design/icons/CloseCircleOutlined';
|
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
|
|
|
import InfoCircleFilled from '@ant-design/icons/InfoCircleFilled';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
2015-08-20 13:06:47 +08:00
|
|
|
import Animate from 'rc-animate';
|
2015-11-29 16:55:43 +08:00
|
|
|
import classNames from 'classnames';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2018-06-15 03:08:51 +08:00
|
|
|
import getDataOrAriaProps from '../_util/getDataOrAriaProps';
|
2019-12-11 14:15:45 +08:00
|
|
|
import ErrorBoundary from './ErrorBoundary';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { replaceElement } from '../_util/reactNode';
|
2015-07-27 20:53:08 +08:00
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface AlertProps {
|
2016-07-14 13:29:50 +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 */
|
|
|
|
message: React.ReactNode;
|
|
|
|
/** 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;
|
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
|
|
|
}
|
|
|
|
|
2018-11-01 06:37:14 +08:00
|
|
|
export interface AlertState {
|
2018-12-07 20:02:01 +08:00
|
|
|
closing: boolean;
|
|
|
|
closed: boolean;
|
2018-11-01 06:37:14 +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
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
const iconMapOutlined = {
|
2019-11-28 12:34:33 +08:00
|
|
|
success: CheckCircleOutlined,
|
|
|
|
info: InfoCircleOutlined,
|
|
|
|
error: CloseCircleOutlined,
|
|
|
|
warning: ExclamationCircleOutlined,
|
2019-08-13 14:07:17 +08:00
|
|
|
};
|
2019-08-02 22:50:11 +08:00
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
export default class Alert extends React.Component<AlertProps, AlertState> {
|
2019-12-11 14:15:45 +08:00
|
|
|
static ErrorBoundary = ErrorBoundary;
|
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
state = {
|
2019-10-30 16:28:28 +08:00
|
|
|
closing: false,
|
2019-08-13 14:07:17 +08:00
|
|
|
closed: false,
|
|
|
|
};
|
2018-11-01 06:37:14 +08:00
|
|
|
|
2019-09-16 11:23:26 +08:00
|
|
|
handleClose = (e: React.MouseEvent<HTMLButtonElement>) => {
|
2015-11-25 17:35:49 +08:00
|
|
|
e.preventDefault();
|
2018-11-01 06:37:14 +08:00
|
|
|
const dom = ReactDOM.findDOMNode(this) as HTMLElement;
|
2016-02-17 18:04:42 +08:00
|
|
|
dom.style.height = `${dom.offsetHeight}px`;
|
2015-08-20 13:06:47 +08:00
|
|
|
// Magic code
|
2015-08-20 13:17:17 +08:00
|
|
|
// 重复一次后才能正确设置 height
|
2016-02-17 18:04:42 +08:00
|
|
|
dom.style.height = `${dom.offsetHeight}px`;
|
2015-08-20 13:06:47 +08:00
|
|
|
|
|
|
|
this.setState({
|
2019-10-28 15:25:22 +08:00
|
|
|
closing: true,
|
2015-08-20 13:06:47 +08:00
|
|
|
});
|
2020-05-09 19:32:02 +08:00
|
|
|
this.props.onClose?.(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-01 06:37:14 +08:00
|
|
|
|
2016-03-28 23:21:47 +08:00
|
|
|
animationEnd = () => {
|
2015-07-27 20:53:08 +08:00
|
|
|
this.setState({
|
2019-10-28 15:25:22 +08:00
|
|
|
closing: false,
|
2015-08-20 13:06:47 +08:00
|
|
|
closed: true,
|
2015-07-27 20:53:08 +08:00
|
|
|
});
|
2020-05-09 19:32:02 +08:00
|
|
|
this.props.afterClose?.();
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-01 06:37:14 +08:00
|
|
|
|
2020-05-09 19:32:02 +08:00
|
|
|
getShowIcon() {
|
|
|
|
const { banner, showIcon } = this.props;
|
|
|
|
// banner 模式默认有 Icon
|
|
|
|
return banner && showIcon === undefined ? true : showIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
getType() {
|
|
|
|
const { banner, type } = this.props;
|
|
|
|
if (type !== undefined) {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
// banner 模式默认为警告
|
|
|
|
return banner ? 'warning' : 'info';
|
|
|
|
}
|
|
|
|
|
|
|
|
getClosable() {
|
|
|
|
const { closable, closeText } = this.props;
|
|
|
|
// closeable when closeText is assigned
|
|
|
|
return closeText ? true : closable;
|
|
|
|
}
|
|
|
|
|
|
|
|
getIconType() {
|
|
|
|
const { description } = this.props;
|
|
|
|
// use outline icon in alert with description
|
|
|
|
return (description ? iconMapOutlined : iconMapFilled)[this.getType()] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderIconNode({ prefixCls }: { prefixCls: string }) {
|
|
|
|
const { icon } = this.props;
|
|
|
|
const iconType = this.getIconType();
|
|
|
|
if (icon) {
|
2020-05-14 20:54:49 +08:00
|
|
|
return replaceElement(icon, <span className={`${prefixCls}-icon`}>{icon}</span>, () => ({
|
|
|
|
className: classNames(`${prefixCls}-icon`, {
|
|
|
|
[(icon as any).props.className]: (icon as any).props.className,
|
|
|
|
}),
|
|
|
|
}));
|
2020-05-09 19:32:02 +08:00
|
|
|
}
|
|
|
|
return React.createElement(iconType, { className: `${prefixCls}-icon` });
|
|
|
|
}
|
|
|
|
|
|
|
|
renderCloseIcon({ prefixCls }: { prefixCls: string }) {
|
|
|
|
const { closeText } = this.props;
|
|
|
|
return this.getClosable() ? (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={this.handleClose}
|
|
|
|
className={`${prefixCls}-close-icon`}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
{closeText ? (
|
|
|
|
<span className={`${prefixCls}-close-text`}>{closeText}</span>
|
|
|
|
) : (
|
|
|
|
<CloseOutlined />
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderAlert = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2018-11-10 21:40:21 +08:00
|
|
|
const {
|
2018-12-07 20:02:01 +08:00
|
|
|
description,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
message,
|
|
|
|
banner,
|
|
|
|
className = '',
|
|
|
|
style,
|
2020-03-25 16:50:43 +08:00
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
|
|
|
onClick,
|
2015-11-29 16:55:43 +08:00
|
|
|
} = this.props;
|
2019-10-28 15:25:22 +08:00
|
|
|
const { closing, closed } = this.state;
|
2015-11-29 16:55:43 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('alert', customizePrefixCls);
|
|
|
|
|
2020-05-09 19:32:02 +08:00
|
|
|
const isShowIcon = this.getShowIcon();
|
|
|
|
const type = this.getType();
|
|
|
|
const closable = this.getClosable();
|
2018-12-05 10:38:42 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
const alertCls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
`${prefixCls}-${type}`,
|
|
|
|
{
|
2019-10-28 15:25:22 +08:00
|
|
|
[`${prefixCls}-closing`]: closing,
|
2018-12-07 20:02:01 +08:00
|
|
|
[`${prefixCls}-with-description`]: !!description,
|
2020-05-09 19:32:02 +08:00
|
|
|
[`${prefixCls}-no-icon`]: !isShowIcon,
|
2018-12-07 20:02:01 +08:00
|
|
|
[`${prefixCls}-banner`]: !!banner,
|
|
|
|
[`${prefixCls}-closable`]: closable,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2018-12-07 20:02:01 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2015-11-29 16:55:43 +08:00
|
|
|
|
2020-05-09 19:32:02 +08:00
|
|
|
const closeIcon = this.renderCloseIcon({ prefixCls });
|
2016-11-25 12:03:39 +08:00
|
|
|
|
2018-06-15 03:08:51 +08:00
|
|
|
const dataOrAriaProps = getDataOrAriaProps(this.props);
|
2018-06-12 03:18:41 +08:00
|
|
|
|
2020-05-09 19:32:02 +08:00
|
|
|
const iconNode = this.renderIconNode({ prefixCls });
|
2018-09-03 19:29:27 +08:00
|
|
|
|
2019-10-28 15:25:22 +08:00
|
|
|
return closed ? null : (
|
2016-10-28 16:42:26 +08:00
|
|
|
<Animate
|
|
|
|
component=""
|
2016-01-05 14:42:06 +08:00
|
|
|
showProp="data-show"
|
2016-06-28 22:13:11 +08:00
|
|
|
transitionName={`${prefixCls}-slide-up`}
|
2016-06-06 13:54:10 +08:00
|
|
|
onEnd={this.animationEnd}
|
|
|
|
>
|
2020-03-25 16:50:43 +08:00
|
|
|
<div
|
|
|
|
data-show={!closing}
|
|
|
|
className={alertCls}
|
|
|
|
style={style}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
onClick={onClick}
|
|
|
|
{...dataOrAriaProps}
|
|
|
|
>
|
2020-05-09 19:32:02 +08:00
|
|
|
{isShowIcon ? iconNode : null}
|
2016-02-17 18:04:42 +08:00
|
|
|
<span className={`${prefixCls}-message`}>{message}</span>
|
|
|
|
<span className={`${prefixCls}-description`}>{description}</span>
|
2016-11-25 12:03:39 +08:00
|
|
|
{closeIcon}
|
2015-11-29 16:55:43 +08:00
|
|
|
</div>
|
|
|
|
</Animate>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderAlert}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|