From 61af36635d554c2b19047505e37793b4231df24a Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Fri, 8 Dec 2023 14:05:44 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20fix:=20=E5=85=BC=E5=AE=B9=20createInsta?= =?UTF-8?q?nce=20=E6=97=B6=E8=87=AA=E5=AE=9A=E4=B9=89=20prefixCls=20?= =?UTF-8?q?=E7=9A=84=E5=9C=BA=E6=99=AF=20(#126)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/factories/createUseTheme.ts | 4 +- .../createInstance.test.tsx.snap | 45 +++++++++++++++++++ tests/functions/createInstance.test.tsx | 42 ++++++++++++++++- 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 tests/functions/__snapshots__/createInstance.test.tsx.snap diff --git a/src/factories/createUseTheme.ts b/src/factories/createUseTheme.ts index e276bb54..bda0f676 100644 --- a/src/factories/createUseTheme.ts +++ b/src/factories/createUseTheme.ts @@ -27,7 +27,9 @@ export const createUseTheme = (options: CreateUseThemeOptions) => (): Theme => { const { iconPrefixCls, getPrefixCls } = useContext(ConfigProvider.ConfigContext); const antdPrefixCls = getPrefixCls(); - const prefixCls = outPrefixCls ?? antdPrefixCls; + // 只有当用户在 createInstance 中传入与 ant 不一样的 prefixCls 时,才会使用用户的 prefixCls + // 否则其他情况下都优先使用 antd 的 prefixCls + const prefixCls = outPrefixCls && outPrefixCls !== 'ant' ? outPrefixCls : antdPrefixCls; const initTheme = useMemo( () => ({ diff --git a/tests/functions/__snapshots__/createInstance.test.tsx.snap b/tests/functions/__snapshots__/createInstance.test.tsx.snap new file mode 100644 index 00000000..55b4569d --- /dev/null +++ b/tests/functions/__snapshots__/createInstance.test.tsx.snap @@ -0,0 +1,45 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`createInstance > 自定义 prefixCls 时,会采用 instance 的 prefixCls 而不是 CP 的prefixCls 1`] = ` +.emotion-0.test-btn { + background: lightsteelblue; + border: none; + color: royalblue; +} + +.emotion-0 .cpicon { + color: darkblue; +} + + +`; diff --git a/tests/functions/createInstance.test.tsx b/tests/functions/createInstance.test.tsx index 9ac5ef85..097d8918 100644 --- a/tests/functions/createInstance.test.tsx +++ b/tests/functions/createInstance.test.tsx @@ -1,5 +1,8 @@ -import { renderHook } from '@testing-library/react'; +import { SmileOutlined } from '@ant-design/icons'; +import { render, renderHook } from '@testing-library/react'; +import { Button, ConfigProvider } from 'antd'; import { createInstance } from 'antd-style'; +import { PropsWithChildren } from 'react'; interface TestCustomToken { x?: string; @@ -32,6 +35,43 @@ describe('createInstance', () => { expect(result.current.prefixCls).toEqual('test'); }); + it('自定义 prefixCls 时,会采用 instance 的 prefixCls 而不是 CP 的prefixCls', () => { + const useStyles = instance.createStyles(({ css, prefixCls, iconPrefixCls }) => { + return { + button: css` + &.${prefixCls}-btn { + background: lightsteelblue; + border: none; + color: royalblue; + } + + .${iconPrefixCls} { + color: darkblue; + } + `, + }; + }); + + const App = () => { + const { styles } = useStyles(); + + return ( + + ); + }; + const wrapper = ({ children }: PropsWithChildren) => ( + + {children} + + ); + + const { container } = render(, { wrapper }); + + expect(container.firstChild).toMatchSnapshot(); + }); + it('useTheme 拿到的 token 里有 x 为 123', () => { const { result } = renderHook(instance.useTheme);