Skip to content

Commit c2c5b91

Browse files
authored
Fix issue with TabController not handling dynamic items size (#1416)
* Fix issue with TabController doesn't take into account when items count has changed * Add backward support for passing items to TabController.TabBar directly * Push missing generated types
1 parent 0f03ea8 commit c2c5b91

File tree

5 files changed

+14
-17
lines changed

5 files changed

+14
-17
lines changed

demo/src/screens/componentScreens/TabControllerScreen/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ class TabControllerScreen extends Component<{}, State> {
7373
}
7474

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

7982
toggleItemsCount = () => {

generatedTypes/components/tabController2/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface TabControllerProps {
3030
* @important: On Android, if using react-native-navigation, make sure to wrap your screen with gestureHandlerRootHOC
3131
* @importantLink: https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#with-wix-react-native-navigation-https-githubcom-wix-react-native-navigation
3232
*/
33-
declare function TabController({ selectedIndex, asCarousel, items, onChangeIndex, carouselPageWidth, children }: PropsWithChildren<TabControllerProps>): JSX.Element | null;
33+
declare function TabController({ selectedIndex, asCarousel, items, onChangeIndex, carouselPageWidth, children }: PropsWithChildren<TabControllerProps>): JSX.Element;
3434
declare namespace TabController {
3535
var TabBar: React.ComponentClass<import("./TabBar").TabControllerBarProps & {
3636
useCustomTheme?: boolean | undefined;

src/components/tabController2/TabBar.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useMemo, useRef, useContext, ReactNode} from 'react';
1+
import React, {useMemo, useContext, ReactNode} from 'react';
22
import {StyleSheet, Platform, StyleProp, ViewStyle} from 'react-native';
33
import Reanimated, {runOnJS, useAnimatedReaction, useAnimatedStyle, interpolate} from 'react-native-reanimated';
44
import _ from 'lodash';
@@ -141,15 +141,12 @@ const TabBar = (props: Props) => {
141141
spreadItems,
142142
indicatorInsets = Spacings.s4,
143143
containerStyle,
144-
testID,
145-
children: propsChildren
144+
testID
146145
} = props;
147146

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

151-
const children = useRef<Props['children']>(_.filter(propsChildren, (child: ChildProps) => !!child));
152-
153150
const containerWidth: number = useMemo(() => {
154151
return propsContainerWidth || Constants.screenWidth;
155152
}, [propsContainerWidth]);
@@ -158,8 +155,6 @@ const TabBar = (props: Props) => {
158155
return contextItems || propsItems;
159156
}, [contextItems, propsItems]);
160157

161-
const itemsCount = useRef<number>(items ? _.size(items) : React.Children.count(children.current));
162-
163158
const {
164159
scrollViewRef: tabBar,
165160
onItemLayout,
@@ -171,7 +166,7 @@ const TabBar = (props: Props) => {
171166
onContentSizeChange,
172167
onLayout
173168
} = useScrollToItem({
174-
itemsCount: itemsCount.current,
169+
itemsCount: items?.length || 0,
175170
selectedIndex,
176171
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
177172
});

src/components/tabController2/TabBarItem.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,12 @@ export default function TabBarItem({
139139
}, []);
140140

141141
const onPress = useCallback(() => {
142-
currentPage.value = index;
142+
if (!ignore) {
143+
currentPage.value = index;
144+
}
145+
143146
props.onPress?.(index);
144-
}, [index, props.onPress]);
147+
}, [index, props.onPress, ignore]);
145148

146149
const onLayout = useCallback((event: LayoutChangeEvent) => {
147150
const {width} = event.nativeEvent.layout;

src/components/tabController2/index.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface TabControllerProps {
4545
function TabController({
4646
selectedIndex = 0,
4747
asCarousel = false,
48-
items = [],
48+
items,
4949
onChangeIndex = _.noop,
5050
carouselPageWidth,
5151
children
@@ -94,10 +94,6 @@ function TabController({
9494
};
9595
}, [/* selectedIndex,*/asCarousel, items, onChangeIndex]);
9696

97-
if (items.length === 0) {
98-
return null;
99-
}
100-
10197
return <TabBarContext.Provider value={context}>{children}</TabBarContext.Provider>;
10298
}
10399

0 commit comments

Comments
 (0)