Skip to content

Commit 4131b4d

Browse files
authored
refactor(cssinjs): simplify code of style (ant-design#57023)
* refactor(cssinjs): simplify code of style * update * update * update * update * update
1 parent 0063d36 commit 4131b4d

9 files changed

Lines changed: 26 additions & 35 deletions

File tree

components/_util/__tests__/responsiveObserve.test.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import useResponsiveObserver from '../responsiveObserver';
55

66
describe('Test ResponsiveObserve', () => {
77
it('test ResponsiveObserve subscribe and unsubscribe', () => {
8-
let responsiveRef: any = null;
8+
const responsiveRef = React.createRef<ReturnType<typeof useResponsiveObserver>>();
99
const Demo: React.FC = () => {
1010
const responsiveObserver = useResponsiveObserver();
11-
responsiveRef = responsiveObserver;
11+
responsiveRef.current = responsiveObserver;
1212
return null;
1313
};
1414
render(<Demo />);
1515
const subscribeFunc = jest.fn();
16-
const token = responsiveRef.subscribe(subscribeFunc);
17-
expect(responsiveRef.matchHandlers[responsiveRef.responsiveMap.xs].mql.matches).toBeTruthy();
16+
const instance = responsiveRef.current;
17+
const token = instance?.subscribe(subscribeFunc);
18+
expect(instance?.matchHandlers[instance?.responsiveMap.xs].mql.matches).toBeTruthy();
1819
expect(subscribeFunc).toHaveBeenCalledTimes(1);
19-
responsiveRef.unsubscribe(token);
20+
if (token !== undefined) {
21+
instance?.unsubscribe(token);
22+
}
2023
expect(
21-
responsiveRef.matchHandlers[responsiveRef.responsiveMap.xs].mql?.removeEventListener,
24+
instance?.matchHandlers[instance?.responsiveMap.xs].mql?.removeEventListener,
2225
).toHaveBeenCalled();
2326
});
2427
});

components/cascader/style/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const genBaseStyle: GenerateStyle<CascaderToken> = (token) => {
9292
};
9393

9494
// ============================== Export ==============================
95-
export const prepareComponentToken = (token: GlobalToken) => {
95+
export const prepareComponentToken = (token: GlobalToken): ComponentToken => {
9696
const itemPaddingVertical = Math.round(
9797
(token.controlHeight - token.fontSize * token.lineHeight) / 2,
9898
);

components/input/style/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@ export const genPlaceholderStyle = (color: string): CSSObject => ({
3333
},
3434
});
3535

36-
export const genActiveStyle = (token: InputToken) => ({
36+
export const genActiveStyle: GenerateStyle<InputToken, CSSObject> = (token) => ({
3737
borderColor: token.activeBorderColor,
3838
boxShadow: token.activeShadow,
3939
outline: 0,
4040
backgroundColor: token.activeBg,
4141
});
4242

43-
export const genInputLargeStyle = (token: InputToken): CSSObject => {
43+
export const genInputLargeStyle: GenerateStyle<InputToken, CSSObject> = (token) => {
4444
const { paddingBlockLG, lineHeightLG, borderRadiusLG, paddingInlineLG } = token;
45-
4645
return {
4746
padding: `${unit(paddingBlockLG)} ${unit(paddingInlineLG)}`,
4847
fontSize: token.inputFontSizeLG,
@@ -51,7 +50,7 @@ export const genInputLargeStyle = (token: InputToken): CSSObject => {
5150
};
5251
};
5352

54-
export const genInputSmallStyle = (token: InputToken): CSSObject => ({
53+
export const genInputSmallStyle: GenerateStyle<InputToken, CSSObject> = (token) => ({
5554
padding: `${unit(token.paddingBlockSM)} ${unit(token.paddingInlineSM)}`,
5655
fontSize: token.inputFontSizeSM,
5756
borderRadius: token.borderRadiusSM,
@@ -92,7 +91,7 @@ export const genBasicInputStyle = (
9291
},
9392
});
9493

95-
export const genInputGroupStyle = (token: InputToken): CSSObject => {
94+
export const genInputGroupStyle: GenerateStyle<InputToken, CSSObject> = (token) => {
9695
const { componentCls, antCls } = token;
9796

9897
return {
@@ -389,7 +388,7 @@ export const genInputStyle: GenerateStyle<InputToken, CSSObject> = (token) => {
389388
};
390389
};
391390

392-
const genAllowClearStyle = (token: InputToken): CSSObject => {
391+
const genAllowClearStyle: GenerateStyle<InputToken, CSSObject> = (token) => {
393392
const { componentCls } = token;
394393
return {
395394
// ========================= Input =========================

components/input/style/search.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,4 @@ const genSearchStyle: GenerateStyle<FullToken<'Input'>, CSSObject> = (token) =>
3232
};
3333
};
3434

35-
export default genStyleHooks(['Input', 'Search'], (token) => {
36-
return [genSearchStyle(token)];
37-
});
35+
export default genStyleHooks(['Input', 'Search'], genSearchStyle);

components/masonry/style/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ export const genMasonryStyle: GenerateStyle<MasonryToken, CSSObject> = (token) =
5959
};
6060
};
6161

62-
export default genStyleHooks('Masonry', (token) => [genMasonryStyle(token)]);
62+
export default genStyleHooks('Masonry', genMasonryStyle);

components/popconfirm/style/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ export const prepareComponentToken: GetDefaultToken<'Popconfirm'> = (token) => {
9090
};
9191
};
9292

93-
export default genStyleHooks('Popconfirm', (token) => genBaseStyle(token), prepareComponentToken, {
93+
export default genStyleHooks('Popconfirm', genBaseStyle, prepareComponentToken, {
9494
resetStyle: false,
9595
});

components/result/style/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ const genResultStyle: GenerateStyle<ResultToken> = (token) => [
137137
genStatusIconStyle(token),
138138
];
139139

140-
const getStyle: GenerateStyle<ResultToken> = (token) => genResultStyle(token);
141-
142140
// ============================== Export ==============================
143141
export const prepareComponentToken: GetDefaultToken<'Result'> = (token) => ({
144142
titleFontSize: token.fontSizeHeading3,
@@ -163,8 +161,7 @@ export default genStyleHooks(
163161
imageWidth: 250,
164162
imageHeight: 295,
165163
});
166-
167-
return [getStyle(resultToken)];
164+
return genResultStyle(resultToken);
168165
},
169166
prepareComponentToken,
170167
);

components/space/style/compact.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@ const genSpaceCompactStyle: GenerateStyle<SpaceToken, CSSObject> = (token) => {
3434
};
3535

3636
// ============================== Export ==============================
37-
export default genStyleHooks(
38-
['Space', 'Compact'],
39-
(token) => [genSpaceCompactStyle(token)],
40-
() => ({}),
41-
{
42-
// Space component don't apply extra font style
43-
// https://github.com/ant-design/ant-design/issues/40315
44-
resetStyle: false,
45-
},
46-
);
37+
export default genStyleHooks(['Space', 'Compact'], genSpaceCompactStyle, () => ({}), {
38+
// Space component don't apply extra font style
39+
// https://github.com/ant-design/ant-design/issues/40315
40+
resetStyle: false,
41+
});

tests/setupAfterEnv.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ expect.addSnapshotSerializer({
9494
container.innerHTML = html;
9595

9696
const children = Array.from(container.childNodes).filter(
97-
(node: any) =>
98-
// Ignore `link` node since React 18 or blew not support this
99-
node.nodeName !== 'LINK',
97+
// Ignore `link` node since React 18 or below not support this
98+
(node) => node.nodeName !== 'LINK',
10099
);
101100

102101
// Clean up `data-reactroot` since React 18 do not have this

0 commit comments

Comments
 (0)