Skip to content

Commit 15a1985

Browse files
committed
fix:65151: Onyx Tab navigator error if per-diem is selected
1 parent 7101f2e commit 15a1985

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/libs/Navigation/OnyxTabNavigator.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {MaterialTopTabNavigationEventMap} from '@react-navigation/material-
22
import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs';
33
import type {EventMapCore, NavigationState, ParamListBase, ScreenListeners} from '@react-navigation/native';
44
import {useRoute} from '@react-navigation/native';
5-
import React, {useCallback, useContext, useEffect, useRef, useState} from 'react';
5+
import React, {useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
66
import {useOnyx} from 'react-native-onyx';
77
import FocusTrapContainerElement from '@components/FocusTrap/FocusTrapContainerElement';
88
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
@@ -69,6 +69,21 @@ const TopTab = createMaterialTopTabNavigator<ParamListBase, string>();
6969
// This provider is placed in the OnyxTabNavigator component and the consumer is in the TabScreenWithFocusTrapWrapper component.
7070
const TabFocusTrapContext = React.createContext<(tabName: string, containerElement: HTMLElement | null) => void>(() => {});
7171

72+
const getTabNames = (children: React.ReactNode): string[] => {
73+
const result: string[] = [];
74+
75+
React.Children.forEach(children, (child) => {
76+
if (React.isValidElement(child)) {
77+
const element = child as React.ReactElement<{name?: string}>;
78+
if (typeof element.props.name === 'string') {
79+
result.push(element.props.name);
80+
}
81+
}
82+
});
83+
84+
return result;
85+
};
86+
7287
// This takes all the same props as MaterialTopTabsNavigator: https://reactnavigation.org/docs/material-top-tab-navigator/#props,
7388
// except ID is now required, and it gets a `selectedTab` from Onyx
7489
// It also takes 2 more optional callbacks to manage the focus trap container elements of the tab bar and the active tab
@@ -94,6 +109,10 @@ function OnyxTabNavigator({
94109
const [focusTrapContainerElementMapping, setFocusTrapContainerElementMapping] = useState<Record<string, HTMLElement>>({});
95110
const [selectedTab, selectedTabResult] = useOnyx(`${ONYXKEYS.COLLECTION.SELECTED_TAB}${id}`, {canBeMissing: false});
96111

112+
const tabNames = useMemo(() => getTabNames(children), [children]);
113+
114+
const validInitialTab = selectedTab && tabNames.includes(selectedTab) ? selectedTab : defaultSelectedTab;
115+
97116
const LazyPlaceholder = useCallback(() => {
98117
return <FullScreenLoadingIndicator />;
99118
}, []);
@@ -146,7 +165,7 @@ function OnyxTabNavigator({
146165
/* eslint-disable-next-line react/jsx-props-no-spreading */
147166
{...rest}
148167
id={id}
149-
initialRouteName={selectedTab ?? defaultSelectedTab}
168+
initialRouteName={validInitialTab}
150169
backBehavior="initialRoute"
151170
keyboardDismissMode="none"
152171
tabBar={TabBarWithFocusTrapInclusion}

0 commit comments

Comments
 (0)