From 647773a83e8fb22959dd51a9a6725cf623516258 Mon Sep 17 00:00:00 2001 From: dengfuping Date: Sat, 9 Sep 2023 18:42:59 +0800 Subject: [PATCH] fix(design): navigate should work in nested ConfigProvider, fix #123 --- .../__snapshots__/spin.test.tsx.snap | 33 +++++++++++++++ .../__tests__/navigate.test.tsx | 42 +++++++++++++++++++ .../config-provider/__tests__/spin.test.tsx | 25 +++++++++++ packages/design/src/config-provider/index.tsx | 4 +- 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/packages/design/src/config-provider/__tests__/__snapshots__/spin.test.tsx.snap b/packages/design/src/config-provider/__tests__/__snapshots__/spin.test.tsx.snap index 045e6757b..b85980572 100644 --- a/packages/design/src/config-provider/__tests__/__snapshots__/spin.test.tsx.snap +++ b/packages/design/src/config-provider/__tests__/__snapshots__/spin.test.tsx.snap @@ -15,3 +15,36 @@ exports[`ConfigProvider spin spin.indicator should work 1`] = ` `; + +exports[`ConfigProvider spin spin.indicator should work in nested ConfigProvider 1`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+`; diff --git a/packages/design/src/config-provider/__tests__/navigate.test.tsx b/packages/design/src/config-provider/__tests__/navigate.test.tsx index 25082725d..dc0d43e40 100644 --- a/packages/design/src/config-provider/__tests__/navigate.test.tsx +++ b/packages/design/src/config-provider/__tests__/navigate.test.tsx @@ -26,4 +26,46 @@ describe('ConfigProvider navigate', () => { ); render(); }); + + it('navigate should work in nested ConfigProvider', () => { + const SubChild1 = () => { + const { navigate } = useContext(ConfigProvider.ExtendedConfigContext); + expect(typeof navigate).toBe('function'); + return
; + }; + const SubChild2 = () => { + const { navigate } = useContext(ConfigProvider.ExtendedConfigContext); + expect(navigate).toBe(null); + return
; + }; + const Child = () => { + const { navigate } = useContext(ConfigProvider.ExtendedConfigContext); + expect(typeof navigate).toBe('function'); + return ( + <> + + + + + + + + ); + }; + const Parent = () => { + const navigate = useNavigate(); + return ( + + + + ); + }; + const Demo = () => ( + // useNavigate() may be used only in the context of a component + + + + ); + render(); + }); }); diff --git a/packages/design/src/config-provider/__tests__/spin.test.tsx b/packages/design/src/config-provider/__tests__/spin.test.tsx index 54feaa5ed..af56554ae 100644 --- a/packages/design/src/config-provider/__tests__/spin.test.tsx +++ b/packages/design/src/config-provider/__tests__/spin.test.tsx @@ -18,4 +18,29 @@ describe('ConfigProvider spin', () => { expect(container.querySelector('.ant-spin-oceanbase')).toBeFalsy(); expect(asFragment().firstChild).toMatchSnapshot(); }); + + it('spin.indicator should work in nested ConfigProvider', () => { + const { container, asFragment } = render( + , + }} + > + + + + , + }} + > + + + + ); + expect(container.querySelectorAll('.custom-indicator-1').length).toBe(1); + expect(container.querySelectorAll('.custom-indicator-2').length).toBe(1); + expect(container.querySelectorAll('.ant-spin-oceanbase').length).toBe(0); + expect(asFragment().firstChild).toMatchSnapshot(); + }); }); diff --git a/packages/design/src/config-provider/index.tsx b/packages/design/src/config-provider/index.tsx index 8fc58c955..7dba3b69a 100644 --- a/packages/design/src/config-provider/index.tsx +++ b/packages/design/src/config-provider/index.tsx @@ -49,6 +49,8 @@ const { defaultSeed, components } = defaultTheme; // ConfigProvider 默认设置主题和内嵌 App,支持 message, notification 和 Modal 等静态方法消费 ConfigProvider 配置 // ref: https://ant.design/components/app-cn const ConfigProvider = ({ children, theme, navigate, spin, ...restProps }: ConfigProviderProps) => { + // inherit from parent ConfigProvider + const parentContext = React.useContext(ExtendedConfigContext); return (