Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tabs: Make sure individual Tabs are linked to the correct TabPanels #57033

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

### Experimental

- `TabPanel`: do not render hidden content ([#57046](https://github.com/WordPress/gutenberg/pull/57046)).
- `Tabs`: do not render hidden content ([#57046](https://github.com/WordPress/gutenberg/pull/57046)).
- `Tabs`: make sure `Tab`s are associated to the right `TabPanel`s, regardless of the order they're rendered in ([#57033](https://github.com/WordPress/gutenberg/pull/57033)).

### Bug Fix

Expand All @@ -28,6 +29,7 @@
- `FontSizePicker`: Add opt-in prop for 40px default size ([#56804](https://github.com/WordPress/gutenberg/pull/56804)).

### Bug Fix

- `PaletteEdit`: temporary custom gradient not saving ([#56896](https://github.com/WordPress/gutenberg/pull/56896)).
- `ToggleGroupControl`: react correctly to external controlled updates ([#56678](https://github.com/WordPress/gutenberg/pull/56678)).
- `ToolsPanel`: fix a performance issue ([#56770](https://github.com/WordPress/gutenberg/pull/56770)).
Expand Down
10 changes: 5 additions & 5 deletions packages/components/src/tabs/tabpanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* External dependencies
*/

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -38,7 +34,11 @@ export const TabPanel = forwardRef<
<StyledTabPanel
ref={ ref }
store={ store }
id={ instancedTabId }
// For TabPanel, the id passed here is the id attribute of the DOM
// element.
// `tabId` is the id of the tab that controls this panel.
id={ `${ instancedTabId }-view` }
tabId={ instancedTabId }
focusable={ focusable }
{ ...otherProps }
>
Expand Down
56 changes: 56 additions & 0 deletions packages/components/src/tabs/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1278,4 +1278,60 @@ describe( 'Tabs', () => {
} );
} );
} );
it( 'should associate each `Tab` with the correct `TabPanel`, even if they are not rendered in the same order', async () => {
const TABS_WITH_DELTA_REVERSED = [ ...TABS_WITH_DELTA ].reverse();

render(
<Tabs>
<Tabs.TabList>
{ TABS_WITH_DELTA.map( ( tabObj ) => (
<Tabs.Tab
key={ tabObj.tabId }
tabId={ tabObj.tabId }
className={ tabObj.tab.className }
disabled={ tabObj.tab.disabled }
>
{ tabObj.title }
</Tabs.Tab>
) ) }
</Tabs.TabList>
{ TABS_WITH_DELTA_REVERSED.map( ( tabObj ) => (
<Tabs.TabPanel
key={ tabObj.tabId }
tabId={ tabObj.tabId }
focusable={ tabObj.tabpanel?.focusable }
>
{ tabObj.content }
</Tabs.TabPanel>
) ) }
</Tabs>
);

// Alpha is the initially selected tab,and should render the correct tabpanel
expect( await getSelectedTab() ).toHaveTextContent( 'Alpha' );
chad1008 marked this conversation as resolved.
Show resolved Hide resolved
expect( screen.getByRole( 'tabpanel' ) ).toHaveTextContent(
'Selected tab: Alpha'
);

// Select Beta, make sure the correct tabpanel is rendered
await click( screen.getByRole( 'tab', { name: 'Beta' } ) );
expect( await getSelectedTab() ).toHaveTextContent( 'Beta' );
expect( screen.getByRole( 'tabpanel' ) ).toHaveTextContent(
'Selected tab: Beta'
);

// Select Gamma, make sure the correct tabpanel is rendered
await click( screen.getByRole( 'tab', { name: 'Gamma' } ) );
expect( await getSelectedTab() ).toHaveTextContent( 'Gamma' );
expect( screen.getByRole( 'tabpanel' ) ).toHaveTextContent(
'Selected tab: Gamma'
);

// Select Delta, make sure the correct tabpanel is rendered
await click( screen.getByRole( 'tab', { name: 'Delta' } ) );
expect( await getSelectedTab() ).toHaveTextContent( 'Delta' );
expect( screen.getByRole( 'tabpanel' ) ).toHaveTextContent(
'Selected tab: Delta'
);
} );
} );
Loading