Skip to content
Open
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
8 changes: 4 additions & 4 deletions src/components/UncontrolledTabs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cx from 'clsx';
import { childrenPropType } from '../helpers/propTypes';
import { getTabsCount as getTabsCountHelper } from '../helpers/count';
import { deepMap } from '../helpers/childrenDeepMap';
import { isTabList, isTabPanel, isTab } from '../helpers/elementTypes';
import { isTabList, isTabPanel } from '../helpers/elementTypes';

function isNode(node) {
return node && 'getAttribute' in node;
Expand Down Expand Up @@ -212,9 +212,9 @@ const UncontrolledTabs = (props) => {
const env =
environment || (typeof window !== 'undefined' ? window : undefined);
if (canUseActiveElement && env) {
wasTabFocused = React.Children.toArray(child.props.children)
.filter(isTab)
.some((tab, i) => env.document.activeElement === getTab(i));
wasTabFocused = Object.keys(tabNodes.current).some(
(key) => env.document.activeElement === tabNodes.current[key],
);
}

result = cloneElement(child, {
Expand Down
46 changes: 46 additions & 0 deletions src/components/__tests__/Tabs-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,52 @@ describe('<Tabs />', () => {
expect(inputField).toHaveFocus();
});

test('should retain tab focus when controlled tabs are inside a fragment', () => {
const renderTabs = (selectedIndex) => (
<Tabs selectedIndex={selectedIndex} onSelect={() => {}}>
<TabList>
<>
<Tab>First</Tab>
<Tab>Second</Tab>
</>
</TabList>
<TabPanel>First panel</TabPanel>
<TabPanel>Second panel</TabPanel>
</Tabs>
);
const { rerender } = render(renderTabs(0));
screen.getByRole('tab', { name: 'First' }).focus();

rerender(renderTabs(1));

expect(screen.getByRole('tab', { name: 'Second' })).toHaveFocus();
});

test('should preserve outside focus when controlled tabs are inside a fragment', () => {
const renderTabs = (selectedIndex) => (
<>
<input aria-label="Outside" />
<Tabs selectedIndex={selectedIndex} onSelect={() => {}}>
<TabList>
<>
<Tab>First</Tab>
<Tab>Second</Tab>
</>
</TabList>
<TabPanel>First panel</TabPanel>
<TabPanel>Second panel</TabPanel>
</Tabs>
</>
);
const { rerender } = render(renderTabs(0));
const input = screen.getByRole('textbox', { name: 'Outside' });
input.focus();

rerender(renderTabs(1));

expect(input).toHaveFocus();
});

test('should not change tabs when arrow up/down is pressed and disableUpDownKeys is passed', async () => {
render(
createTabs({
Expand Down