|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
|
|
|
|
|
|
|
import CheckableTag from './CheckableTag';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import {
|
|
|
|
PresetColorTypes,
|
|
|
|
PresetStatusColorTypes,
|
|
|
|
PresetColorType,
|
|
|
|
PresetStatusColorType,
|
|
|
|
} from '../_util/colors';
|
|
|
|
import Wave from '../_util/wave';
|
|
|
|
import { LiteralUnion } from '../_util/type';
|
|
|
|
|
|
|
|
export { CheckableTagProps } from './CheckableTag';
|
|
|
|
|
|
|
|
export interface TagProps extends React.HTMLAttributes<HTMLSpanElement> {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
color?: LiteralUnion<PresetColorType | PresetStatusColorType, string>;
|
|
|
|
closable?: boolean;
|
|
|
|
closeIcon?: React.ReactNode;
|
|
|
|
visible?: boolean;
|
|
|
|
onClose?: (e: React.MouseEvent<HTMLElement>) => void;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
icon?: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
|
|
|
|
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);
|
|
|
|
|
|
|
|
export interface TagType
|
|
|
|
extends React.ForwardRefExoticComponent<TagProps & React.RefAttributes<HTMLElement>> {
|
|
|
|
CheckableTag: typeof CheckableTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
const InternalTag: React.ForwardRefRenderFunction<HTMLSpanElement, TagProps> = (
|
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
children,
|
|
|
|
icon,
|
|
|
|
color,
|
|
|
|
onClose,
|
|
|
|
closeIcon,
|
|
|
|
closable = false,
|
|
|
|
...props
|
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const [visible, setVisible] = React.useState(true);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if ('visible' in props) {
|
|
|
|
setVisible(props.visible!);
|
|
|
|
}
|
|
|
|
}, [props.visible]);
|
|
|
|
|
|
|
|
const isPresetColor = (): boolean => {
|
|
|
|
if (!color) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
|
|
|
|
};
|
|
|
|
|
|
|
|