2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2019-08-13 14:07:17 +08:00
|
|
|
import {
|
2019-11-28 12:34:33 +08:00
|
|
|
CloseOutlined,
|
|
|
|
CheckCircleOutlined,
|
|
|
|
ExclamationCircleOutlined,
|
|
|
|
InfoCircleOutlined,
|
|
|
|
CloseCircleOutlined,
|
2019-08-13 14:07:17 +08:00
|
|
|
CheckCircleFilled,
|
|
|
|
ExclamationCircleFilled,
|
|
|
|
InfoCircleFilled,
|
|
|
|
CloseCircleFilled,
|
|
|
|
} from '@ant-design/icons';
|
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';
|
2015-07-27 20:53:08 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
function noop() {}
|
2016-10-21 18:02:37 +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;
|
|
|
|
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;
|
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
|
|
|
});
|
2016-10-21 18:02:37 +08:00
|
|
|
(this.props.onClose || noop)(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
|
|
|
});
|
2018-03-09 15:02:13 +08:00
|
|
|
(this.props.afterClose || noop)();
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-01 06:37:14 +08:00
|
|
|
|
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,
|
|
|
|
closeText,
|
|
|
|
banner,
|
|
|
|
className = '',
|
|
|
|
style,
|
|
|
|
icon,
|
2015-11-29 16:55:43 +08:00
|
|
|
} = this.props;
|
2019-08-13 14:07:17 +08:00
|
|
|
let { closable, type, showIcon } = 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);
|
|
|
|
|
2016-08-03 14:54:10 +08:00
|
|
|
// banner模式默认有 Icon
|
2017-04-22 10:37:00 +08:00
|
|
|
showIcon = banner && showIcon === undefined ? true : showIcon;
|
2016-08-03 14:54:10 +08:00
|
|
|
// banner模式默认为警告
|
2017-04-22 10:37:00 +08:00
|
|
|
type = banner && type === undefined ? 'warning' : type || 'info';
|
2016-08-03 14:54:10 +08:00
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
// use outline icon in alert with description
|
|
|
|
const iconType = (description ? iconMapOutlined : iconMapFilled)[type] || null;
|
2015-11-30 15:07:29 +08:00
|
|
|
|
2018-12-05 10:38:42 +08:00
|
|
|
// closeable when closeText is assigned
|
|
|
|
if (closeText) {
|
|
|
|
closable = true;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
[`${prefixCls}-no-icon`]: !showIcon,
|
|
|
|
[`${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
|
|
|
|
2016-11-25 12:03:39 +08:00
|
|
|
const closeIcon = closable ? (
|
2019-09-16 11:23:26 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
2019-08-05 18:38:10 +08:00
|
|
|
onClick={this.handleClose}
|
|
|
|
className={`${prefixCls}-close-icon`}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
2019-11-28 12:34:33 +08:00
|
|
|
{closeText ? (
|
|
|
|
<span className={`${prefixCls}-close-text`}>{closeText}</span>
|
|
|
|
) : (
|
|
|
|
<CloseOutlined />
|
|
|
|
)}
|
2019-09-16 11:23:26 +08:00
|
|
|
</button>
|
2016-11-25 12:03:39 +08:00
|
|
|
) : null;
|
|
|
|
|
2018-06-15 03:08:51 +08:00
|
|
|
const dataOrAriaProps = getDataOrAriaProps(this.props);
|
2018-06-12 03:18:41 +08:00
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
const iconNode =
|
|
|
|
(icon &&
|
|
|
|
(React.isValidElement<{ className?: string }>(icon) ? (
|
|
|
|
React.cloneElement(icon, {
|
2019-10-30 16:28:28 +08:00
|
|
|
className: classNames(`${prefixCls}-icon`, {
|
2019-08-13 14:07:17 +08:00
|
|
|
[icon.props.className as string]: icon.props.className,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
) : (
|
|
|
|
<span className={`${prefixCls}-icon`}>{icon}</span>
|
|
|
|
))) ||
|
|
|
|
React.createElement(iconType, { className: `${prefixCls}-icon` });
|
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}
|
|
|
|
>
|
2019-10-28 15:25:22 +08:00
|
|
|
<div data-show={!closing} className={alertCls} style={style} {...dataOrAriaProps}>
|
2018-09-03 19:29:27 +08:00
|
|
|
{showIcon ? 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
|
|
|
}
|