mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-15 17:31:25 +08:00
7f189d56e5
* style: remove redundant code * feat(style): add preset colour style generation method (cherry picked from commit 1266e42ba27accb48a6544e3eddaa4a94daef00c) * feat: uniform preset colour generation css selector method (cherry picked from commit 5af87e8d5ffcfd23e755946167f200c0565f8222) * chore: merge preset colors (cherry picked from commit 05040dfb703f60a3ea1715326748c508acbf41a6) * chore: update (cherry picked from commit 241b40a1361469487a6a3e8f1ad07a25d250463d) * chore: remove Badge preset inverse colors * chore: remove fix 删除的这部分其实一个 Bug,但是为了给 PR 减负(尽可能单一, 后面新开一个 PR 来修复这个问题 * suggestions accepted Update components/style/presetColor.tsx Co-authored-by: MadCcc <1075746765@qq.com> Co-authored-by: MadCcc <1075746765@qq.com>
144 lines
3.7 KiB
TypeScript
144 lines
3.7 KiB
TypeScript
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
|
import classNames from 'classnames';
|
|
import * as React from 'react';
|
|
import { ConfigContext } from '../config-provider';
|
|
import type { PresetColorType, PresetStatusColorType } from '../_util/colors';
|
|
import { isPresetColor, isPresetStatusColor } from '../_util/colors';
|
|
import Wave from '../_util/wave';
|
|
import warning from '../_util/warning';
|
|
import CheckableTag from './CheckableTag';
|
|
import useStyle from './style';
|
|
import type { LiteralUnion } from '../_util/type';
|
|
|
|
export type { CheckableTagProps } from './CheckableTag';
|
|
|
|
export interface TagProps extends React.HTMLAttributes<HTMLSpanElement> {
|
|
prefixCls?: string;
|
|
className?: string;
|
|
color?: LiteralUnion<PresetColorType | PresetStatusColorType>;
|
|
closable?: boolean;
|
|
closeIcon?: 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;
|
|
}
|
|
|
|
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);
|
|
|
|
// 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]);
|
|
|
|
const isInternalColor = isPresetColor(color) || isPresetStatusColor(color);
|
|
|
|
const tagStyle = {
|
|
backgroundColor: color && !isInternalColor ? color : undefined,
|
|
...style,
|
|
};
|
|
|
|
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
|
// Style
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
const tagClassName = classNames(
|
|
prefixCls,
|
|
{
|
|
[`${prefixCls}-${color}`]: isInternalColor,
|
|
[`${prefixCls}-has-color`]: color && !isInternalColor,
|
|
[`${prefixCls}-hidden`]: !visible,
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
},
|
|
className,
|
|
hashId,
|
|
);
|
|
|
|
const handleCloseClick = (e: React.MouseEvent<HTMLElement>) => {
|
|
e.stopPropagation();
|
|
onClose?.(e);
|
|
|
|
if (e.defaultPrevented) {
|
|
return;
|
|
}
|
|
setVisible(false);
|
|
};
|
|
|
|
const renderCloseIcon = () => {
|
|
if (closable) {
|
|
return closeIcon ? (
|
|
<span className={`${prefixCls}-close-icon`} onClick={handleCloseClick}>
|
|
{closeIcon}
|
|
</span>
|
|
) : (
|
|
<CloseOutlined className={`${prefixCls}-close-icon`} onClick={handleCloseClick} />
|
|
);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const isNeedWave =
|
|
typeof props.onClick === 'function' ||
|
|
(children && (children as React.ReactElement<any>).type === 'a');
|
|
const iconNode = icon || null;
|
|
const kids = iconNode ? (
|
|
<>
|
|
{iconNode}
|
|
<span>{children}</span>
|
|
</>
|
|
) : (
|
|
children
|
|
);
|
|
|
|
const tagNode = (
|
|
<span {...props} ref={ref} className={tagClassName} style={tagStyle}>
|
|
{kids}
|
|
{renderCloseIcon()}
|
|
</span>
|
|
);
|
|
|
|
return wrapSSR(isNeedWave ? <Wave>{tagNode}</Wave> : tagNode);
|
|
};
|
|
|
|
const Tag = React.forwardRef<unknown, TagProps>(InternalTag) as TagType;
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
Tag.displayName = 'Tag';
|
|
}
|
|
|
|
Tag.CheckableTag = CheckableTag;
|
|
|
|
export default Tag;
|