'use client'; import CloseOutlined from '@ant-design/icons/CloseOutlined'; import classNames from 'classnames'; import * as React from 'react'; import type { PresetColorType, PresetStatusColorType } from '../_util/colors'; import { isPresetColor, isPresetStatusColor } from '../_util/colors'; import useClosable from '../_util/hooks/useClosable'; import type { LiteralUnion } from '../_util/type'; import warning from '../_util/warning'; import Wave from '../_util/wave'; import { ConfigContext } from '../config-provider'; import CheckableTag from './CheckableTag'; import useStyle from './style'; export type { CheckableTagProps } from './CheckableTag'; export interface TagProps extends React.HTMLAttributes { prefixCls?: string; className?: string; rootClassName?: string; color?: LiteralUnion; closable?: boolean; /** Advised to use closeIcon instead. */ closeIcon?: boolean | React.ReactNode; /** @deprecated `visible` will be removed in next major version. */ visible?: boolean; onClose?: (e: React.MouseEvent) => void; style?: React.CSSProperties; icon?: React.ReactNode; bordered?: boolean; } export interface TagType extends React.ForwardRefExoticComponent> { CheckableTag: typeof CheckableTag; } const InternalTag: React.ForwardRefRenderFunction = (tagProps, ref) => { const { prefixCls: customizePrefixCls, className, rootClassName, style, children, icon, color, onClose, closeIcon, closable, bordered = true, ...props } = tagProps; const { getPrefixCls, direction, tag } = React.useContext(ConfigContext); const [visible, setVisible] = React.useState(true); // Warning for deprecated usage if (process.env.NODE_ENV !== 'production') { warning( !('visible' in props), 'Tag', '`visible` is deprecated, please use `visible && ` instead.', ); } React.useEffect(() => { if ('visible' in props) { setVisible(props.visible!); } }, [props.visible]); const isInternalColor = isPresetColor(color) || isPresetStatusColor(color); const tagStyle: React.CSSProperties = { backgroundColor: color && !isInternalColor ? color : undefined, ...tag?.style, ...style, }; const prefixCls = getPrefixCls('tag', customizePrefixCls); // Style const [wrapSSR, hashId] = useStyle(prefixCls); const tagClassName = classNames( prefixCls, tag?.className, { [`${prefixCls}-${color}`]: isInternalColor, [`${prefixCls}-has-color`]: color && !isInternalColor, [`${prefixCls}-hidden`]: !visible, [`${prefixCls}-rtl`]: direction === 'rtl', [`${prefixCls}-borderless`]: !bordered, }, className, rootClassName, hashId, ); const handleCloseClick = (e: React.MouseEvent) => { e.stopPropagation(); onClose?.(e); if (e.defaultPrevented) { return; } setVisible(false); }; const [, mergedCloseIcon] = useClosable( closable, closeIcon, (iconNode: React.ReactNode) => iconNode === null ? ( ) : ( {iconNode} ), null, false, ); const isNeedWave = typeof props.onClick === 'function' || (children && (children as React.ReactElement).type === 'a'); const iconNode: React.ReactNode = icon || null; const kids: React.ReactNode = iconNode ? ( <> {iconNode} {children && {children}} ) : ( children ); const tagNode: React.ReactNode = ( {kids} {mergedCloseIcon} ); return wrapSSR(isNeedWave ? {tagNode} : tagNode); }; const Tag = React.forwardRef(InternalTag) as TagType; if (process.env.NODE_ENV !== 'production') { Tag.displayName = 'Tag'; } Tag.CheckableTag = CheckableTag; export default Tag;