2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-01-27 01:47:12 +08:00
|
|
|
import classNames from 'classnames';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2016-11-10 11:14:03 +08:00
|
|
|
import CheckableTag from './CheckableTag';
|
2020-05-28 09:56:45 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { PresetColorType, PresetStatusColorType } from '../_util/colors';
|
|
|
|
import { PresetColorTypes, PresetStatusColorTypes } from '../_util/colors';
|
2019-04-10 21:42:35 +08:00
|
|
|
import Wave from '../_util/wave';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { LiteralUnion } from '../_util/type';
|
2015-08-21 17:12:42 +08:00
|
|
|
|
2017-09-25 22:14:49 +08:00
|
|
|
export { CheckableTagProps } from './CheckableTag';
|
|
|
|
|
2019-07-30 11:46:10 +08:00
|
|
|
export interface TagProps extends React.HTMLAttributes<HTMLSpanElement> {
|
2016-12-19 15:19:15 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2020-04-07 22:10:07 +08:00
|
|
|
color?: LiteralUnion<PresetColorType | PresetStatusColorType, string>;
|
2016-07-13 11:14:24 +08:00
|
|
|
closable?: boolean;
|
2020-06-28 22:41:59 +08:00
|
|
|
closeIcon?: React.ReactNode;
|
2018-06-12 22:23:04 +08:00
|
|
|
visible?: boolean;
|
2020-09-29 11:06:12 +08:00
|
|
|
onClose?: (e: React.MouseEvent<HTMLElement>) => void;
|
2016-07-13 11:14:24 +08:00
|
|
|
style?: React.CSSProperties;
|
2020-03-21 12:01:23 +08:00
|
|
|
icon?: React.ReactNode;
|
2016-07-09 10:52:16 +08:00
|
|
|
}
|
2016-07-13 11:14:24 +08:00
|
|
|
|
2019-03-31 20:21:20 +08:00
|
|
|
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
|
2019-10-25 23:17:25 +08:00
|
|
|
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);
|
2019-03-31 20:21:20 +08:00
|
|
|
|
2020-04-29 11:29:26 +08:00
|
|
|
export interface TagType
|
2020-04-27 13:14:00 +08:00
|
|
|
extends React.ForwardRefExoticComponent<TagProps & React.RefAttributes<HTMLElement>> {
|
2020-04-22 10:59:24 +08:00
|
|
|
CheckableTag: typeof CheckableTag;
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2021-01-13 21:00:30 +08:00
|
|
|
const InternalTag: React.ForwardRefRenderFunction<HTMLSpanElement, TagProps> = (
|
2020-05-28 09:56:45 +08:00
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
children,
|
|
|
|
icon,
|
|
|
|
color,
|
|
|
|
onClose,
|
2020-06-28 22:41:59 +08:00
|
|
|
closeIcon,
|
2020-05-28 09:56:45 +08:00
|
|
|
closable = false,
|
|
|
|
...props
|
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-04-22 10:59:24 +08:00
|
|
|
const [visible, setVisible] = React.useState(true);
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2020-04-22 10:59:24 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if ('visible' in props) {
|
|
|
|
setVisible(props.visible!);
|
2018-08-18 14:29:25 +08:00
|
|
|
}
|
2020-04-22 10:59:24 +08:00
|
|
|
}, [props.visible]);
|
2015-07-30 23:52:44 +08:00
|
|
|
|
2020-04-22 10:59:24 +08:00
|
|
|
const isPresetColor = (): boolean => {
|
|
|
|
if (!color) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
|
2018-06-12 22:23:04 +08:00
|
|
|
};
|
|
|
|
|
2020-05-28 09:56:45 +08:00
|
|
|
const tagStyle = {
|
|
|
|
backgroundColor: color && !isPresetColor() ? color : undefined,
|
|
|
|
...style,
|
2020-04-22 10:59:24 +08:00
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-05-28 09:56:45 +08:00
|
|
|
const presetColor = isPresetColor();
|
|
|
|
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
|
|
|
const tagClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${color}`]: presetColor,
|
|
|
|
[`${prefixCls}-has-color`]: color && !presetColor,
|
|
|
|
[`${prefixCls}-hidden`]: !visible,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-05-28 09:56:45 +08:00
|
|
|
const handleCloseClick = (e: React.MouseEvent<HTMLElement>) => {
|
2020-04-22 10:59:24 +08:00
|
|
|
e.stopPropagation();
|
2021-02-19 18:26:53 +08:00
|
|
|
onClose?.(e);
|
2019-07-09 11:46:21 +08:00
|
|
|
|
2018-12-22 19:20:38 +08:00
|
|
|
if (e.defaultPrevented) {
|
2018-06-12 22:23:04 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-04-22 10:59:24 +08:00
|
|
|
if (!('visible' in props)) {
|
|
|
|
setVisible(false);
|
2016-07-13 11:14:24 +08:00
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-06-12 22:23:04 +08:00
|
|
|
|
2020-04-22 10:59:24 +08:00
|
|
|
const renderCloseIcon = () => {
|
2020-06-28 22:41:59 +08:00
|
|
|
if (closable) {
|
|
|
|
return closeIcon ? (
|
2020-10-19 18:27:20 +08:00
|
|
|
<span className={`${prefixCls}-close-icon`} onClick={handleCloseClick}>
|
2020-06-28 22:41:59 +08:00
|
|
|
{closeIcon}
|
2020-10-19 18:27:20 +08:00
|
|
|
</span>
|
2020-06-28 22:41:59 +08:00
|
|
|
) : (
|
|
|
|
<CloseOutlined className={`${prefixCls}-close-icon`} onClick={handleCloseClick} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
2020-04-22 10:59:24 +08:00
|
|
|
};
|
2018-08-18 14:29:25 +08:00
|
|
|
|
2020-04-27 13:14:00 +08:00
|
|
|
const isNeedWave =
|
2020-05-28 09:56:45 +08:00
|
|
|
'onClick' in props || (children && (children as React.ReactElement<any>).type === 'a');
|
|
|
|
const tagProps = omit(props, ['visible']);
|
2020-04-27 13:14:00 +08:00
|
|
|
const iconNode = icon || null;
|
|
|
|
const kids = iconNode ? (
|
|
|
|
<>
|
|
|
|
{iconNode}
|
|
|
|
<span>{children}</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
children
|
|
|
|
);
|
|
|
|
|
2020-05-28 09:56:45 +08:00
|
|
|
const tagNode = (
|
|
|
|
<span {...tagProps} ref={ref} className={tagClassName} style={tagStyle}>
|
2020-04-27 13:14:00 +08:00
|
|
|
{kids}
|
|
|
|
{renderCloseIcon()}
|
|
|
|
</span>
|
|
|
|
);
|
2020-05-28 09:56:45 +08:00
|
|
|
|
|
|
|
return isNeedWave ? <Wave>{tagNode}</Wave> : tagNode;
|
2020-04-22 10:59:24 +08:00
|
|
|
};
|
|
|
|
|
2020-04-29 11:29:26 +08:00
|
|
|
const Tag = React.forwardRef<unknown, TagProps>(InternalTag) as TagType;
|
2020-04-27 13:14:00 +08:00
|
|
|
|
|
|
|
Tag.displayName = 'Tag';
|
|
|
|
|
2020-04-22 10:59:24 +08:00
|
|
|
Tag.CheckableTag = CheckableTag;
|
2018-06-13 10:57:34 +08:00
|
|
|
|
|
|
|
export default Tag;
|