ant-design/components/tag/index.tsx

160 lines
3.9 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as 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';
import omit from 'omit.js';
2018-06-13 10:57:34 +08:00
import { polyfill } from 'react-lifecycles-compat';
2016-10-31 17:58:12 +08:00
import Icon from '../icon';
2016-11-10 11:14:03 +08:00
import CheckableTag from './CheckableTag';
2015-08-21 17:12:42 +08:00
export { CheckableTagProps } from './CheckableTag';
2018-05-18 18:22:33 +08:00
export interface TagProps extends React.HTMLAttributes<HTMLDivElement> {
2016-12-19 15:19:15 +08:00
prefixCls?: string;
className?: string;
color?: string;
2016-07-13 11:14:24 +08:00
/** 标签是否可以关闭 */
closable?: boolean;
2018-06-12 22:23:04 +08:00
visible?: boolean;
2016-07-13 11:14:24 +08:00
/** 关闭时的回调 */
onClose?: Function;
/** 动画关闭后的回调 */
afterClose?: Function;
style?: React.CSSProperties;
2016-07-09 10:52:16 +08:00
}
2016-07-13 11:14:24 +08:00
2017-07-31 00:42:25 +08:00
export interface TagState {
closing: boolean;
closed: boolean;
2018-06-12 22:23:04 +08:00
visible: boolean;
2017-07-31 00:42:25 +08:00
}
2018-06-13 10:57:34 +08:00
class Tag extends React.Component<TagProps, TagState> {
2016-11-10 11:14:03 +08:00
static CheckableTag = CheckableTag;
static defaultProps = {
prefixCls: 'ant-tag',
closable: false,
2016-07-13 11:14:24 +08:00
};
2018-06-12 22:23:04 +08:00
static getDerivedStateFromProps(nextProps: TagProps) {
return ('visible' in nextProps) ? { visible: nextProps.visible } : null;
}
2015-07-30 23:52:44 +08:00
2018-06-12 22:23:04 +08:00
state = {
closing: false,
closed: false,
visible: true,
};
componentDidUpdate(_prevProps: TagProps, prevState: TagState) {
if (prevState.visible && !this.state.visible) {
this.close();
} else if (!prevState.visible && this.state.visible) {
this.show();
}
2015-07-30 23:52:44 +08:00
}
2015-08-20 11:46:02 +08:00
2018-06-12 22:23:04 +08:00
handleIconClick = (e: React.MouseEvent<HTMLElement>) => {
2016-10-24 16:30:38 +08:00
const onClose = this.props.onClose;
if (onClose) {
onClose(e);
}
2018-06-12 22:23:04 +08:00
if (e.defaultPrevented || 'visible' in this.props) {
return;
}
this.setState({ visible: false });
}
close = () => {
if (this.state.closing || this.state.closed) {
2016-07-13 11:14:24 +08:00
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
2018-06-12 22:23:04 +08:00
show = () => {
this.setState({
closed: false,
});
}
2017-07-31 00:42:25 +08:00
animationEnd = (_: string, existed: boolean) => {
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();
}
2018-06-12 22:23:04 +08:00
} else {
this.setState({
closed: false,
});
2016-01-27 01:20:05 +08:00
}
2015-08-21 17:12:42 +08:00
}
2015-08-19 17:26:55 +08:00
2017-07-31 00:42:25 +08:00
isPresetColor(color?: string): boolean {
if (!color) { return false; }
2017-10-16 15:35:54 +08:00
return (
/^(pink|red|yellow|orange|cyan|green|blue|purple|geekblue|magenta|volcano|gold|lime)(-inverse)?$/
.test(color)
);
}
2015-07-28 17:08:06 +08:00
render() {
2016-12-20 10:23:25 +08:00
const { prefixCls, closable, color, className, children, style, ...otherProps } = this.props;
2018-06-12 22:23:04 +08:00
const closeIcon = closable ? <Icon type="cross" onClick={this.handleIconClick} /> : '';
const isPresetColor = this.isPresetColor(color);
const classString = classNames(prefixCls, {
[`${prefixCls}-${color}`]: isPresetColor,
[`${prefixCls}-has-color`]: (color && !isPresetColor),
[`${prefixCls}-close`]: this.state.closing,
}, className);
2016-07-08 10:23:20 +08:00
// fix https://fb.me/react-unknown-prop
const divProps = omit(otherProps, [
'onClose',
'afterClose',
2018-06-12 22:23:04 +08:00
'visible',
2016-07-08 10:23:20 +08:00
]);
const tagStyle = {
backgroundColor: (color && !isPresetColor) ? color : null,
...style,
};
const tag = this.state.closed ? null : (
<div
data-show={!this.state.closing}
{...divProps}
className={classString}
style={tagStyle}
>
{children}
{closeIcon}
</div>
);
2016-01-27 01:51:54 +08:00
return (
2016-11-09 17:10:05 +08:00
<Animate
component=""
2016-01-27 14:48:25 +08:00
showProp="data-show"
transitionName={`${prefixCls}-zoom`}
2017-05-31 15:48:11 +08:00
transitionAppear
onEnd={this.animationEnd}
2016-11-09 17:10:05 +08:00
>
{tag}
2016-01-26 23:40:20 +08:00
</Animate>
);
2015-07-28 17:08:06 +08:00
}
}
2018-06-13 10:57:34 +08:00
polyfill(Tag);
export default Tag;