ant-design/components/alert/index.tsx

195 lines
5.1 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as ReactDOM from 'react-dom';
2015-08-20 13:06:47 +08:00
import Animate from 'rc-animate';
import classNames from 'classnames';
2019-08-05 18:38:10 +08:00
import Icon, { ThemeType } from '../icon';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import getDataOrAriaProps from '../_util/getDataOrAriaProps';
import warning from '../_util/warning';
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 */
onClose?: React.MouseEventHandler<HTMLAnchorElement>;
/** 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;
iconType?: string;
2016-07-14 13:29:50 +08:00
style?: React.CSSProperties;
prefixCls?: string;
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
}
export default class Alert extends React.Component<AlertProps, AlertState> {
constructor(props: AlertProps) {
super(props);
warning(
!('iconType' in props),
'Alert',
'`iconType` is deprecated. Please use `icon` instead.',
);
this.state = {
closing: true,
closed: false,
};
}
2018-11-01 06:37:14 +08:00
handleClose = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();
2018-11-01 06:37:14 +08:00
const dom = ReactDOM.findDOMNode(this) as HTMLElement;
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
dom.style.height = `${dom.offsetHeight}px`;
2015-08-20 13:06:47 +08:00
this.setState({
2016-05-11 09:32:33 +08:00
closing: false,
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
animationEnd = () => {
2015-07-27 20:53:08 +08:00
this.setState({
2015-08-20 13:06:47 +08:00
closed: true,
2016-05-11 09:32:33 +08:00
closing: true,
2015-07-27 20:53:08 +08:00
});
(this.props.afterClose || noop)();
2018-12-07 20:02:01 +08:00
};
2018-11-01 06:37:14 +08:00
renderAlert = ({ getPrefixCls }: 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,
} = this.props;
2018-11-10 21:40:21 +08:00
let { closable, type, showIcon, iconType } = this.props;
const prefixCls = getPrefixCls('alert', customizePrefixCls);
2016-08-03 14:54:10 +08:00
// banner模式默认有 Icon
showIcon = banner && showIcon === undefined ? true : showIcon;
2016-08-03 14:54:10 +08:00
// banner模式默认为警告
type = banner && type === undefined ? 'warning' : type || 'info';
2016-08-03 14:54:10 +08:00
let iconTheme: ThemeType = 'filled';
if (!iconType) {
switch (type) {
case 'success':
iconType = 'check-circle';
break;
case 'info':
iconType = 'info-circle';
break;
case 'error':
2018-08-24 18:36:08 +08:00
iconType = 'close-circle';
break;
case 'warning':
iconType = 'exclamation-circle';
break;
default:
iconType = 'default';
}
// use outline icon in alert with description
2019-08-05 18:38:10 +08:00
if (description) {
iconTheme = 'outlined';
}
}
// closeable when closeText is assigned
if (closeText) {
closable = true;
}
2018-12-07 20:02:01 +08:00
const alertCls = classNames(
prefixCls,
`${prefixCls}-${type}`,
{
[`${prefixCls}-close`]: !this.state.closing,
[`${prefixCls}-with-description`]: !!description,
[`${prefixCls}-no-icon`]: !showIcon,
[`${prefixCls}-banner`]: !!banner,
[`${prefixCls}-closable`]: closable,
},
className,
);
const closeIcon = closable ? (
2019-08-05 18:38:10 +08:00
<span
role="button"
onClick={this.handleClose}
className={`${prefixCls}-close-icon`}
tabIndex={0}
>
{closeText ? (
<span className={`${prefixCls}-close-text`}>{closeText}</span>
) : (
<Icon type="close" />
)}
</span>
) : null;
const dataOrAriaProps = getDataOrAriaProps(this.props);
2018-12-07 20:02:01 +08:00
const iconNode = (icon &&
(React.isValidElement<{ className?: string }>(icon) ? (
React.cloneElement(icon, {
className: classNames({
[icon.props.className as string]: icon.props.className,
[`${prefixCls}-icon`]: true,
}),
})
) : (
<span className={`${prefixCls}-icon`}>{icon}</span>
))) || <Icon className={`${prefixCls}-icon`} type={iconType} theme={iconTheme} />;
return this.state.closed ? null : (
<Animate
component=""
2016-01-05 14:42:06 +08:00
showProp="data-show"
transitionName={`${prefixCls}-slide-up`}
onEnd={this.animationEnd}
>
<div data-show={this.state.closing} className={alertCls} style={style} {...dataOrAriaProps}>
{showIcon ? iconNode : null}
<span className={`${prefixCls}-message`}>{message}</span>
<span className={`${prefixCls}-description`}>{description}</span>
{closeIcon}
</div>
</Animate>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderAlert}</ConfigConsumer>;
}
}