From efd6ec5808e07ea45957c29936c15a9150043818 Mon Sep 17 00:00:00 2001 From: dengfuping Date: Thu, 25 Apr 2024 20:36:03 +0800 Subject: [PATCH] fix(design): ConfigProvider should not inject StaticFunction many times by default --- .../__tests__/injectStaticFunction.test.tsx | 18 ++++++++++++++++++ packages/design/src/config-provider/index.tsx | 4 ++-- packages/design/src/static-function/index.tsx | 6 +++++- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 packages/design/src/config-provider/__tests__/injectStaticFunction.test.tsx diff --git a/packages/design/src/config-provider/__tests__/injectStaticFunction.test.tsx b/packages/design/src/config-provider/__tests__/injectStaticFunction.test.tsx new file mode 100644 index 000000000..4fd54cde6 --- /dev/null +++ b/packages/design/src/config-provider/__tests__/injectStaticFunction.test.tsx @@ -0,0 +1,18 @@ +import React, { useContext } from 'react'; +import { render } from '@testing-library/react'; +import { ConfigProvider, useToken } from '@oceanbase/design'; +import { injectedStaticFunction } from '../../static-function'; + +describe('ConfigProvider injectStaticFunction', () => { + it('injectStaticFunction', () => { + const Child = () => { + expect(injectedStaticFunction).toBe(true); + return
; + }; + render( + + + + ); + }); +}); diff --git a/packages/design/src/config-provider/index.tsx b/packages/design/src/config-provider/index.tsx index acc59ed35..a6efa8925 100644 --- a/packages/design/src/config-provider/index.tsx +++ b/packages/design/src/config-provider/index.tsx @@ -13,7 +13,7 @@ import type { StyleProviderProps } from '@ant-design/cssinjs'; import StyleContext from '@ant-design/cssinjs/es/StyleContext'; import type { StyleContextProps } from '@ant-design/cssinjs/es/StyleContext'; import { merge } from 'lodash'; -import StaticFunction from '../static-function'; +import StaticFunction, { injectedStaticFunction } from '../static-function'; import themeConfig from '../theme'; import defaultTheme from '../theme/default'; import darkTheme from '../theme/dark'; @@ -97,7 +97,7 @@ const ConfigProvider: ConfigProviderType = ({ spin, table, tabs, - injectStaticFunction = true, + injectStaticFunction = !injectedStaticFunction, styleProviderProps, ...restProps }) => { diff --git a/packages/design/src/static-function/index.tsx b/packages/design/src/static-function/index.tsx index 8fbd408cf..4ed880366 100644 --- a/packages/design/src/static-function/index.tsx +++ b/packages/design/src/static-function/index.tsx @@ -36,6 +36,9 @@ let modal: Omit & { useModal: typeof AntModal.useModal; } = AntModal; +// injected static function or not +let injectedStaticFunction = false; + export default () => { // 自动注入 useToken,避免每次使用都要声明一遍,比较繁琐 token = useToken().token; @@ -54,7 +57,8 @@ export default () => { ...staticFunction.modal, useModal: AntModal.useModal, }; + injectedStaticFunction = true; return null; }; -export { token, message, notification, modal }; +export { token, message, notification, modal, injectedStaticFunction };