ant-design/components/alert/index.jsx

98 lines
3.1 KiB
React
Raw Normal View History

2015-07-27 20:53:08 +08:00
import React from 'react';
2015-08-20 13:06:47 +08:00
import Animate from 'rc-animate';
2015-10-02 14:21:02 +08:00
import Icon from '../iconfont';
2015-07-27 20:53:08 +08:00
export default React.createClass({
getDefaultProps() {
2015-07-29 18:44:56 +08:00
return {
prefixCls: 'ant-alert'
};
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-08-20 13:06:47 +08:00
let dom = React.findDOMNode(this);
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-07-29 15:22:47 +08:00
if (this.props.onClose) {
2015-07-29 18:44:56 +08:00
this.props.onClose.call(this, e);
2015-07-27 20:53:08 +08:00
}
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-09-01 16:18:46 +08:00
let iconClass = this.props.description ?
2015-10-02 14:21:02 +08:00
'ant-alert-with-description-icon' : 'ant-alert-icon';
let iconType = '';
2015-07-29 10:27:31 +08:00
switch (this.props.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-10-02 14:21:02 +08:00
iconType = 'question-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-09-01 16:18:46 +08:00
let html;
let closeName = !this.state.closing ? ' ' + this.props.prefixCls + '-close' : '';
2015-07-29 17:22:36 +08:00
if (this.props.description) {
2015-07-29 18:44:56 +08:00
let close = this.props.closable ?
2015-08-20 13:06:47 +08:00
<a onClick={this.handleClose} className={'ant-alert-with-description-close-icon'}>
2015-09-01 16:18:46 +08:00
<span className="ant-alert-with-description-close-icon-x"></span>
2015-08-20 13:06:47 +08:00
</a> : '';
html = <div data-show={this.state.closing} className={'ant-alert-with-description ant-alert-with-description-' + this.props.type + closeName}>
2015-10-02 14:21:02 +08:00
<Icon className={iconClass} type={iconType} />
2015-08-20 13:06:47 +08:00
<p className={'ant-alert-with-description-message'}>{this.props.message}</p>
<span className={'ant-alert-with-description-description'}>{this.props.description}</span>
2015-07-29 17:22:36 +08:00
{close}
2015-08-20 13:06:47 +08:00
</div>;
2015-07-27 20:53:08 +08:00
} else {
2015-07-29 10:27:31 +08:00
if (this.props.closeText) {
2015-08-20 13:06:47 +08:00
html = <div data-show={this.state.closing} className={'ant-alert ant-alert-' + this.props.type + closeName}>
2015-10-02 14:21:02 +08:00
<Icon className={iconClass} type={iconType} />
2015-08-20 13:06:47 +08:00
<span className={'ant-alert-description'}>{this.props.message}</span>
<span onClick={this.handleClose} className={'ant-alert-close-text'}>{this.props.closeText}</span>
</div>;
2015-07-27 20:53:08 +08:00
} else {
2015-07-29 18:44:56 +08:00
let close = this.props.closable ?
2015-07-29 17:22:36 +08:00
<a onClick={this.handleClose} className={'ant-alert-close-icon'}>
2015-09-01 16:18:46 +08:00
<span className="ant-alert-close-icon-x"></span>
2015-07-29 17:22:36 +08:00
</a> : '';
2015-08-20 13:06:47 +08:00
html = <div data-show={this.state.closing} className={'ant-alert ant-alert-' + this.props.type + closeName}>
2015-10-02 14:21:02 +08:00
<Icon className={iconClass} type={iconType} />
2015-08-20 13:06:47 +08:00
<span className={'ant-alert-description'}>{this.props.message}</span>
2015-08-20 13:17:17 +08:00
{close}
2015-08-20 13:06:47 +08:00
</div>;
2015-07-27 20:53:08 +08:00
}
}
2015-08-20 13:17:17 +08:00
return this.state.closed ? null : <Animate
2015-08-20 13:06:47 +08:00
component=""
2015-09-01 16:18:46 +08:00
showProp="data-show"
2015-08-20 13:06:47 +08:00
transitionName="slide-up"
onEnd={this.animationEnd}>
2015-08-20 13:17:17 +08:00
{html}
</Animate>;
2015-07-27 20:53:08 +08:00
}
});