ant-design/components/tag/index.tsx

158 lines
3.8 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
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';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { PresetColorTypes } from '../_util/colors';
import warning from '../_util/warning';
2019-04-10 21:42:35 +08:00
import Wave from '../_util/wave';
2015-08-21 17:12:42 +08:00
export { CheckableTagProps } from './CheckableTag';
export interface TagProps extends React.HTMLAttributes<HTMLSpanElement> {
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
2018-12-22 19:20:38 +08:00
interface TagState {
2018-06-12 22:23:04 +08:00
visible: boolean;
2017-07-31 00:42:25 +08:00
}
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
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;
2019-08-05 18:38:10 +08:00
static defaultProps = {
closable: false,
2016-07-13 11:14:24 +08:00
};
2018-12-22 19:20:38 +08:00
static getDerivedStateFromProps(nextProps: TagProps) {
if ('visible' in nextProps) {
2018-12-22 19:20:38 +08:00
return {
visible: nextProps.visible,
};
}
return null;
2018-06-12 22:23:04 +08:00
}
2015-07-30 23:52:44 +08:00
2018-06-12 22:23:04 +08:00
state = {
visible: true,
};
constructor(props: TagProps) {
super(props);
warning(
!('afterClose' in props),
'Tag',
"'afterClose' will be deprecated, please use 'onClose', we will remove this in the next version.",
);
}
2019-08-05 18:38:10 +08:00
getTagStyle() {
const { color, style } = this.props;
const isPresetColor = this.isPresetColor();
return {
backgroundColor: color && !isPresetColor ? color : undefined,
...style,
};
}
getTagClassName({ getPrefixCls }: ConfigConsumerProps) {
const { prefixCls: customizePrefixCls, className, color } = this.props;
const { visible } = this.state;
const isPresetColor = this.isPresetColor();
const prefixCls = getPrefixCls('tag', customizePrefixCls);
return classNames(
prefixCls,
{
[`${prefixCls}-${color}`]: isPresetColor,
[`${prefixCls}-has-color`]: color && !isPresetColor,
[`${prefixCls}-hidden`]: !visible,
},
className,
);
}
2018-12-22 19:20:38 +08:00
setVisible(visible: boolean, e: React.MouseEvent<HTMLElement>) {
const { onClose, afterClose } = this.props;
2016-10-24 16:30:38 +08:00
if (onClose) {
onClose(e);
}
if (afterClose && !onClose) {
// next version remove.
afterClose();
}
2018-12-22 19:20:38 +08:00
if (e.defaultPrevented) {
2018-06-12 22:23:04 +08:00
return;
}
2018-12-22 19:20:38 +08:00
if (!('visible' in this.props)) {
this.setState({ visible });
2016-07-13 11:14:24 +08:00
}
2018-12-22 19:20:38 +08:00
}
2015-08-21 17:27:29 +08:00
2018-12-22 19:20:38 +08:00
handleIconClick = (e: React.MouseEvent<HTMLElement>) => {
this.setVisible(false, e);
2018-12-07 20:02:01 +08:00
};
2018-06-12 22:23:04 +08:00
2019-08-05 18:38:10 +08:00
isPresetColor(): boolean {
const { color } = this.props;
2018-12-07 20:02:01 +08:00
if (!color) {
return false;
}
return PresetColorRegex.test(color);
}
2018-12-22 19:20:38 +08:00
renderCloseIcon() {
const { closable } = this.props;
return closable ? <Icon type="close" onClick={this.handleIconClick} /> : null;
}
2018-12-26 16:01:00 +08:00
renderTag = (configProps: ConfigConsumerProps) => {
2019-08-05 18:38:10 +08:00
const { children, ...otherProps } = this.props;
2019-04-10 21:42:35 +08:00
const isNeedWave =
'onClick' in otherProps || (children && (children as React.ReactElement<any>).type === 'a');
const tagProps = omit(otherProps, [
'onClose',
'afterClose',
'color',
'visible',
'closable',
'prefixCls',
]);
2019-04-10 21:42:35 +08:00
return isNeedWave ? (
<Wave>
<span
{...tagProps}
className={this.getTagClassName(configProps)}
style={this.getTagStyle()}
>
2019-04-10 21:42:35 +08:00
{children}
{this.renderCloseIcon()}
</span>
2019-04-10 21:42:35 +08:00
</Wave>
) : (
<span {...tagProps} className={this.getTagClassName(configProps)} style={this.getTagStyle()}>
2019-04-09 22:35:21 +08:00
{children}
{this.renderCloseIcon()}
</span>
2016-01-26 23:40:20 +08:00
);
2019-01-07 09:40:55 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderTag}</ConfigConsumer>;
}
2015-07-28 17:08:06 +08:00
}
2018-06-13 10:57:34 +08:00
polyfill(Tag);
export default Tag;