Skip to content

Commit

Permalink
fix(Tabs): filter nullish values from TabPanels children (#17859)
Browse files Browse the repository at this point in the history
This change allows users again to conditionally render children of TabPanels:
If children are not filtered for nullish/falsy values before cloning them,
`React.cloneElement` will fail.

This regression has been introduced with commit:
f406632, Tabs.tsx:L1257
  • Loading branch information
remolueoend authored Oct 29, 2024
1 parent 51d9bbe commit a24e338
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react/src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ function TabPanels({ children }: TabPanelsProps) {
return (
<>
{React.Children.map(children, (child, index) => {
return (
return !isElement(child) ? null : (
<TabPanelContext.Provider value={index}>
{React.cloneElement(child as React.ReactElement<any>, {
ref: (element: HTMLDivElement) => {
Expand Down
24 changes: 24 additions & 0 deletions packages/react/src/components/Tabs/__tests__/Tabs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Tabs>
<TabList aria-label="List of tabs" data-test-id="test-id">
<Tab>Tab Label 1</Tab>
{condition ? (
<Tab data-test-id="excluded-tab">Tab Label 2</Tab>
) : null}
</TabList>
<TabPanels>
<TabPanel>Tab Panel 1</TabPanel>
{condition ? (
<TabPanel data-test-id="excluded-panel">Tab Panel 2</TabPanel>
) : null}
</TabPanels>
</Tabs>
);

expect(screen.queryByTestId('excluded-tab')).not.toBeInTheDocument();
expect(screen.queryByTestId('excluded-panel')).not.toBeInTheDocument();
});
});

Expand Down

0 comments on commit a24e338

Please sign in to comment.