diff --git a/packages/react/src/components/Tabs/Tabs.tsx b/packages/react/src/components/Tabs/Tabs.tsx index ee9ecb0d1f3d..c1fcb1a6ba2e 100644 --- a/packages/react/src/components/Tabs/Tabs.tsx +++ b/packages/react/src/components/Tabs/Tabs.tsx @@ -1717,7 +1717,7 @@ function TabPanels({ children }: TabPanelsProps) { return ( <> {React.Children.map(children, (child, index) => { - return ( + return !isElement(child) ? null : ( {React.cloneElement(child as React.ReactElement, { ref: (element: HTMLDivElement) => { diff --git a/packages/react/src/components/Tabs/__tests__/Tabs-test.js b/packages/react/src/components/Tabs/__tests__/Tabs-test.js index e3a346a9f535..8a6ec38b8dee 100644 --- a/packages/react/src/components/Tabs/__tests__/Tabs-test.js +++ b/packages/react/src/components/Tabs/__tests__/Tabs-test.js @@ -64,6 +64,30 @@ describe('Tabs', () => { ); expect(container.firstChild).toHaveClass('custom-class'); + expect(container); + }); + + it('should not render conditionally excluded tabs and panels', () => { + const condition = false; + render( + + + Tab Label 1 + {condition ? ( + Tab Label 2 + ) : null} + + + Tab Panel 1 + {condition ? ( + Tab Panel 2 + ) : null} + + + ); + + expect(screen.queryByTestId('excluded-tab')).not.toBeInTheDocument(); + expect(screen.queryByTestId('excluded-panel')).not.toBeInTheDocument(); }); });