Skip to content

Commit ba1c5f8

Browse files
committed
fix: Ensure that initial selected index never goes below 0
1 parent 5da2a7b commit ba1c5f8

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/components/Tabs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ For more information about controlled and uncontrolled mode of react-tabs see th
9999
};
100100

101101
if (newState.mode === MODE_UNCONTROLLED) {
102-
const maxTabIndex = getTabsCount(props.children) - 1;
102+
const maxTabIndex = Math.max(0, getTabsCount(props.children) - 1);
103103
let selectedIndex = null;
104104

105105
if (state.selectedIndex != null) {

src/components/__tests__/Tabs-test.js

+17
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,21 @@ describe('<Tabs />', () => {
541541
expect(firstTab).toHaveFocus();
542542
assertTabSelected(1);
543543
});
544+
545+
test('should render first tab once tabs are available', () => {
546+
const { rerender } = render(<Tabs></Tabs>);
547+
548+
rerender(
549+
<Tabs>
550+
<TabList>
551+
<Tab data-testid="tab1">Tab1</Tab>
552+
<Tab data-testid="tab2">Tab2</Tab>
553+
</TabList>
554+
<TabPanel data-testid="panel1">Hello Tab1</TabPanel>
555+
<TabPanel data-testid="panel2">Hello Tab2</TabPanel>
556+
</Tabs>,
557+
);
558+
559+
assertTabSelected(1);
560+
});
544561
});

0 commit comments

Comments
 (0)