2016-09-18 10:12:21 +08:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2015-08-21 17:12:42 +08:00
|
|
|
import Animate from 'rc-animate';
|
2016-01-27 01:47:12 +08:00
|
|
|
import classNames from 'classnames';
|
2016-09-10 17:17:55 +08:00
|
|
|
import omit from 'omit.js';
|
2016-10-31 17:58:12 +08:00
|
|
|
import warning from 'warning';
|
|
|
|
import Icon from '../icon';
|
|
|
|
import splitObject from '../_util/splitObject';
|
2015-08-21 17:12:42 +08:00
|
|
|
|
2016-07-09 10:52:16 +08:00
|
|
|
export interface TagProps {
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 标签是否可以关闭 */
|
|
|
|
closable?: boolean;
|
|
|
|
/** 关闭时的回调 */
|
|
|
|
onClose?: Function;
|
|
|
|
/** 动画关闭后的回调 */
|
|
|
|
afterClose?: Function;
|
|
|
|
style?: React.CSSProperties;
|
2016-07-09 10:52:16 +08:00
|
|
|
}
|
2016-07-13 11:14:24 +08:00
|
|
|
|
2016-07-09 10:52:16 +08:00
|
|
|
export default class Tag extends React.Component<TagProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-tag',
|
|
|
|
closable: false,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2015-07-30 23:52:44 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-10-31 17:58:12 +08:00
|
|
|
warning(!('color' in props), '`Tag[color]` is deprecated, please override color by CSS instead.');
|
2015-07-30 23:52:44 +08:00
|
|
|
|
|
|
|
this.state = {
|
2015-08-21 17:27:29 +08:00
|
|
|
closing: false,
|
2016-01-26 23:40:20 +08:00
|
|
|
closed: false,
|
2015-07-30 23:52:44 +08:00
|
|
|
};
|
|
|
|
}
|
2015-08-20 11:46:02 +08:00
|
|
|
|
2016-03-23 19:50:44 +08:00
|
|
|
close = (e) => {
|
2016-10-24 16:30:38 +08:00
|
|
|
const onClose = this.props.onClose;
|
|
|
|
if (onClose) {
|
|
|
|
onClose(e);
|
|
|
|
}
|
2016-07-13 11:14:24 +08:00
|
|
|
if (e.defaultPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-24 16:09:55 +08:00
|
|
|
const dom = ReactDOM.findDOMNode(this) as HTMLElement;
|
2016-06-28 16:16:27 +08:00
|
|
|
dom.style.width = `${dom.getBoundingClientRect().width}px`;
|
2015-08-20 13:56:08 +08:00
|
|
|
// It's Magic Code, don't know why
|
2016-06-28 16:16:27 +08:00
|
|
|
dom.style.width = `${dom.getBoundingClientRect().width}px`;
|
2015-07-30 23:52:44 +08:00
|
|
|
this.setState({
|
2016-01-26 23:40:20 +08:00
|
|
|
closing: true,
|
2015-07-30 23:52:44 +08:00
|
|
|
});
|
2015-07-28 17:08:06 +08:00
|
|
|
}
|
2015-08-21 17:27:29 +08:00
|
|
|
|
2016-10-20 22:10:46 +08:00
|
|
|
animationEnd = (_, existed) => {
|
2016-04-16 20:24:50 +08:00
|
|
|
if (!existed && !this.state.closed) {
|
2016-01-27 01:20:05 +08:00
|
|
|
this.setState({
|
|
|
|
closed: true,
|
|
|
|
closing: false,
|
|
|
|
});
|
2016-10-24 16:30:38 +08:00
|
|
|
|
|
|
|
const afterClose = this.props.afterClose;
|
|
|
|
if (afterClose) {
|
|
|
|
afterClose();
|
|
|
|
}
|
2016-01-27 01:20:05 +08:00
|
|
|
}
|
2015-08-21 17:12:42 +08:00
|
|
|
}
|
2015-08-19 17:26:55 +08:00
|
|
|
|
2015-07-28 17:08:06 +08:00
|
|
|
render() {
|
2016-06-22 13:18:43 +08:00
|
|
|
const [{
|
2016-07-13 17:22:23 +08:00
|
|
|
prefixCls, closable, color, className, children,
|
2016-07-08 16:34:02 +08:00
|
|
|
}, otherProps] = splitObject(
|
|
|
|
this.props,
|
2016-07-13 11:14:24 +08:00
|
|
|
['prefixCls', 'closable', 'color', 'className', 'children']
|
2016-07-08 16:34:02 +08:00
|
|
|
);
|
2016-07-09 16:15:08 +08:00
|
|
|
const closeIcon = closable ? <Icon type="cross" onClick={this.close} /> : '';
|
2016-03-03 12:16:25 +08:00
|
|
|
const classString = classNames({
|
2016-01-27 01:47:12 +08:00
|
|
|
[prefixCls]: true,
|
2016-02-17 18:04:42 +08:00
|
|
|
[`${prefixCls}-${color}`]: !!color,
|
2016-09-30 11:30:51 +08:00
|
|
|
[`${prefixCls}-has-color`]: !!color,
|
2016-02-17 18:04:42 +08:00
|
|
|
[`${prefixCls}-close`]: this.state.closing,
|
2016-03-03 12:16:25 +08:00
|
|
|
[className]: !!className,
|
2016-01-27 01:47:12 +08:00
|
|
|
});
|
2016-07-08 10:23:20 +08:00
|
|
|
// fix https://fb.me/react-unknown-prop
|
|
|
|
const divProps = omit(otherProps, [
|
|
|
|
'onClose',
|
|
|
|
'afterClose',
|
|
|
|
]);
|
2016-01-27 01:51:54 +08:00
|
|
|
return (
|
2016-01-27 14:48:25 +08:00
|
|
|
<Animate component=""
|
|
|
|
showProp="data-show"
|
2016-02-17 18:04:42 +08:00
|
|
|
transitionName={`${prefixCls}-zoom`}
|
2016-01-27 14:48:25 +08:00
|
|
|
transitionAppear
|
2016-06-06 13:54:10 +08:00
|
|
|
onEnd={this.animationEnd}
|
2016-07-09 10:52:16 +08:00
|
|
|
>
|
2016-01-27 01:51:54 +08:00
|
|
|
{this.state.closed ? null : (
|
2016-08-03 09:55:16 +08:00
|
|
|
<div
|
|
|
|
data-show={!this.state.closing}
|
|
|
|
{...divProps}
|
|
|
|
className={classString}
|
|
|
|
style={{ backgroundColor: /blue|red|green|yellow/.test(color) ? null : color }}
|
|
|
|
>
|
2016-03-03 12:16:25 +08:00
|
|
|
<span className={`${prefixCls}-text`}>{children}</span>
|
2016-07-09 16:15:08 +08:00
|
|
|
{closeIcon}
|
2016-01-27 01:51:54 +08:00
|
|
|
</div>
|
2016-07-09 10:52:16 +08:00
|
|
|
) }
|
2016-01-26 23:40:20 +08:00
|
|
|
</Animate>
|
|
|
|
);
|
2015-07-28 17:08:06 +08:00
|
|
|
}
|
|
|
|
}
|