Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BottomNavigation warn when shifting with the wrong number of tabs #3408

Merged
merged 1 commit into from
Oct 18, 2022
Merged
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
9 changes: 9 additions & 0 deletions src/components/BottomNavigation/BottomNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type Props = {
*
* By default, this is `false` with theme version 3 and `true` when you have more than 3 tabs.
* Pass `shifting={false}` to explicitly disable this animation, or `shifting={true}` to always use this animation.
* Note that you need at least 2 tabs be able to run this animation.
*/
shifting?: boolean;
/**
Expand Down Expand Up @@ -387,6 +388,14 @@ const BottomNavigation = ({
}: Props) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we can change shifting = navigationState.routes.length > 3 to shifting = navigationState.routes.length >= 2
Would that be considered a breaking change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that be considered a breaking change?

Partially it's because it affects users with two tabs, who didn't want shiftingstyle to be applied. Why would you like to change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thought it would make more sense since the interpolation animation expects at least 2 items. So having 2 tabs would be perfectly fine to enable the animation.
I'm totally ok keeping it as it is 😃

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 @@ -175,6 +175,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