Skip to content

Commit

Permalink
Fix TabsTab
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Feb 17, 2025
1 parent 42a342e commit 7dbdef8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
8 changes: 6 additions & 2 deletions packages/react/src/tabs/root/TabsRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ describe('<Tabs.Root />', () => {
</Tabs.Root>
</DirectionProvider>,
);
const [firstTab, disabledTab] = getAllByRole('tab');
const [firstTab, disabledTab, thirdTab] = getAllByRole('tab');
await act(async () => {
firstTab.focus();
});
Expand All @@ -704,6 +704,10 @@ describe('<Tabs.Root />', () => {
expect(handleChange.callCount).to.equal(0);
expect(handleKeyDown.callCount).to.equal(1);
expect(handleKeyDown.firstCall.args[0]).to.have.property('defaultPrevented', true);

fireEvent.keyDown(disabledTab, { key: nextItemKey });
await flushMicrotasks();
expect(thirdTab).toHaveFocus();
});
});
},
Expand Down Expand Up @@ -853,7 +857,7 @@ describe('<Tabs.Root />', () => {
const handleChange = spy();
const handleKeyDown = spy();
const { getAllByRole } = await render(
<Tabs.Root onKeyDown={handleKeyDown} onValueChange={handleChange} value={2}>
<Tabs.Root onKeyDown={handleKeyDown} onValueChange={handleChange} value={0}>
<Tabs.List activateOnFocus={activateOnFocusProp}>
<Tabs.Tab value={0} />
<Tabs.Tab value={1} />
Expand Down
15 changes: 7 additions & 8 deletions packages/react/src/tabs/tab/useTabsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ function useTabsTab(parameters: useTabsTab.Parameters): useTabsTab.ReturnValue {
return valueParam === selectedTabValue;
}, [index, selectedTabValue, valueParam]);

// when activateOnFocus is `true`, ensure the active item in Composite's roving
// focus group matches the selected Tab
useEnhancedEffect(() => {
if (activateOnFocus && selected && index > -1 && highlightedTabIndex !== index) {
setHighlightedTabIndex(index);
}
}, [activateOnFocus, highlightedTabIndex, index, selected, setHighlightedTabIndex]);

const { getButtonProps, buttonRef } = useButton({
disabled,
focusableWhenDisabled: true,
Expand Down Expand Up @@ -105,6 +97,11 @@ function useTabsTab(parameters: useTabsTab.Parameters): useTabsTab.ReturnValue {
onTabActivation(tabValue, event.nativeEvent);
}
},
onKeyDown() {
if (index > -1) {
setHighlightedTabIndex(index);
}
},
onPointerDown(event) {
if (selected || disabled) {
return;
Expand Down Expand Up @@ -139,6 +136,8 @@ function useTabsTab(parameters: useTabsTab.Parameters): useTabsTab.ReturnValue {
tabPanelId,
tabValue,
disabled,
index,
setHighlightedTabIndex,
],
);

Expand Down

0 comments on commit 7dbdef8

Please sign in to comment.