Skip to content

Fix error when passing a signle item to TabController #1578

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

Merged
merged 2 commits into from
Oct 3, 2021
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: 5 additions & 4 deletions src/components/tabController/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,13 @@ const TabBar = (props: Props) => {
selectedIndex,
containerWidth: contextContainerWidth
} = context;

const containerWidth: number = useMemo(() => {
return propsContainerWidth || contextContainerWidth;
}, [propsContainerWidth, contextContainerWidth]);

const items = useMemo(() => {
return contextItems || propsItems;
}, [contextItems, propsItems]);
const itemsCount = items?.length || 0;

const {
onItemLayout,
Expand All @@ -179,7 +178,7 @@ const TabBar = (props: Props) => {
} = useScrollToItem({
// @ts-expect-error TODO: typing bug
scrollViewRef: tabBar,
itemsCount: items?.length || 0,
itemsCount,
selectedIndex: selectedIndex || initialIndex,
containerWidth,
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
Expand Down Expand Up @@ -283,7 +282,9 @@ const TabBar = (props: Props) => {
onLayout={onLayout}
>
<View style={tabBarContainerStyle}>{tabBarItems}</View>
<Reanimated.View style={[styles.selectedIndicator, indicatorStyle, _indicatorTransitionStyle]}/>
{itemsCount > 1 && (
<Reanimated.View style={[styles.selectedIndicator, indicatorStyle, _indicatorTransitionStyle]}/>
)}
</FadedScrollView>
</View>
);
Expand Down
7 changes: 6 additions & 1 deletion src/components/tabController/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ function TabController({
}: PropsWithChildren<TabControllerProps>) {
const [screenWidth, setScreenWidth] = useState<number>(Constants.windowWidth);
const orientation = useRef<orientations>(Constants.orientation);

if (items?.length < 2) {
console.error('TabController component expect a minimum of 2 items');
}

useEffect(() => {
const onOrientationChange = () => {
if (orientation.current !== Constants.orientation) {
Expand Down Expand Up @@ -126,7 +131,7 @@ function TabController({
/* Callbacks */
onChangeIndex
};
}, [/* initialIndex,*/initialIndex, asCarousel, items, onChangeIndex, screenWidth]);
}, [initialIndex, asCarousel, items, onChangeIndex, screenWidth]);

return <TabBarContext.Provider value={context}>{children}</TabBarContext.Provider>;
}
Expand Down