Skip to content

Commit

Permalink
fix: BottomNavigation warn when shifting with the wrong number of tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
brunohkbx committed Oct 11, 2022
1 parent 92a0a80 commit 4d28ea4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/components/BottomNavigation/BottomNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type Props = {
/**
* Whether the shifting style is used, the active tab icon shifts up to show the label and the inactive tabs won't have a label.
*
* By default, this is `true` when you have more than 3 tabs.
* By default, this is `true` when you have more than 3 tabs. Note that you need at least 2 tabs be able to run this animation.
* Pass `shifting={false}` to explicitly disable this animation, or `shifting={true}` to always use this animation.
*/
shifting?: boolean;
Expand Down Expand Up @@ -352,6 +352,14 @@ const BottomNavigation = ({
}: Props) => {
const { scale } = theme.animation;

if (shifting && navigationState.routes.length < 2) {
shifting = false;

console.warn(
'BottomNavigation needs at least 2 tabs to run shifting animation'
);
}

const focusedKey = navigationState.routes[navigationState.index].key;

/**
Expand Down
19 changes: 19 additions & 0 deletions src/components/__tests__/BottomNavigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ it('renders non-shifting bottom navigation', () => {
expect(tree).toMatchSnapshot();
});

it('does not crash when shifting is true and the number of tabs in the navigationState is less than 2', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});

render(
<BottomNavigation
shifting={true}
navigationState={createState(0, 1)}
onIndexChange={jest.fn()}
renderScene={({ route }) => route.title}
/>
);

expect(console.warn).toHaveBeenCalledWith(
'BottomNavigation needs at least 2 tabs to run shifting animation'
);

jest.restoreAllMocks();
});

it('renders custom icon and label in shifting bottom navigation', () => {
const tree = renderer
.create(
Expand Down

0 comments on commit 4d28ea4

Please sign in to comment.