Skip to content

Commit 5315496

Browse files
committed
✨ feat: add antdPrefixCls
- Add antdPrefixCls to the return value of useStyles - Add antdPrefixCls to CreateStylesUtils
1 parent 96914f0 commit 5315496

File tree

5 files changed

+79
-8
lines changed

5 files changed

+79
-8
lines changed

src/factories/createStyles/index.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ export const createStylesFactory =
5757

5858
// 函数场景
5959
if (styleOrGetStyle instanceof Function) {
60-
const { stylish, appearance, isDarkMode, prefixCls, iconPrefixCls, ...token } = theme;
60+
const {
61+
stylish,
62+
appearance,
63+
isDarkMode,
64+
prefixCls,
65+
iconPrefixCls,
66+
antdPrefixCls,
67+
...token
68+
} = theme;
6169

6270
// 创建响应式断点选择器的工具函数
6371
// @ts-ignore
@@ -74,6 +82,7 @@ export const createStylesFactory =
7482
isDarkMode,
7583
prefixCls,
7684
iconPrefixCls,
85+
antdPrefixCls,
7786
// 工具函数们
7887
cx,
7988
css: serializeCSS,
@@ -122,8 +131,8 @@ export const createStylesFactory =
122131
}, [props, theme]);
123132

124133
return useMemo(() => {
125-
const { prefixCls, iconPrefixCls, ...res } = theme;
126-
return { styles, cx, theme: res, prefixCls, iconPrefixCls };
134+
const { prefixCls, iconPrefixCls, antdPrefixCls, ...res } = theme;
135+
return { styles, cx, theme: res, prefixCls, iconPrefixCls, antdPrefixCls };
127136
}, [styles, theme]);
128137
};
129138
};

src/factories/createStyles/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ export interface CreateStylesUtils extends CommonStyleUtils {
3232
*/
3333
prefixCls: string;
3434
iconPrefixCls: string;
35+
antdPrefixCls: string;
3536
}
3637

3738
/**
3839
* 最终返回 styles 对象的类型定义
3940
*/
4041
export interface ReturnStyles<T extends BaseReturnType> extends Pick<CommonStyleUtils, 'cx'> {
4142
styles: ReturnStyleToUse<T>;
42-
theme: Omit<Theme, 'prefixCls'>;
43+
theme: Omit<Theme, 'prefixCls' | 'antdPrefixCls'>;
4344
iconPrefixCls: string;
45+
antdPrefixCls: string;
4446
prefixCls: string;
4547
}
4648

src/factories/createUseTheme.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const createUseTheme = (options: CreateUseThemeOptions) => (): Theme => {
2727
const { iconPrefixCls, getPrefixCls } = useContext(ConfigProvider.ConfigContext);
2828

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

@@ -38,14 +38,15 @@ export const createUseTheme = (options: CreateUseThemeOptions) => (): Theme => {
3838
...defaultCustomTheme,
3939
prefixCls,
4040
iconPrefixCls,
41+
antdPrefixCls,
4142
}),
42-
[antdTheme, themeState, defaultCustomTheme, prefixCls, iconPrefixCls],
43+
[antdTheme, themeState, defaultCustomTheme, prefixCls, iconPrefixCls, antdPrefixCls],
4344
);
4445

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

50-
return { ...styledTheme, prefixCls, iconPrefixCls } as Theme;
51+
return { ...styledTheme, prefixCls, iconPrefixCls, antdPrefixCls } as Theme;
5152
};

src/types/theme.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ export interface FullToken extends AntdToken, CustomToken {}
7373
export interface Theme extends FullToken, ThemeContextState {
7474
stylish: FullStylish;
7575
/**
76-
* antd 组件的 prefixCls
76+
* 只有当用户在 createInstance 中传入与字符串 'ant' 不一样的 prefixCls 时,才会返回用户的 prefixCls
77+
* 否则返回 antd 的 prefixCls
7778
*/
7879
prefixCls: string;
7980
iconPrefixCls: string;
81+
/**
82+
* antd 组件的 prefixCls
83+
*/
84+
antdPrefixCls: string;
8085
}

tests/functions/createStyles.test.tsx

+54
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,60 @@ describe('createStyles', () => {
9292
});
9393
});
9494

95+
it('获取 antdPrefixCls,未通过ConfigProvider传入 prefixCls时,拿到的perfixCls应该为ant design默认的"ant"', () => {
96+
const useStyles = createStyles(({ css, antdPrefixCls }) => {
97+
return {
98+
button: css`
99+
&.${antdPrefixCls}-div {
100+
background: lightsteelblue;
101+
border: none;
102+
font-size: 10px;
103+
}
104+
`,
105+
};
106+
});
107+
108+
const App = () => {
109+
const { styles } = useStyles();
110+
111+
return <div className={`${styles.button} ant-div`}>my custom button</div>;
112+
};
113+
114+
const { container } = render(<App />);
115+
expect(container.firstChild).toHaveStyle({ fontSize: '10px' });
116+
});
117+
118+
it('获取 antdPrefixCls,通过ConfigProvider传入 prefixCls 时,拿到的值为传入的prefixCls', () => {
119+
const myCustomAntPrefix = 'my-custom-ant-prefix';
120+
const useStyles = createStyles(({ css, antdPrefixCls }) => {
121+
return {
122+
button: css`
123+
&.${antdPrefixCls}-div {
124+
background: lightsteelblue;
125+
border: none;
126+
font-size: 11px;
127+
}
128+
`,
129+
};
130+
});
131+
132+
const App = () => {
133+
const { styles } = useStyles();
134+
135+
return (
136+
<div className={`${styles.button} ${`${myCustomAntPrefix}-div`}`}>my custom Button</div>
137+
);
138+
};
139+
140+
const wrapper = ({ children }: PropsWithChildren) => (
141+
<ConfigProvider prefixCls={myCustomAntPrefix}>{children}</ConfigProvider>
142+
);
143+
144+
const { container } = render(<App />, { wrapper });
145+
expect(container.firstChild).toHaveClass(`${myCustomAntPrefix}-div`);
146+
expect(container.firstChild).toHaveStyle({ fontSize: '11px' });
147+
});
148+
95149
describe('styleObject 方法', () => {
96150
it('对象模式的用法', () => {
97151
const useStyles = createStyles({

0 commit comments

Comments
 (0)