You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.5 KiB
TypeScript
99 lines
2.5 KiB
TypeScript
10 years ago
|
import React from 'react';
|
||
9 years ago
|
import ReactDOM from 'react-dom';
|
||
10 years ago
|
import Animate from 'rc-animate';
|
||
9 years ago
|
import Icon from '../icon';
|
||
9 years ago
|
import classNames from 'classnames';
|
||
10 years ago
|
|
||
9 years ago
|
export default class Alert extends React.Component {
|
||
|
static defaultProps = {
|
||
|
prefixCls: 'ant-alert',
|
||
|
showIcon: false,
|
||
|
onClose() {},
|
||
|
type: 'info',
|
||
|
}
|
||
|
constructor(props) {
|
||
|
super(props);
|
||
|
this.state = {
|
||
10 years ago
|
closing: true,
|
||
9 years ago
|
closed: false,
|
||
10 years ago
|
};
|
||
9 years ago
|
}
|
||
|
handleClose = (e) => {
|
||
9 years ago
|
e.preventDefault();
|
||
9 years ago
|
let dom = ReactDOM.findDOMNode(this);
|
||
9 years ago
|
dom.style.height = `${dom.offsetHeight}px`;
|
||
10 years ago
|
// Magic code
|
||
10 years ago
|
// 重复一次后才能正确设置 height
|
||
9 years ago
|
dom.style.height = `${dom.offsetHeight}px`;
|
||
10 years ago
|
|
||
|
this.setState({
|
||
9 years ago
|
closing: false,
|
||
10 years ago
|
});
|
||
9 years ago
|
this.props.onClose(e);
|
||
9 years ago
|
}
|
||
|
animationEnd = () => {
|
||
10 years ago
|
this.setState({
|
||
10 years ago
|
closed: true,
|
||
9 years ago
|
closing: true,
|
||
10 years ago
|
});
|
||
9 years ago
|
}
|
||
10 years ago
|
render() {
|
||
9 years ago
|
let {
|
||
9 years ago
|
closable, description, type, prefixCls, message, closeText, showIcon,
|
||
9 years ago
|
} = this.props;
|
||
|
|
||
9 years ago
|
let iconType = '';
|
||
9 years ago
|
switch (type) {
|
||
9 years ago
|
case 'success':
|
||
|
iconType = 'check-circle';
|
||
|
break;
|
||
|
case 'info':
|
||
|
iconType = 'info-circle';
|
||
|
break;
|
||
|
case 'error':
|
||
9 years ago
|
iconType = 'cross-circle';
|
||
9 years ago
|
break;
|
||
9 years ago
|
case 'warning':
|
||
9 years ago
|
iconType = 'exclamation-circle';
|
||
|
break;
|
||
|
default:
|
||
|
iconType = 'default';
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// use outline icon in alert with description
|
||
|
if (!!description) {
|
||
|
iconType += '-o';
|
||
|
}
|
||
|
|
||
9 years ago
|
let alertCls = classNames({
|
||
|
[prefixCls]: true,
|
||
9 years ago
|
[`${prefixCls}-${type}`]: true,
|
||
|
[`${prefixCls}-close`]: !this.state.closing,
|
||
|
[`${prefixCls}-with-description`]: !!description,
|
||
|
[`${prefixCls}-no-icon`]: !showIcon,
|
||
9 years ago
|
});
|
||
|
|
||
9 years ago
|
// closeable when closeText is assigned
|
||
9 years ago
|
if (closeText) {
|
||
|
closable = true;
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
return this.state.closed ? null : (
|
||
|
<Animate component=""
|
||
9 years ago
|
showProp="data-show"
|
||
9 years ago
|
transitionName={`${prefixCls}-slide-up`}
|
||
9 years ago
|
onEnd={this.animationEnd}
|
||
|
>
|
||
9 years ago
|
<div data-show={this.state.closing} className={alertCls}>
|
||
9 years ago
|
{showIcon ? <Icon className="ant-alert-icon" type={iconType} /> : null}
|
||
9 years ago
|
<span className={`${prefixCls}-message`}>{message}</span>
|
||
|
<span className={`${prefixCls}-description`}>{description}</span>
|
||
|
{closable ? <a onClick={this.handleClose} className={`${prefixCls}-close-icon`}>
|
||
9 years ago
|
{closeText || <Icon type="cross" />}
|
||
9 years ago
|
</a> : null}
|
||
|
</div>
|
||
|
</Animate>
|
||
|
);
|
||
10 years ago
|
}
|
||
9 years ago
|
}
|