Skip to content

Commit 8d716d6

Browse files
authored
Fix error when passing a signle item to TabController (#1578)
* Fix error when passing a signle item to TabController * log an error when tab controller receive less than 2 items and handle the issue gracefully
1 parent 00ee43b commit 8d716d6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/components/tabController/TabBar.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,13 @@ const TabBar = (props: Props) => {
157157
selectedIndex,
158158
containerWidth: contextContainerWidth
159159
} = context;
160-
161160
const containerWidth: number = useMemo(() => {
162161
return propsContainerWidth || contextContainerWidth;
163162
}, [propsContainerWidth, contextContainerWidth]);
164-
165163
const items = useMemo(() => {
166164
return contextItems || propsItems;
167165
}, [contextItems, propsItems]);
166+
const itemsCount = items?.length || 0;
168167

169168
const {
170169
onItemLayout,
@@ -179,7 +178,7 @@ const TabBar = (props: Props) => {
179178
} = useScrollToItem({
180179
// @ts-expect-error TODO: typing bug
181180
scrollViewRef: tabBar,
182-
itemsCount: items?.length || 0,
181+
itemsCount,
183182
selectedIndex: selectedIndex || initialIndex,
184183
containerWidth,
185184
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
@@ -283,7 +282,9 @@ const TabBar = (props: Props) => {
283282
onLayout={onLayout}
284283
>
285284
<View style={tabBarContainerStyle}>{tabBarItems}</View>
286-
<Reanimated.View style={[styles.selectedIndicator, indicatorStyle, _indicatorTransitionStyle]}/>
285+
{itemsCount > 1 && (
286+
<Reanimated.View style={[styles.selectedIndicator, indicatorStyle, _indicatorTransitionStyle]}/>
287+
)}
287288
</FadedScrollView>
288289
</View>
289290
);

src/components/tabController/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ function TabController({
5959
}: PropsWithChildren<TabControllerProps>) {
6060
const [screenWidth, setScreenWidth] = useState<number>(Constants.windowWidth);
6161
const orientation = useRef<orientations>(Constants.orientation);
62+
63+
if (items?.length < 2) {
64+
console.error('TabController component expect a minimum of 2 items');
65+
}
66+
6267
useEffect(() => {
6368
const onOrientationChange = () => {
6469
if (orientation.current !== Constants.orientation) {
@@ -126,7 +131,7 @@ function TabController({
126131
/* Callbacks */
127132
onChangeIndex
128133
};
129-
}, [/* initialIndex,*/initialIndex, asCarousel, items, onChangeIndex, screenWidth]);
134+
}, [initialIndex, asCarousel, items, onChangeIndex, screenWidth]);
130135

131136
return <TabBarContext.Provider value={context}>{children}</TabBarContext.Provider>;
132137
}

0 commit comments

Comments
 (0)