refactor: Tag migrate to genComponentStyleHook (#34914)

This commit is contained in:
MadCcc 2022-04-07 22:19:56 +08:00 committed by GitHub
parent f581159cb5
commit 41452045e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 55 deletions

View File

@ -56,6 +56,7 @@ export interface OverrideToken {
Popover?: {}; Popover?: {};
Select?: SelectComponentToken; Select?: SelectComponentToken;
Slider?: SliderComponentToken; Slider?: SliderComponentToken;
Tag?: {};
Tree?: {}; Tree?: {};
TreeSelect?: {}; TreeSelect?: {};
Typography?: TypographyComponentToken; Typography?: TypographyComponentToken;

View File

@ -25,7 +25,7 @@ const CheckableTag: React.FC<CheckableTagProps> = ({
onClick, onClick,
...restProps ...restProps
}) => { }) => {
const { getPrefixCls, iconPrefixCls } = React.useContext(ConfigContext); const { getPrefixCls } = React.useContext(ConfigContext);
const handleClick = (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => { const handleClick = (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
onChange?.(!checked); onChange?.(!checked);
@ -34,7 +34,7 @@ const CheckableTag: React.FC<CheckableTagProps> = ({
const prefixCls = getPrefixCls('tag', customizePrefixCls); const prefixCls = getPrefixCls('tag', customizePrefixCls);
// Style // Style
const [wrapSSR, hashId] = useStyle(prefixCls, iconPrefixCls); const [wrapSSR, hashId] = useStyle(prefixCls);
const cls = classNames( const cls = classNames(
prefixCls, prefixCls,

View File

@ -53,7 +53,7 @@ const InternalTag: React.ForwardRefRenderFunction<HTMLSpanElement, TagProps> = (
}, },
ref, ref,
) => { ) => {
const { getPrefixCls, direction, iconPrefixCls } = React.useContext(ConfigContext); const { getPrefixCls, direction } = React.useContext(ConfigContext);
const [visible, setVisible] = React.useState(true); const [visible, setVisible] = React.useState(true);
React.useEffect(() => { React.useEffect(() => {
@ -77,7 +77,7 @@ const InternalTag: React.ForwardRefRenderFunction<HTMLSpanElement, TagProps> = (
const presetColor = isPresetColor(); const presetColor = isPresetColor();
const prefixCls = getPrefixCls('tag', customizePrefixCls); const prefixCls = getPrefixCls('tag', customizePrefixCls);
// Style // Style
const [wrapSSR, hashId] = useStyle(prefixCls, iconPrefixCls); const [wrapSSR, hashId] = useStyle(prefixCls);
const tagClassName = classNames( const tagClassName = classNames(
prefixCls, prefixCls,

View File

@ -3,15 +3,13 @@ import { CSSInterpolation, CSSObject } from '@ant-design/cssinjs';
import capitalize from '../../_util/capitalize'; import capitalize from '../../_util/capitalize';
import { import {
PresetColorType, PresetColorType,
DerivativeToken,
useStyleRegister,
useToken,
resetComponent, resetComponent,
UseComponentStyleResult,
PresetColors, PresetColors,
FullToken,
genComponentStyleHook,
} from '../../_util/theme'; } from '../../_util/theme';
interface TagToken extends DerivativeToken { interface TagToken extends FullToken<'Tag'> {
tagFontSize: number; tagFontSize: number;
tagLineHeight: React.CSSProperties['lineHeight']; tagLineHeight: React.CSSProperties['lineHeight'];
tagDefaultBg: string; tagDefaultBg: string;
@ -23,14 +21,13 @@ interface TagToken extends DerivativeToken {
type CssVariableType = 'Success' | 'Info' | 'Error' | 'Warning'; type CssVariableType = 'Success' | 'Info' | 'Error' | 'Warning';
const genTagStatusStyle = ( const genTagStatusStyle = (
prefixCls: string,
token: TagToken, token: TagToken,
status: 'success' | 'processing' | 'error' | 'warning', status: 'success' | 'processing' | 'error' | 'warning',
cssVariableType: CssVariableType, cssVariableType: CssVariableType,
): CSSInterpolation => { ): CSSInterpolation => {
const capitalizedCssVariableType = capitalize<CssVariableType>(cssVariableType); const capitalizedCssVariableType = capitalize<CssVariableType>(cssVariableType);
return { return {
[`.${prefixCls}-${status}`]: { [`${token.componentCls}-${status}`]: {
color: token[`color${cssVariableType}`], color: token[`color${cssVariableType}`],
background: token[`colorBg${capitalizedCssVariableType}`], background: token[`colorBg${capitalizedCssVariableType}`],
borderColor: token[`color${capitalizedCssVariableType}Secondary`], borderColor: token[`color${capitalizedCssVariableType}Secondary`],
@ -39,7 +36,7 @@ const genTagStatusStyle = (
}; };
// FIXME: special preset colors // FIXME: special preset colors
const genTagColorStyle = (prefixCls: string, token: TagToken): CSSInterpolation => const genTagColorStyle = (token: TagToken): CSSInterpolation =>
PresetColors.reduce((prev: CSSObject, colorKey: keyof PresetColorType) => { PresetColors.reduce((prev: CSSObject, colorKey: keyof PresetColorType) => {
const lightColor = token[`${colorKey}-1`]; const lightColor = token[`${colorKey}-1`];
const lightBorderColor = token[`${colorKey}-3`]; const lightBorderColor = token[`${colorKey}-3`];
@ -47,12 +44,12 @@ const genTagColorStyle = (prefixCls: string, token: TagToken): CSSInterpolation
const textColor = token[`${colorKey}-7`]; const textColor = token[`${colorKey}-7`];
return { return {
...prev, ...prev,
[`.${prefixCls}-${colorKey}`]: { [`${token.componentCls}-${colorKey}`]: {
color: textColor, color: textColor,
background: lightColor, background: lightColor,
borderColor: lightBorderColor, borderColor: lightBorderColor,
}, },
[`.${prefixCls}-${colorKey}-inverse`]: { [`${token.componentCls}-${colorKey}-inverse`]: {
color: token.colorTextLightSolid, color: token.colorTextLightSolid,
background: darkColor, background: darkColor,
borderColor: darkColor, borderColor: darkColor,
@ -60,17 +57,13 @@ const genTagColorStyle = (prefixCls: string, token: TagToken): CSSInterpolation
}; };
}, {} as CSSObject); }, {} as CSSObject);
const genBaseStyle = ( const genBaseStyle = (token: TagToken): CSSInterpolation => ({
prefixCls: string,
iconPrefixCls: string,
token: TagToken,
): CSSInterpolation => ({
// Result // Result
[`.${prefixCls}`]: { [token.componentCls]: {
...resetComponent(token), ...resetComponent(token),
display: 'inline-block', display: 'inline-block',
height: 'auto', height: 'auto',
marginInlineStart: token.marginXS, marginInlineEnd: token.marginXS,
// FIXME: hard code // FIXME: hard code
padding: '0 7px', padding: '0 7px',
fontSize: token.tagFontSize, fontSize: token.tagFontSize,
@ -93,7 +86,7 @@ const genBaseStyle = (
color: token.tagDefaultColor, color: token.tagDefaultColor,
}, },
[`.${prefixCls}-close-icon`]: { [`${token.componentCls}-close-icon`]: {
// FIXME: hard code // FIXME: hard code
marginInlineStart: 3, marginInlineStart: 3,
color: token.colorTextSecondary, color: token.colorTextSecondary,
@ -110,7 +103,7 @@ const genBaseStyle = (
[`&&-has-color`]: { [`&&-has-color`]: {
borderColor: 'transparent', borderColor: 'transparent',
[`&, a, a:hover, .${iconPrefixCls}-close, .${iconPrefixCls}-close:hover`]: { [`&, a, a:hover, ${token.iconCls}-close, ${token.iconCls}-close:hover`]: {
color: token.colorTextLightSolid, color: token.colorTextLightSolid,
}, },
}, },
@ -142,18 +135,15 @@ const genBaseStyle = (
}, },
// To ensure that a space will be placed between character and `Icon`. // To ensure that a space will be placed between character and `Icon`.
[`> .${iconPrefixCls} + span, > span + .${iconPrefixCls}`]: { [`> ${token.iconCls} + span, > span + ${token.iconCls}`]: {
// FIXME: hard code // FIXME: hard code
marginInlineStart: 7, marginInlineStart: 7,
}, },
}, },
}); });
export const genTagStyle = ( // ============================== Export ==============================
prefixCls: string, export default genComponentStyleHook('Tag', token => {
iconPrefixCls: string,
token: DerivativeToken,
): CSSInterpolation => {
const tagFontSize = token.fontSizeSM; const tagFontSize = token.fontSizeSM;
// FIXME: hard code // FIXME: hard code
const tagLineHeight = '18px'; const tagLineHeight = '18px';
@ -169,30 +159,11 @@ export const genTagStyle = (
}; };
return [ return [
genBaseStyle(prefixCls, iconPrefixCls, tagToken), genBaseStyle(tagToken),
genTagColorStyle(prefixCls, tagToken), genTagColorStyle(tagToken),
genTagStatusStyle(prefixCls, tagToken, 'success', 'Success'), genTagStatusStyle(tagToken, 'success', 'Success'),
genTagStatusStyle(prefixCls, tagToken, 'processing', 'Info'), genTagStatusStyle(tagToken, 'processing', 'Info'),
genTagStatusStyle(prefixCls, tagToken, 'error', 'Error'), genTagStatusStyle(tagToken, 'error', 'Error'),
genTagStatusStyle(prefixCls, tagToken, 'warning', 'Warning'), genTagStatusStyle(tagToken, 'warning', 'Warning'),
]; ];
}; });
// ============================== Export ==============================
export function getStyle(prefixCls: string, iconPrefixCls: string, token: DerivativeToken) {
return [genTagStyle(prefixCls, iconPrefixCls, token)];
}
export default function useStyle(
prefixCls: string,
iconPrefixCls: string,
): UseComponentStyleResult {
const [theme, token, hashId] = useToken();
return [
useStyleRegister({ theme, token, hashId, path: [prefixCls] }, () =>
getStyle(prefixCls, iconPrefixCls, token),
),
hashId,
];
}