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): fix bottom navigation to allow unselected state #4653

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions example/src/Examples/BottomNavigationExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ const BottomNavigationExample = ({ navigation }: Props) => {
}}
title="Scene animation: opacity"
/>
<Menu.Item
trailingIcon={sceneAnimation === 'opacity' ? 'check' : undefined}
onPress={() => {
setIndex(-1);
}}
title="Unselect menu items"
/>
</Menu>
</Appbar.Header>
<BottomNavigation
Expand Down
4 changes: 2 additions & 2 deletions example/src/Examples/DialogExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Platform, StyleSheet } from 'react-native';

import { Button } from 'react-native-paper';

import { useExampleTheme } from '..';
import ScreenWrapper from '../ScreenWrapper';
import {
DialogWithCustomColors,
DialogWithDismissableBackButton,
Expand All @@ -12,8 +14,6 @@ import {
DialogWithRadioBtns,
UndismissableDialog,
} from './Dialogs';
import { useExampleTheme } from '..';
import ScreenWrapper from '../ScreenWrapper';

type ButtonVisibility = {
[key: string]: boolean | undefined;
Expand Down
8 changes: 7 additions & 1 deletion src/components/BottomNavigation/BottomNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ const BottomNavigation = <Route extends BaseRoute>({
}: Props<Route>) => {
const theme = useInternalTheme(themeOverrides);
const { scale } = theme.animation;
const isValidIndex =
navigationState.index >= 0 &&
navigationState.index < navigationState.routes.length;
const compact = compactProp ?? !theme.isV3;
let shifting =
shiftingProp ?? (theme.isV3 ? false : navigationState.routes.length > 3);
Expand All @@ -364,7 +367,9 @@ const BottomNavigation = <Route extends BaseRoute>({
);
}

const focusedKey = navigationState.routes[navigationState.index].key;
const focusedKey = isValidIndex
? navigationState.routes[navigationState.index].key
: '';

/**
* Active state of individual tab item positions:
Expand Down Expand Up @@ -400,6 +405,7 @@ const BottomNavigation = <Route extends BaseRoute>({

const animateToIndex = React.useCallback(
(index: number) => {
if (index < 0 || index > navigationState.routes.length) return;
Animated.parallel([
...navigationState.routes.map((_, i) =>
Animated.timing(tabsPositionAnims[i], {
Expand Down
1 change: 1 addition & 0 deletions src/components/BottomNavigation/BottomNavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({

const animateToIndex = React.useCallback(
(index: number) => {
if (index < 0 || index > navigationState.routes.length) return;
// Reset the ripple to avoid glitch if it's currently animating
rippleAnim.setValue(MIN_RIPPLE_SCALE);

Expand Down
78 changes: 77 additions & 1 deletion src/components/__tests__/BottomNavigation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Animated, Easing, Platform, StyleSheet } from 'react-native';
import { Animated, Easing, Platform, StyleSheet, Text } from 'react-native';

import { act, fireEvent, render } from '@testing-library/react-native';
import color from 'color';
Expand Down Expand Up @@ -656,3 +656,79 @@ it("allows customizing Route's type via generics", () => {

expect(tree).toMatchSnapshot();
});

it('renders all icons as unfocused when index is -1', () => {
const navigationState = {
index: -1,
routes: Array.from({ length: 3 }, (_, i) => ({
key: `key-${i}`,
icon: icons[i],
title: `Route: ${i}`,
})),
};

const component = render(
<BottomNavigation
shifting={false}
navigationState={navigationState}
onIndexChange={jest.fn()}
renderScene={({ route }) => route.title}
renderIcon={({ route, focused, color }) => (
<Text testID={`icon-${route.key}`} style={{ color }}>
{focused ? 'focused' : 'unfocused'}
</Text>
)}
/>
);
const { container } = component;
const iconNodes0 = container.findAll(
(node) => node.props.testID === 'icon-key-0'
);
const visibleIcon0 = iconNodes0.find((node) => {
// node.parent it's an Animated.View wrapping the icon so we can check that its style.opacity equals 1.
const parentStyle = node.parent?.props.style;
if (Array.isArray(parentStyle)) {
return parentStyle.some((style) => style && style.opacity === 1);
}
return parentStyle && parentStyle.opacity === 1;
});
expect(visibleIcon0!.props.children).toBe('unfocused');
});

it('renders all icons as unfocused when index is greater than the the routes length', () => {
const navigationState = {
index: 4,
routes: Array.from({ length: 3 }, (_, i) => ({
key: `key-${i}`,
icon: icons[i],
title: `Route: ${i}`,
})),
};

const component = render(
<BottomNavigation
shifting={false}
navigationState={navigationState}
onIndexChange={jest.fn()}
renderScene={({ route }) => route.title}
renderIcon={({ route, focused, color }) => (
<Text testID={`icon-${route.key}`} style={{ color }}>
{focused ? 'focused' : 'unfocused'}
</Text>
)}
/>
);
const { container } = component;
const iconNodes0 = container.findAll(
(node) => node.props.testID === 'icon-key-2'
);
const visibleIcon2 = iconNodes0.find((node) => {
// node.parent it's an Animated.View wrapping the icon so we can check that its style.opacity equals 1.
const parentStyle = node.parent?.props.style;
if (Array.isArray(parentStyle)) {
return parentStyle.some((style) => style && style.opacity === 1);
}
return parentStyle && parentStyle.opacity === 1;
});
expect(visibleIcon2!.props.children).toBe('unfocused');
});
2 changes: 1 addition & 1 deletion src/styles/fonts.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Platform, PlatformOSType } from 'react-native';

import { typescale } from './themes/v3/tokens';
import type { Fonts, MD3Type, MD3Typescale, MD3TypescaleKey } from '../types';
import { typescale } from './themes/v3/tokens';

export const fontConfig = {
web: {
Expand Down