2022-06-21 10:24:52 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
2016-01-27 01:47:12 +08:00
|
|
|
import classNames from 'classnames';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { PresetColorType, PresetStatusColorType } from '../_util/colors';
|
2023-01-06 11:06:46 +08:00
|
|
|
import { isPresetColor, isPresetStatusColor } from '../_util/colors';
|
2023-03-21 13:02:27 +08:00
|
|
|
import type { LiteralUnion } from '../_util/type';
|
2022-08-15 14:39:02 +08:00
|
|
|
import warning from '../_util/warning';
|
2023-03-21 13:02:27 +08:00
|
|
|
import Wave from '../_util/wave';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-21 10:24:52 +08:00
|
|
|
import CheckableTag from './CheckableTag';
|
2022-03-15 15:33:50 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export type { CheckableTagProps } from './CheckableTag';
|
2017-09-25 22:14:49 +08:00
|
|
|
|
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;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2022-12-28 23:59:55 +08:00
|
|
|
color?: LiteralUnion<PresetColorType | PresetStatusColorType>;
|
2016-07-13 11:14:24 +08:00
|
|
|
closable?: boolean;
|
2020-06-28 22:41:59 +08:00
|
|
|
closeIcon?: React.ReactNode;
|
2022-09-06 21:46:49 +08:00
|
|
|
/** @deprecated `visible` will be removed in next major version. */
|
|
|
|
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;
|
2023-03-21 13:02:27 +08:00
|
|
|
bordered?: boolean;
|
2016-07-09 10:52:16 +08:00
|
|
|
}
|
2016-07-13 11:14:24 +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
|
|
|
|
2023-03-21 13:02:27 +08:00
|
|
|
const InternalTag: React.ForwardRefRenderFunction<HTMLSpanElement, TagProps> = (tagProps, ref) => {
|
|
|
|
const {
|
2020-05-28 09:56:45 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2020-05-28 09:56:45 +08:00
|
|
|
style,
|
|
|
|
children,
|
|
|
|
icon,
|
|
|
|
color,
|
|
|
|
onClose,
|
2020-06-28 22:41:59 +08:00
|
|
|
closeIcon,
|
2020-05-28 09:56:45 +08:00
|
|
|
closable = false,
|
2023-03-21 13:02:27 +08:00
|
|
|
bordered = true,
|
2020-05-28 09:56:45 +08:00
|
|
|
...props
|
2023-03-21 13:02:27 +08:00
|
|
|
} = tagProps;
|
2022-04-07 22:19:56 +08:00
|
|
|
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
|
|
|
|
2022-09-06 21:46:49 +08:00
|
|
|
// Warning for deprecated usage
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(
|
|
|
|
!('visible' in props),
|
|
|
|
'Tag',
|
|
|
|
'`visible` is deprecated, please use `visible && <Tag />` instead.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if ('visible' in props) {
|
|
|
|
setVisible(props.visible!);
|
|
|
|
}
|
|
|
|
}, [props.visible]);
|
|
|
|
|
2023-01-06 11:06:46 +08:00
|
|
|
const isInternalColor = isPresetColor(color) || isPresetStatusColor(color);
|
2018-06-12 22:23:04 +08:00
|
|
|
|
2023-03-21 13:02:27 +08:00
|
|
|
const tagStyle: React.CSSProperties = {
|
2023-01-06 11:06:46 +08:00
|
|
|
backgroundColor: color && !isInternalColor ? color : undefined,
|
2020-05-28 09:56:45 +08:00
|
|
|
...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 prefixCls = getPrefixCls('tag', customizePrefixCls);
|
2022-03-15 15:33:50 +08:00
|
|
|
// Style
|
2022-04-07 22:19:56 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-03-15 15:33:50 +08:00
|
|
|
|
2020-05-28 09:56:45 +08:00
|
|
|
const tagClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
2023-01-06 11:06:46 +08:00
|
|
|
[`${prefixCls}-${color}`]: isInternalColor,
|
|
|
|
[`${prefixCls}-has-color`]: color && !isInternalColor,
|
2020-05-28 09:56:45 +08:00
|
|
|
[`${prefixCls}-hidden`]: !visible,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2023-03-21 13:02:27 +08:00
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
2020-05-28 09:56:45 +08:00
|
|
|
},
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-03-15 15:33:50 +08:00
|
|
|
hashId,
|
2020-05-28 09:56:45 +08:00
|
|
|
);
|
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;
|
|
|
|
}
|
2022-08-16 16:51:18 +08:00
|
|
|
setVisible(false);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-06-12 22:23:04 +08:00
|
|
|
|
2023-03-21 13:02:27 +08:00
|
|
|
const closeIconNode = React.useMemo<React.ReactNode>(() => {
|
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;
|
2023-03-21 13:02:27 +08:00
|
|
|
}, [closable, closeIcon, prefixCls, handleCloseClick]);
|
2018-08-18 14:29:25 +08:00
|
|
|
|
2020-04-27 13:14:00 +08:00
|
|
|
const isNeedWave =
|
2023-01-05 10:24:14 +08:00
|
|
|
typeof props.onClick === 'function' ||
|
|
|
|
(children && (children as React.ReactElement<any>).type === 'a');
|
2023-03-21 13:02:27 +08:00
|
|
|
|
2020-04-27 13:14:00 +08:00
|
|
|
const iconNode = icon || null;
|
2023-03-21 13:02:27 +08:00
|
|
|
|
2020-04-27 13:14:00 +08:00
|
|
|
const kids = iconNode ? (
|
|
|
|
<>
|
|
|
|
{iconNode}
|
|
|
|
<span>{children}</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
children
|
|
|
|
);
|
|
|
|
|
2020-05-28 09:56:45 +08:00
|
|
|
const tagNode = (
|
2022-08-16 16:51:18 +08:00
|
|
|
<span {...props} ref={ref} className={tagClassName} style={tagStyle}>
|
2020-04-27 13:14:00 +08:00
|
|
|
{kids}
|
2023-03-21 13:02:27 +08:00
|
|
|
{closeIconNode}
|
2020-04-27 13:14:00 +08:00
|
|
|
</span>
|
|
|
|
);
|
2020-05-28 09:56:45 +08:00
|
|
|
|
2022-03-15 15:33:50 +08:00
|
|
|
return wrapSSR(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
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Tag.displayName = 'Tag';
|
|
|
|
}
|
2020-04-27 13:14:00 +08:00
|
|
|
|
2020-04-22 10:59:24 +08:00
|
|
|
Tag.CheckableTag = CheckableTag;
|
2018-06-13 10:57:34 +08:00
|
|
|
|
|
|
|
export default Tag;
|