ant-design/components/tag/index.tsx
kiner-tang(文辉) d5abbad8f1
refactor: deprecatedWarning (#44791)
* refactor: deprecatedWraning

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code

* feat: optimize code
2023-09-13 22:07:33 +08:00

161 lines
4.4 KiB
TypeScript

import * as React from 'react';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import classNames from 'classnames';
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 { devUseWarning } from '../_util/warning';
import Wave from '../_util/wave';
import { ConfigContext } from '../config-provider';
import CheckableTag from './CheckableTag';
import useStyle from './style';
import PresetCmp from './style/presetCmp';
import StatusCmp from './style/statusCmp';
export type { CheckableTagProps } from './CheckableTag';
export interface TagProps extends React.HTMLAttributes<HTMLSpanElement> {
prefixCls?: string;
className?: string;
rootClassName?: string;
color?: LiteralUnion<PresetColorType | PresetStatusColorType>;
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<HTMLElement>) => void;
style?: React.CSSProperties;
icon?: React.ReactNode;
bordered?: boolean;
}
export interface TagType
extends React.ForwardRefExoticComponent<TagProps & React.RefAttributes<HTMLElement>> {
CheckableTag: typeof CheckableTag;
}
const InternalTag: React.ForwardRefRenderFunction<HTMLSpanElement, TagProps> = (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') {
const warning = devUseWarning('Tag');
warning.deprecated(!('visible' in props), 'visible', 'visible && <Tag />');
}
React.useEffect(() => {
if ('visible' in props) {
setVisible(props.visible!);
}
}, [props.visible]);
const isPreset = isPresetColor(color);
const isStatus = isPresetStatusColor(color);
const isInternalColor = isPreset || isStatus;
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<HTMLElement>) => {
e.stopPropagation();
onClose?.(e);
if (e.defaultPrevented) {
return;
}
setVisible(false);
};
const [, mergedCloseIcon] = useClosable(
closable,
closeIcon,
(iconNode: React.ReactNode) =>
iconNode === null ? (
<CloseOutlined className={`${prefixCls}-close-icon`} onClick={handleCloseClick} />
) : (
<span className={`${prefixCls}-close-icon`} onClick={handleCloseClick}>
{iconNode}
</span>
),
null,
false,
);
const isNeedWave =
typeof props.onClick === 'function' ||
(children && (children as React.ReactElement<any>).type === 'a');
const iconNode: React.ReactNode = icon || null;
const kids: React.ReactNode = iconNode ? (
<>
{iconNode}
{children && <span>{children}</span>}
</>
) : (
children
);
const tagNode: React.ReactNode = (
<span {...props} ref={ref} className={tagClassName} style={tagStyle}>
{kids}
{mergedCloseIcon}
{isPreset && <PresetCmp key="preset" prefixCls={prefixCls} />}
{isStatus && <StatusCmp key="status" prefixCls={prefixCls} />}
</span>
);
return wrapSSR(isNeedWave ? <Wave component="Tag">{tagNode}</Wave> : tagNode);
};
const Tag = React.forwardRef<HTMLSpanElement, TagProps>(InternalTag) as TagType;
if (process.env.NODE_ENV !== 'production') {
Tag.displayName = 'Tag';
}
Tag.CheckableTag = CheckableTag;
export default Tag;