Skip to content

Commit 463be01

Browse files
zombieJclaude
andauthored
refactor: update component renders from antd (#28)
* refactor: update component renders from antd - Enhance Layout render with all sub-components (Header, Sider, Content, Footer) - Add Input.Password to Input render - Add Input.Group with nested Inputs - Move Space.Addon inside Space.Compact with content - Add default Modal._InternalPanelDoNotUseOrYouWillBeFired - Simplify Cascader and Layout render functions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add Modal.confirm test and update SSR warnings - Add test for Modal.confirm style extraction - Add Input.Group deprecation warning to allowed list - Fix minor formatting issues (trailing commas, semicolons) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: improve type annotations and code formatting - Add explicit types for component render functions (typeof antd.XXX) - Optimize import statements structure - Apply consistent code formatting Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c84cb5a commit 463be01

3 files changed

Lines changed: 67 additions & 44 deletions

File tree

src/index.tsx

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { createCache, extractStyle as extStyle, StyleProvider } from '@ant-design/cssinjs';
2-
import { renderToString } from 'react-dom/server';
1+
import {
2+
createCache,
3+
extractStyle as extStyle,
4+
StyleProvider,
5+
} from '@ant-design/cssinjs';
36
import * as antd from 'antd';
47
import React from 'react';
8+
import { renderToString } from 'react-dom/server';
59
import type { CustomRender } from './interface';
610

7-
const defaultBlackList: string[] = [
8-
'ConfigProvider',
9-
'Grid',
10-
];
11+
const defaultBlackList: string[] = ['ConfigProvider', 'Grid'];
1112

1213
const ComponentCustomizeRender: Record<
1314
string,
@@ -19,6 +20,12 @@ const ComponentCustomizeRender: Record<
1920
</Affix>
2021
),
2122
BackTop: () => <antd.FloatButton.BackTop />,
23+
Cascader: (Cascader: typeof antd.Cascader) => (
24+
<>
25+
<Cascader />
26+
<Cascader.Panel />
27+
</>
28+
),
2229
Dropdown: (Dropdown) => (
2330
<Dropdown menu={{ items: [] }}>
2431
<div />
@@ -39,18 +46,32 @@ const ComponentCustomizeRender: Record<
3946
<Badge.Ribbon />
4047
</>
4148
),
42-
Space: (Space: any) => (
49+
Space: (Space: typeof antd.Space) => (
4350
<>
4451
<Space />
45-
<Space.Addon />
4652
<Space.Compact>
4753
<antd.Button />
54+
<Space.Addon>1</Space.Addon>
4855
</Space.Compact>
4956
</>
5057
),
51-
Modal: (Modal: any) => (
58+
Input: (Input: typeof antd.Input) => (
59+
<>
60+
<Input />
61+
<Input.Group>
62+
<Input />
63+
<Input />
64+
</Input.Group>
65+
<Input.Search />
66+
<Input.TextArea />
67+
<Input.Password />
68+
<Input.OTP />
69+
</>
70+
),
71+
Modal: (Modal: typeof antd.Modal) => (
5272
<>
5373
<Modal />
74+
<Modal._InternalPanelDoNotUseOrYouWillBeFired />
5475
<Modal._InternalPanelDoNotUseOrYouWillBeFired type="confirm" />
5576
</>
5677
),
@@ -63,24 +84,12 @@ const ComponentCustomizeRender: Record<
6384
return <PurePanel />;
6485
},
6586
Layout: (Layout: typeof antd.Layout) => (
66-
<>
67-
<Layout />
68-
<Layout.Sider />
69-
</>
70-
),
71-
Cascader: (Cascader: typeof antd.Cascader) => (
72-
<>
73-
<Cascader options={[]} />
74-
<Cascader.Panel options={[]} />
75-
</>
76-
),
77-
Input: (Input: typeof antd.Input) => (
78-
<>
79-
<Input />
80-
<Input.OTP />
81-
<Input.Search />
82-
<Input.TextArea />
83-
</>
87+
<Layout>
88+
<Layout.Header>Header</Layout.Header>
89+
<Layout.Sider>Sider</Layout.Sider>
90+
<Layout.Content>Content</Layout.Content>
91+
<Layout.Footer>Footer</Layout.Footer>
92+
</Layout>
8493
),
8594
};
8695

