Skip to content

Fix issue with TabController not handling dynamic items size #1416

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 3 commits into from
Jul 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ class TabControllerScreen extends Component<{}, State> {
}

onAddItem = () => {
console.warn('Add Item');
const {items} = this.state;
let newItems = items.slice(0, -1) as TabControllerItemProps[];
newItems = [...newItems, {label: `New Item # ${newItems.length + 1}`}, items[items.length - 1]];
this.setState({items: newItems});
};

toggleItemsCount = () => {
Expand Down
2 changes: 1 addition & 1 deletion generatedTypes/components/tabController2/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface TabControllerProps {
* @important: On Android, if using react-native-navigation, make sure to wrap your screen with gestureHandlerRootHOC
* @importantLink: https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#with-wix-react-native-navigation-https-githubcom-wix-react-native-navigation
*/
declare function TabController({ selectedIndex, asCarousel, items, onChangeIndex, carouselPageWidth, children }: PropsWithChildren<TabControllerProps>): JSX.Element | null;
declare function TabController({ selectedIndex, asCarousel, items, onChangeIndex, carouselPageWidth, children }: PropsWithChildren<TabControllerProps>): JSX.Element;
declare namespace TabController {
var TabBar: React.ComponentClass<import("./TabBar").TabControllerBarProps & {
useCustomTheme?: boolean | undefined;
Expand Down
11 changes: 3 additions & 8 deletions src/components/tabController2/TabBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useMemo, useRef, useContext, ReactNode} from 'react';
import React, {useMemo, useContext, ReactNode} from 'react';
import {StyleSheet, Platform, StyleProp, ViewStyle} from 'react-native';
import Reanimated, {runOnJS, useAnimatedReaction, useAnimatedStyle, interpolate} from 'react-native-reanimated';
import _ from 'lodash';
Expand Down Expand Up @@ -141,15 +141,12 @@ const TabBar = (props: Props) => {
spreadItems,
indicatorInsets = Spacings.s4,
containerStyle,
testID,
children: propsChildren
testID
} = props;

const context = useContext(TabBarContext);
const {items: contextItems, currentPage, targetPage, selectedIndex} = context;

const children = useRef<Props['children']>(_.filter(propsChildren, (child: ChildProps) => !!child));

const containerWidth: number = useMemo(() => {
return propsContainerWidth || Constants.screenWidth;
}, [propsContainerWidth]);
Expand All @@ -158,8 +155,6 @@ const TabBar = (props: Props) => {
return contextItems || propsItems;
}, [contextItems, propsItems]);

const itemsCount = useRef<number>(items ? _.size(items) : React.Children.count(children.current));

const {
scrollViewRef: tabBar,
onItemLayout,
Expand All @@ -171,7 +166,7 @@ const TabBar = (props: Props) => {
onContentSizeChange,
onLayout
} = useScrollToItem({
itemsCount: itemsCount.current,
itemsCount: items?.length || 0,
selectedIndex,
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
});
Expand Down
7 changes: 5 additions & 2 deletions src/components/tabController2/TabBarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ export default function TabBarItem({
}, []);

const onPress = useCallback(() => {
currentPage.value = index;
if (!ignore) {
currentPage.value = index;
}

props.onPress?.(index);
}, [index, props.onPress]);
}, [index, props.onPress, ignore]);

const onLayout = useCallback((event: LayoutChangeEvent) => {
const {width} = event.nativeEvent.layout;
Expand Down
6 changes: 1 addition & 5 deletions src/components/tabController2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface TabControllerProps {
function TabController({
selectedIndex = 0,
asCarousel = false,
items = [],
items,
onChangeIndex = _.noop,
carouselPageWidth,
children
Expand Down Expand Up @@ -94,10 +94,6 @@ function TabController({
};
}, [/* selectedIndex,*/asCarousel, items, onChangeIndex]);

if (items.length === 0) {
return null;
}

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

Expand Down