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 { animationEndEvent, addEventListenerOnce } from '../util/index';
import Animate from 'rc-animate';
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
// 重复是去除浏览器渲染bug
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
},
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-07-29 18:44:56 +08:00
var iconClass = this.props.description ?
'ant-alert-with-description-icon anticon-' : 'ant-alert-icon anticon-';
2015-07-29 10:27:31 +08:00
switch (this.props.type) {
2015-07-27 20:53:08 +08:00
case 'success':
iconClass += 'check-circle';
break;
case 'info':
2015-07-30 11:20:14 +08:00
iconClass += 'info-circle';
2015-07-27 20:53:08 +08:00
break;
case 'error':
2015-07-30 11:20:14 +08:00
iconClass += 'exclamation-circle';
break;
2015-07-27 20:53:08 +08:00
case 'warn':
2015-07-30 11:20:14 +08:00
iconClass += 'question-circle';
2015-07-27 20:53:08 +08:00
break;
default:
iconClass += 'default';
}
2015-08-20 13:06:47 +08:00
let html, 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'}>
<span
className='ant-alert-with-description-close-icon-x'></span>
</a> : '';
html = <div data-show={this.state.closing} className={'ant-alert-with-description ant-alert-with-description-' + this.props.type + closeName}>
<i className={'anticon ' + iconClass}></i>
<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}>
<i className={'anticon ' + iconClass}></i>
<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'}>
<span className='ant-alert-close-icon-x'></span>
</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}>
<i className={'anticon ' + iconClass}></i>
<span className={'ant-alert-description'}>{this.props.message}</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
}
}
2015-08-20 13:06:47 +08:00
return !this.state.closed ? <Animate
component=""
showProp='data-show'
transitionName="slide-up"
onEnd={this.animationEnd}>
{html}
</Animate> : null;
2015-07-27 20:53:08 +08:00
}
});