You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.1 KiB

import React from 'react';
import ReactDOM from 'react-dom';
import Animate from 'rc-animate';
import classNames from 'classnames';
import omit from 'omit.js';
import assign from 'object-assign';
import Icon from '../icon';
import warning from '../_util/warning';
import splitObject from '../_util/splitObject';
import CheckableTag from './CheckableTag';
9 years ago
export interface TagProps {
color?: string;
/** 标签是否可以关闭 */
closable?: boolean;
/** 关闭时的回调 */
onClose?: Function;
/** 动画关闭后的回调 */
afterClose?: Function;
style?: React.CSSProperties;
9 years ago
}
9 years ago
export default class Tag extends React.Component<TagProps, any> {
static CheckableTag = CheckableTag;
static defaultProps = {
prefixCls: 'ant-tag',
closable: false,
};
constructor(props: TagProps) {
super(props);
warning(
!/blue|red|green|yellow/.test(props.color || ''),
'`Tag[color=red|green|blue|yellow]` is deprecated, ' +
'please set color by `#abc` or `rgb(a, b, c)` instead.'
);
this.state = {
closing: false,
closed: false,
};
}
9 years ago
close = (e) => {
const onClose = this.props.onClose;
if (onClose) {
onClose(e);
}
if (e.defaultPrevented) {
return;
}
const dom = ReactDOM.findDOMNode(this) as HTMLElement;
dom.style.width = `${dom.getBoundingClientRect().width}px`;
// It's Magic Code, don't know why
dom.style.width = `${dom.getBoundingClientRect().width}px`;
this.setState({
closing: true,
});
}
8 years ago
animationEnd = (_, existed) => {
if (!existed && !this.state.closed) {
this.setState({
closed: true,
closing: false,
});
const afterClose = this.props.afterClose;
if (afterClose) {
afterClose();
}
}
}
render() {
const [{
prefixCls, closable, color, className, children, style,
9 years ago
}, otherProps] = splitObject(
this.props,
['prefixCls', 'closable', 'color', 'className', 'children', 'style']
9 years ago
);
9 years ago
const closeIcon = closable ? <Icon type="cross" onClick={this.close} /> : '';
const classString = classNames({
[prefixCls]: true,
[`${prefixCls}-${color}`]: !!color,
[`${prefixCls}-has-color`]: !!color,
[`${prefixCls}-close`]: this.state.closing,
[className]: !!className,
});
// fix https://fb.me/react-unknown-prop
const divProps = omit(otherProps, [
'onClose',
'afterClose',
]);
return (
<Animate
component=""
showProp="data-show"
transitionName={`${prefixCls}-zoom`}
transitionAppear
onEnd={this.animationEnd}
>
{this.state.closed ? null : (
<div
data-show={!this.state.closing}
{...divProps}
className={classString}
style={assign({
backgroundColor: /blue|red|green|yellow/.test(color) ? null : color,
}, style)}
>
<span className={`${prefixCls}-text`}>{children}</span>
9 years ago
{closeIcon}
</div>
9 years ago
) }
</Animate>
);
}
}