Skip to content

Commit

Permalink
✨ feat: add antdPrefixCls
Browse files Browse the repository at this point in the history
- Add antdPrefixCls to the return value of useStyles
- Add antdPrefixCls to CreateStylesUtils
  • Loading branch information
DBvc committed Apr 9, 2024
1 parent 4187b95 commit 9fca6f1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/factories/createStyles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ export const createStylesFactory =

// 函数场景
if (styleOrGetStyle instanceof Function) {
const { stylish, appearance, isDarkMode, prefixCls, iconPrefixCls, ...token } = theme;
const {
stylish,
appearance,
isDarkMode,
prefixCls,
iconPrefixCls,
antdPrefixCls,
...token
} = theme;

// 创建响应式断点选择器的工具函数
// @ts-ignore
Expand All @@ -74,6 +82,7 @@ export const createStylesFactory =
isDarkMode,
prefixCls,
iconPrefixCls,
antdPrefixCls,
// 工具函数们
cx,
css: serializeCSS,
Expand Down Expand Up @@ -122,8 +131,8 @@ export const createStylesFactory =
}, [props, theme]);

return useMemo(() => {
const { prefixCls, iconPrefixCls, ...res } = theme;
return { styles, cx, theme: res, prefixCls, iconPrefixCls };
const { prefixCls, iconPrefixCls, antdPrefixCls, ...res } = theme;
return { styles, cx, theme: res, prefixCls, iconPrefixCls, antdPrefixCls };
}, [styles, theme]);
};
};
4 changes: 3 additions & 1 deletion src/factories/createStyles/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ export interface CreateStylesUtils extends CommonStyleUtils {
*/
prefixCls: string;
iconPrefixCls: string;
antdPrefixCls: string;
}

/**
* 最终返回 styles 对象的类型定义
*/
export interface ReturnStyles<T extends BaseReturnType> extends Pick<CommonStyleUtils, 'cx'> {
styles: ReturnStyleToUse<T>;
theme: Omit<Theme, 'prefixCls'>;
theme: Omit<Theme, 'prefixCls' | 'antdPrefixCls'>;
iconPrefixCls: string;
antdPrefixCls: string;
prefixCls: string;
}

Expand Down
7 changes: 4 additions & 3 deletions src/factories/createUseTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createUseTheme = (options: CreateUseThemeOptions) => (): Theme => {
const { iconPrefixCls, getPrefixCls } = useContext(ConfigProvider.ConfigContext);

const antdPrefixCls = getPrefixCls();
// 只有当用户在 createInstance 中传入与 ant 不一样的 prefixCls 时,才会使用用户的 prefixCls
// 只有当用户在 createInstance 中传入与字符串 'ant' 不一样的 prefixCls 时,才会使用用户的 prefixCls
// 否则其他情况下都优先使用 antd 的 prefixCls
const prefixCls = outPrefixCls && outPrefixCls !== 'ant' ? outPrefixCls : antdPrefixCls;

Expand All @@ -38,14 +38,15 @@ export const createUseTheme = (options: CreateUseThemeOptions) => (): Theme => {
...defaultCustomTheme,
prefixCls,
iconPrefixCls,
antdPrefixCls,
}),
[antdTheme, themeState, defaultCustomTheme, prefixCls, iconPrefixCls],
[antdTheme, themeState, defaultCustomTheme, prefixCls, iconPrefixCls, antdPrefixCls],
);

// 如果是个空值,说明没有套 Provider,返回 antdTheme 的默认值
if (!styledTheme || Object.keys(styledTheme).length === 0) {
return initTheme;
}

return { ...styledTheme, prefixCls, iconPrefixCls } as Theme;
return { ...styledTheme, prefixCls, iconPrefixCls, antdPrefixCls } as Theme;
};
7 changes: 6 additions & 1 deletion src/types/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ export interface FullToken extends AntdToken, CustomToken {}
export interface Theme extends FullToken, ThemeContextState {
stylish: FullStylish;
/**
* antd 组件的 prefixCls
* 只有当用户在 createInstance 中传入与字符串 'ant' 不一样的 prefixCls 时,才会返回用户的 prefixCls
* 否则返回 antd 的 prefixCls
*/
prefixCls: string;
iconPrefixCls: string;
/**
* antd 组件的 prefixCls
*/
antdPrefixCls: string;
}

0 comments on commit 9fca6f1

Please sign in to comment.