Skip to content

Commit ef75f03

Browse files
authored
fix: warning of useless components (#16)
* test: clean up * fix: warning of extract * chore: fix eslint conflict
1 parent 6ad3cea commit ef75f03

File tree

5 files changed

+66
-35
lines changed

5 files changed

+66
-35
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"antd": "^5.8.5",
4646
"cross-env": "^7.0.1",
4747
"dumi": "^2.1.0",
48-
"eslint": "^7.0.0",
48+
"eslint": "^8.0.0",
4949
"father": "^4.0.0",
5050
"less": "^3.10.3",
5151
"np": "^6.2.0",

src/index.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import React from 'react';
21
import {
32
createCache,
43
extractStyle as extStyle,
54
StyleProvider,
65
} from '@ant-design/cssinjs';
76
import * as antd from 'antd';
7+
import React from 'react';
88
import { renderToString } from 'react-dom/server';
99
import type { CustomRender } from './interface';
10+
1011
const blackList: string[] = [
1112
'ConfigProvider',
1213
'Drawer',
@@ -18,6 +19,26 @@ const blackList: string[] = [
1819
'Tour',
1920
];
2021

22+
const ComponentCustomizeRender: Record<
23+
string,
24+
(component: React.ComponentType<any>) => React.ReactNode
25+
> = {
26+
Affix: (Affix) => (
27+
<Affix>
28+
<div />
29+
</Affix>
30+
),
31+
BackTop: () => <antd.FloatButton.BackTop />,
32+
Dropdown: (Dropdown) => (
33+
<Dropdown menu={{ items: [] }}>
34+
<div />
35+
</Dropdown>
36+
),
37+
Menu: (Menu) => <Menu items={[]} />,
38+
QRCode: (QRCode) => <QRCode value="https://ant.design" />,
39+
Tree: (Tree) => <Tree treeData={[]} />,
40+
};
41+
2142
const defaultNode = () => (
2243
<>
2344
{Object.keys(antd)
@@ -27,16 +48,15 @@ const defaultNode = () => (
2748
)
2849
.map((compName) => {
2950
const Comp = antd[compName];
30-
if (compName === 'Dropdown') {
51+
52+
const renderFunc = ComponentCustomizeRender[compName];
53+
54+
if (renderFunc) {
3155
return (
32-
<Comp
33-
key={compName}
34-
menu={{ items: [] }}
35-
>
36-
<div />
37-
</Comp>
56+
<React.Fragment key={compName}>{renderFunc(Comp)}</React.Fragment>
3857
);
3958
}
59+
4060
return <Comp key={compName} />;
4161
})}
4262
</>

tests/__snapshots__/index.test.tsx.snap

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/index.test.tsx

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { StyleProvider } from '@ant-design/cssinjs';
2-
import { extractStyle } from '../src/index';
32
import { ConfigProvider } from 'antd';
3+
import { extractStyle } from '../src/index';
44

55
const testGreenColor = '#008000';
66
describe('Static-Style-Extract', () => {
77
it('should extract static styles', () => {
88
const cssText = extractStyle();
99
expect(cssText).not.toContain(testGreenColor);
10-
expect(cssText).toMatchSnapshot();
10+
expect(cssText).toContain('.ant-btn');
1111
});
12+
1213
it('should extract static styles with customTheme', () => {
1314
const cssText = extractStyle((node) => (
1415
<ConfigProvider
@@ -22,27 +23,24 @@ describe('Static-Style-Extract', () => {
2223
</ConfigProvider>
2324
));
2425
expect(cssText).toContain(testGreenColor);
25-
expect(cssText).toMatchSnapshot();
2626
});
27+
2728
it('with custom hashPriority', () => {
28-
const cssText = extractStyle(
29-
(node) => (
30-
<StyleProvider hashPriority='high'>
31-
<ConfigProvider
32-
theme={{
33-
token: {
34-
colorPrimary: testGreenColor,
35-
},
36-
}}
37-
>
38-
{node}
39-
</ConfigProvider>
40-
</StyleProvider>
41-
)
42-
);
29+
const cssText = extractStyle((node) => (
30+
<StyleProvider hashPriority="high">
31+
<ConfigProvider
32+
theme={{
33+
token: {
34+
colorPrimary: testGreenColor,
35+
},
36+
}}
37+
>
38+
{node}
39+
</ConfigProvider>
40+
</StyleProvider>
41+
));
4342
expect(cssText).toContain(testGreenColor);
4443
expect(cssText).not.toContain(':where');
45-
expect(cssText).toMatchSnapshot();
4644

4745
const cssText2 = extractStyle((node) => (
4846
<ConfigProvider

tests/ssr.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { extractStyle } from '../src/index';
2+
3+
// Spy useLayoutEffect to avoid warning
4+
jest.mock('react', () => {
5+
const oriReact = jest.requireActual('react');
6+
return {
7+
...oriReact,
8+
useLayoutEffect: oriReact.useEffect,
9+
};
10+
});
11+
12+
describe('Static-Style-Extract.SSR', () => {
13+
it('not warning', () => {
14+
const errSpy = jest.spyOn(console, 'error');
15+
16+
extractStyle();
17+
18+
expect(errSpy).not.toHaveBeenCalled();
19+
});
20+
});

0 commit comments

Comments
 (0)