Skip to content

Commit caa5e7b

Browse files
fix: trim routes for android - no more then 6 tabs (#38)
* fix: trim routes for android - no more then 6 tabs * refactor: refactor inline style for lefthook * fix: optimization - calculate trimmed routes only one time * refactor: update constants, useMemo() dependencies, style
1 parent 5922bcf commit caa5e7b

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/TabView.tsx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ interface Props<Route extends BaseRoute> {
7070
}) => ImageSource | undefined;
7171
}
7272

73+
const ANDROID_MAX_TABS = 6;
74+
7375
const TabView = <Route extends BaseRoute>({
7476
navigationState,
7577
renderScene,
@@ -87,9 +89,18 @@ const TabView = <Route extends BaseRoute>({
8789
// @ts-ignore
8890
const focusedKey = navigationState.routes[navigationState.index].key;
8991

90-
if (navigationState.routes.length > 6) {
91-
throw new Error('TabView only supports up to 6 tabs');
92-
}
92+
const trimmedRoutes = useMemo(() => {
93+
if (
94+
Platform.OS === 'android' &&
95+
navigationState.routes.length > ANDROID_MAX_TABS
96+
) {
97+
console.warn(
98+
`TabView only supports up to ${ANDROID_MAX_TABS} tabs on Android`
99+
);
100+
return navigationState.routes.slice(0, ANDROID_MAX_TABS);
101+
}
102+
return navigationState.routes;
103+
}, [navigationState.routes]);
93104

94105
/**
95106
* List of loaded tabs, tabs will be loaded when navigated to.
@@ -103,18 +114,18 @@ const TabView = <Route extends BaseRoute>({
103114

104115
const icons = useMemo(
105116
() =>
106-
navigationState.routes.map((route) =>
117+
trimmedRoutes.map((route) =>
107118
getIcon({
108119
route,
109120
focused: route.key === focusedKey,
110121
})
111122
),
112-
[focusedKey, getIcon, navigationState.routes]
123+
[focusedKey, getIcon, trimmedRoutes]
113124
);
114125

115126
const items: TabViewItems = useMemo(
116127
() =>
117-
navigationState.routes.map((route, index) => {
128+
trimmedRoutes.map((route, index) => {
118129
const icon = icons[index];
119130
const isSfSymbol = isAppleSymbol(icon);
120131

@@ -130,7 +141,7 @@ const TabView = <Route extends BaseRoute>({
130141
badge: props.getBadge?.({ route }),
131142
};
132143
}),
133-
[getLabelText, icons, navigationState.routes, props]
144+
[getLabelText, icons, trimmedRoutes, props]
134145
);
135146

136147
const resolvedIconAssets: ImageSource[] = useMemo(
@@ -145,9 +156,7 @@ const TabView = <Route extends BaseRoute>({
145156
);
146157

147158
const jumpTo = useLatestCallback((key: string) => {
148-
const index = navigationState.routes.findIndex(
149-
(route) => route.key === key
150-
);
159+
const index = trimmedRoutes.findIndex((route) => route.key === key);
151160

152161
onIndexChange(index);
153162
});
@@ -163,7 +172,7 @@ const TabView = <Route extends BaseRoute>({
163172
}}
164173
{...props}
165174
>
166-
{navigationState.routes.map((route) => {
175+
{trimmedRoutes.map((route) => {
167176
if (getLazy({ route }) !== false && !loaded.includes(route.key)) {
168177
// Don't render a screen if we've never navigated to it
169178
if (Platform.OS === 'android') {
@@ -176,7 +185,7 @@ const TabView = <Route extends BaseRoute>({
176185
<View
177186
key={route.key}
178187
style={[
179-
{ width: '100%', height: '100%' },
188+
styles.fullWidth,
180189
Platform.OS === 'android' && {
181190
display: route.key === focusedKey ? 'flex' : 'none',
182191
},

0 commit comments

Comments
 (0)