ant-design/components/tag/index.jsx

55 lines
1.4 KiB
React
Raw Normal View History

2015-07-28 17:08:06 +08:00
import React from 'react';
const prefixCls = 'ant-tag';
2015-08-20 11:46:02 +08:00
import { transitionEndEvent, addEventListenerOnce } from '../util/index';
2015-07-28 17:08:06 +08:00
class AntTag extends React.Component {
2015-07-30 23:52:44 +08:00
constructor(props) {
super(props);
this.state = {
2015-08-20 11:46:02 +08:00
closing: false,
2015-07-30 23:52:44 +08:00
closed: false
};
}
2015-08-20 11:46:02 +08:00
2015-07-30 23:52:44 +08:00
close(e) {
2015-08-19 19:21:40 +08:00
let dom = React.findDOMNode(this);
2015-08-19 17:26:55 +08:00
dom.style.width = dom.offsetWidth + 'px';
// Magic code
// 重复是去除浏览器渲染bug
dom.style.width = dom.offsetWidth + 'px';
2015-07-30 23:52:44 +08:00
this.setState({
2015-08-20 11:46:02 +08:00
closing: true
});
addEventListenerOnce(dom, transitionEndEvent, () => {
this.setState({
closed: true,
closing: false
});
2015-07-30 23:52:44 +08:00
});
2015-07-29 20:13:42 +08:00
this.props.onClose.call(this, e);
2015-07-28 17:08:06 +08:00
}
2015-08-19 17:26:55 +08:00
2015-07-28 17:08:06 +08:00
render() {
2015-07-28 20:03:10 +08:00
let close = this.props.closable ?
2015-07-30 23:52:44 +08:00
<i className="anticon anticon-cross" onClick={this.close.bind(this)}></i> : '';
2015-08-20 11:46:02 +08:00
let colorClass = this.props.color ? this.props.prefixCls + '-' + this.props.color : '';
2015-07-28 20:03:10 +08:00
2015-08-19 17:26:55 +08:00
let className = this.props.prefixCls + ' ' + colorClass;
2015-08-20 11:46:02 +08:00
className = this.state.closing ? className + ' ' + this.props.prefixCls + '-close' : className;
2015-08-19 17:26:55 +08:00
2015-08-20 11:46:02 +08:00
return (this.state.closed && !this.state.closing) ? null : <div className={className}>
2015-07-28 20:03:10 +08:00
<a className={this.props.prefixCls + '-text'} {...this.props} />
2015-07-28 17:08:06 +08:00
{close}
</div>;
}
}
AntTag.defaultProps = {
prefixCls: prefixCls,
closable: false,
2015-08-19 19:21:40 +08:00
onClose: function () {}
2015-07-28 17:08:06 +08:00
};
export default AntTag;