2015-07-27 20:53:08 +08:00
|
|
|
import React from 'react';
|
2015-10-21 17:59:57 +08:00
|
|
|
import 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';
|
2015-11-29 16:55:43 +08:00
|
|
|
import classNames from 'classnames';
|
2015-07-27 20:53:08 +08:00
|
|
|
|
|
|
|
export default React.createClass({
|
|
|
|
getDefaultProps() {
|
2015-07-29 18:44:56 +08:00
|
|
|
return {
|
2015-11-29 16:55:43 +08:00
|
|
|
prefixCls: 'ant-alert',
|
|
|
|
showIcon: false,
|
|
|
|
onClose() {}
|
2015-07-29 18:44:56 +08:00
|
|
|
};
|
2015-07-27 20:53:08 +08:00
|
|
|
},
|
2015-07-29 18:44:56 +08:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
2015-08-20 13:06:47 +08:00
|
|
|
closing: true,
|
|
|
|
closed: false
|
2015-07-29 18:44:56 +08:00
|
|
|
};
|
2015-07-27 20:53:08 +08:00
|
|
|
},
|
2015-07-29 18:44:56 +08:00
|
|
|
handleClose(e) {
|
2015-11-25 17:35:49 +08:00
|
|
|
e.preventDefault();
|
2015-10-21 17:59:57 +08:00
|
|
|
let dom = ReactDOM.findDOMNode(this);
|
2015-08-20 13:06:47 +08:00
|
|
|
dom.style.height = dom.offsetHeight + 'px';
|
|
|
|
// Magic code
|
2015-08-20 13:17:17 +08:00
|
|
|
// 重复一次后才能正确设置 height
|
2015-08-20 13:06:47 +08:00
|
|
|
dom.style.height = dom.offsetHeight + 'px';
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
closing: false
|
|
|
|
});
|
2015-11-29 16:55:43 +08:00
|
|
|
this.props.onClose.call(this, e);
|
2015-08-20 13:06:47 +08:00
|
|
|
},
|
2015-08-20 13:17:17 +08:00
|
|
|
animationEnd() {
|
2015-07-27 20:53:08 +08:00
|
|
|
this.setState({
|
2015-08-20 13:06:47 +08:00
|
|
|
closed: true,
|
|
|
|
closing: true
|
2015-07-27 20:53:08 +08:00
|
|
|
});
|
|
|
|
},
|
2015-08-20 13:06:47 +08:00
|
|
|
render() {
|
2015-11-29 16:55:43 +08:00
|
|
|
let {
|
2015-11-29 18:42:57 +08:00
|
|
|
closable, description, type, prefixCls, message, closeText, showIcon
|
2015-11-29 16:55:43 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2015-10-02 14:21:02 +08:00
|
|
|
let iconType = '';
|
2015-11-29 16:55:43 +08:00
|
|
|
switch (type) {
|
2015-09-01 16:18:46 +08:00
|
|
|
case 'success':
|
2015-10-02 14:21:02 +08:00
|
|
|
iconType = 'check-circle';
|
2015-09-01 16:18:46 +08:00
|
|
|
break;
|
|
|
|
case 'info':
|
2015-10-02 14:21:02 +08:00
|
|
|
iconType = 'info-circle';
|
2015-09-01 16:18:46 +08:00
|
|
|
break;
|
|
|
|
case 'error':
|
2015-10-02 14:21:02 +08:00
|
|
|
iconType = 'exclamation-circle';
|
2015-09-01 16:18:46 +08:00
|
|
|
break;
|
|
|
|
case 'warn':
|
2015-12-10 16:41:41 +08:00
|
|
|
iconType = 'exclamation-circle';
|
2015-09-01 16:18:46 +08:00
|
|
|
break;
|
|
|
|
default:
|
2015-10-02 14:21:02 +08:00
|
|
|
iconType = 'default';
|
2015-07-27 20:53:08 +08:00
|
|
|
}
|
2015-11-29 16:55:43 +08:00
|
|
|
|
2015-11-30 15:07:29 +08:00
|
|
|
// use outline icon in alert with description
|
|
|
|
if (!!description) {
|
|
|
|
iconType += '-o';
|
|
|
|
}
|
|
|
|
|
2015-11-29 16:55:43 +08:00
|
|
|
let alertCls = classNames({
|
|
|
|
[prefixCls]: true,
|
|
|
|
[prefixCls + '-' + type]: true,
|
|
|
|
[prefixCls + '-close']: !this.state.closing,
|
|
|
|
[prefixCls + '-with-description']: !!description,
|
2015-11-29 18:42:57 +08:00
|
|
|
[prefixCls + '-no-icon']: !showIcon,
|
2015-11-29 16:55:43 +08:00
|
|
|
});
|
|
|
|
|
2015-11-30 15:07:29 +08:00
|
|
|
// closeable when closeText is assigned
|
2015-11-29 16:55:43 +08:00
|
|
|
if (closeText) {
|
|
|
|
closable = true;
|
2015-07-27 20:53:08 +08:00
|
|
|
}
|
2015-11-29 16:55:43 +08:00
|
|
|
|
|
|
|
return this.state.closed ? null : (
|
|
|
|
<Animate component=""
|
|
|
|
showProp="data-show"
|
|
|
|
transitionName="slide-up"
|
|
|
|
onEnd={this.animationEnd}>
|
|
|
|
<div data-show={this.state.closing} className={alertCls}>
|
2015-11-29 18:42:57 +08:00
|
|
|
{showIcon ? <Icon className="ant-alert-icon" type={iconType} /> : null}
|
2015-11-29 16:55:43 +08:00
|
|
|
<span className={prefixCls + '-message'}>{message}</span>
|
|
|
|
<span className={prefixCls + '-description'}>{description}</span>
|
|
|
|
{closable ? <a onClick={this.handleClose} className={prefixCls + '-close-icon'}>
|
2015-11-29 18:42:57 +08:00
|
|
|
{closeText || <Icon type="cross" />}
|
2015-11-29 16:55:43 +08:00
|
|
|
</a> : null}
|
|
|
|
</div>
|
|
|
|
</Animate>
|
|
|
|
);
|
2015-07-27 20:53:08 +08:00
|
|
|
}
|
|
|
|
});
|