Skip to content

Commit

Permalink
fix: warning of extract
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Aug 28, 2024
1 parent ed7b0f5 commit 0612c6c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import {
createCache,
extractStyle as extStyle,
StyleProvider,
} from '@ant-design/cssinjs';
import * as antd from 'antd';
import React from 'react';
import { renderToString } from 'react-dom/server';
import type { CustomRender } from './interface';

const blackList: string[] = [
'ConfigProvider',
'Drawer',
Expand All @@ -18,6 +19,26 @@ const blackList: string[] = [
'Tour',
];

const ComponentCustomizeRender: Record<
string,
(component: React.ComponentType<any>) => React.ReactNode
> = {
Affix: (Affix) => (
<Affix>
<div />
</Affix>
),
BackTop: () => <antd.FloatButton.BackTop />,
Dropdown: (Dropdown) => (
<Dropdown menu={{ items: [] }}>
<div />
</Dropdown>
),
Menu: (Menu) => <Menu items={[]} />,
QRCode: (QRCode) => <QRCode value="https://ant.design" />,
Tree: (Tree) => <Tree treeData={[]} />,
};

const defaultNode = () => (
<>
{Object.keys(antd)
Expand All @@ -27,16 +48,15 @@ const defaultNode = () => (
)
.map((compName) => {
const Comp = antd[compName];
if (compName === 'Dropdown') {

const renderFunc = ComponentCustomizeRender[compName];

if (renderFunc) {
return (
<Comp
key={compName}
menu={{ items: [] }}
>
<div />
</Comp>
<React.Fragment key={compName}>{renderFunc(Comp)}</React.Fragment>
);
}

return <Comp key={compName} />;
})}
</>
Expand Down
20 changes: 20 additions & 0 deletions tests/ssr.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { extractStyle } from '../src/index';

// Spy useLayoutEffect to avoid warning
jest.mock('react', () => {
const oriReact = jest.requireActual('react');
return {
...oriReact,
useLayoutEffect: oriReact.useEffect,
};
});

describe('Static-Style-Extract.SSR', () => {
it('not warning', () => {
const errSpy = jest.spyOn(console, 'error');

extractStyle();

expect(errSpy).not.toHaveBeenCalled();
});
});

0 comments on commit 0612c6c

Please sign in to comment.