Skip to content

Commit

Permalink
fix: Ensure that initial selected index never goes below 0
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Mar 9, 2021
1 parent 5da2a7b commit ba1c5f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/Tabs.js
Expand Up @@ -99,7 +99,7 @@ For more information about controlled and uncontrolled mode of react-tabs see th
};

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

if (state.selectedIndex != null) {
Expand Down
17 changes: 17 additions & 0 deletions src/components/__tests__/Tabs-test.js
Expand Up @@ -541,4 +541,21 @@ describe('<Tabs />', () => {
expect(firstTab).toHaveFocus();
assertTabSelected(1);
});

test('should render first tab once tabs are available', () => {
const { rerender } = render(<Tabs></Tabs>);

rerender(
<Tabs>
<TabList>
<Tab data-testid="tab1">Tab1</Tab>
<Tab data-testid="tab2">Tab2</Tab>
</TabList>
<TabPanel data-testid="panel1">Hello Tab1</TabPanel>
<TabPanel data-testid="panel2">Hello Tab2</TabPanel>
</Tabs>,
);

assertTabSelected(1);
});
});

0 comments on commit ba1c5f8

Please sign in to comment.