@@ -97,7 +106,9 @@ const defaultNode = ({ excludes = [], includes }: NodeProps) => {
97106
{components
98107
.filter(
99108
(name) =>
100-
![...defaultBlackList, ...excludes].includes(name) && (name[0] === name[0].toUpperCase() || ['notification', 'message'].includes(name)),
109+
![...defaultBlackList, ...excludes].includes(name) &&
110+
(name[0] === name[0].toUpperCase() ||
111+
['notification', 'message'].includes(name)),
101112
)
102113
.map((compName) => {
103114
const Comp = antd[compName];
@@ -114,13 +125,17 @@ const defaultNode = ({ excludes = [], includes }: NodeProps) => {
114125
})}
115126
</>
116127
);
117-
}
128+
};
118129

119-
export function extractStyle(arg?: CustomRender | {
120-
customTheme?: CustomRender,
121-
excludes?: string[],
122-
includes?: string[],
123-
}): string {
130+
export function extractStyle(
131+
arg?:
132+
| CustomRender
133+
| {
134+
customTheme?: CustomRender;
135+
excludes?: string[];
136+
includes?: string[];
137+
},
138+
): string {
124139
const cache = createCache();
125140

126141
let customTheme: CustomRender | undefined;
@@ -135,11 +150,13 @@ export function extractStyle(arg?: CustomRender | {
135150
const nodeProps: NodeProps = {
136151
includes,
137152
excludes,
138-
}
153+
};
139154

140155
renderToString(
141156
<StyleProvider cache={cache}>
142-
{customTheme ? customTheme(defaultNode(nodeProps)) : defaultNode(nodeProps)}
157+
{customTheme
158+
? customTheme(defaultNode(nodeProps))
159+
: defaultNode(nodeProps)}
143160
</StyleProvider>,
144161
);
145162

tests/index.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Static-Style-Extract', () => {
4848
token: {
4949
colorPrimary: testGreenColor,
5050
},
51-
hashed: true
51+
hashed: true,
5252
}}
5353
>
5454
{node}
@@ -59,24 +59,29 @@ describe('Static-Style-Extract', () => {
5959

6060
it('whitelist should work', () => {
6161
const cssText = extractStyle({
62-
includes: ['Button']
62+
includes: ['Button'],
6363
});
6464
expect(cssText).toContain('.ant-btn');
6565
expect(cssText).not.toContain('.ant-select');
6666
});
6767

6868
it('blacklist should work', () => {
6969
const cssText = extractStyle({
70-
excludes: ['Card']
70+
excludes: ['Card'],
7171
});
7272
expect(cssText).toContain('.ant-btn');
7373
expect(cssText).toContain('.ant-notification');
7474
expect(cssText).toContain('.ant-message');
7575
expect(cssText).not.toContain('.ant-card');
76-
})
76+
});
7777

7878
it('should extract Layout.Sider', () => {
7979
const cssText = extractStyle();
8080
expect(cssText).toContain('.ant-layout-sider');
8181
});
82+
83+
it('should extract Modal.confirm', () => {
84+
const cssText = extractStyle();
85+
expect(cssText).toContain('.ant-modal-confirm-title');
86+
});
8287
});

tests/ssr.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ jest.mock('react', () => {
1212
describe('Static-Style-Extract.SSR', () => {
1313
it('should not produce unexpected warnings', () => {
1414
const allowedWarnings = [
15-
'Warning: [antd: List] The `List` component is deprecated. And will be removed in next major version.'
16-
]
15+
'Warning: [antd: List] The `List` component is deprecated. And will be removed in next major version.',
16+
'Warning: [antd: Input.Group] `Input.Group` is deprecated. Please use `Space.Compact` instead.',
17+
];
1718

1819
const errSpy = jest.spyOn(console, 'error');
1920

2021
extractStyle();
2122

2223
const filteredCalls = errSpy.mock.calls.filter(([msg]) => {
2324
return !allowedWarnings.some((allowed) => msg === allowed);
24-
})
25-
25+
});
26+
2627
expect(filteredCalls).toHaveLength(0);
2728
});
2829
});

0 commit comments

Comments
 (0)