ant-design/components/alert/index.tsx

134 lines
3.6 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';
2015-11-20 11:05:34 +08:00
import Icon from '../icon';
import classNames from 'classnames';
2015-07-27 20:53:08 +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 */
afterClose?: Function;
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;
2016-07-14 13:29:50 +08:00
}
export default class Alert extends React.Component<AlertProps, any> {
constructor(props: AlertProps) {
super(props);
this.state = {
2015-08-20 13:06:47 +08:00
closing: true,
2016-05-11 09:32:33 +08:00
closed: false,
2015-07-29 18:44:56 +08:00
};
}
handleClose = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();
2016-07-14 13:29:50 +08:00
let 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);
}
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)();
}
2015-08-20 13:06:47 +08:00
render() {
let {
2016-10-21 18:02:37 +08:00
closable, description, type, prefixCls = 'ant-alert', message, closeText, showIcon, banner,
className = '', style, iconType,
} = this.props;
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
if (!iconType) {
switch (type) {
case 'success':
iconType = 'check-circle';
break;
case 'info':
iconType = 'info-circle';
break;
case 'error':
iconType = 'cross-circle';
break;
case 'warning':
iconType = 'exclamation-circle';
break;
default:
iconType = 'default';
}
// use outline icon in alert with description
if (!!description) {
iconType += '-o';
}
}
let alertCls = classNames(prefixCls, {
[`${prefixCls}-${type}`]: true,
[`${prefixCls}-close`]: !this.state.closing,
[`${prefixCls}-with-description`]: !!description,
[`${prefixCls}-no-icon`]: !showIcon,
2016-08-03 14:54:10 +08:00
[`${prefixCls}-banner`]: !!banner,
}, className);
// closeable when closeText is assigned
if (closeText) {
closable = true;
2015-07-27 20:53:08 +08:00
}
const closeIcon = closable ? (
<a onClick={this.handleClose} className={`${prefixCls}-close-icon`}>
{closeText || <Icon type="cross" />}
</a>
) : null;
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}>
{showIcon ? <Icon className={`${prefixCls}-icon`} type={iconType} /> : null}
<span className={`${prefixCls}-message`}>{message}</span>
<span className={`${prefixCls}-description`}>{description}</span>
{closeIcon}
</div>
</Animate>
);
2015-07-27 20:53:08 +08:00
}
}