Skip to content

Commit 8e681fe

Browse files
adids1221M-i-k-e-l
andauthored
refactor: simplify RTL handling in tab controller components (#3456)
* refactor: simplify RTL handling in tab controller components * Move to FIX_RTL const --------- Co-authored-by: M-i-k-e-l <[email protected]>
1 parent 54a4b0c commit 8e681fe

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/components/tabController/PageCarousel.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Reanimated, {
1010
} from 'react-native-reanimated';
1111
import {Constants} from '../../commons/new';
1212

13-
const FIX_RTL = Constants.isRTL && Constants.isAndroid;
13+
const FIX_RTL = Constants.isRTL;
1414

1515
/**
1616
* @description: TabController's Page Carousel

src/components/tabController/TabBar.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {FaderProps} from '../fader';
1818
import useScrollToItem from './useScrollToItem';
1919
import {useDidUpdate} from 'hooks';
2020

21-
const FIX_RTL = Constants.isRTL && Constants.isAndroid;
2221
const DEFAULT_HEIGHT = 48;
2322

2423
const DEFAULT_LABEL_STYLE = {
@@ -187,7 +186,7 @@ const TabBar = (props: Props) => {
187186
// @ts-expect-error TODO: typing bug
188187
scrollViewRef: tabBar,
189188
itemsCount,
190-
selectedIndex: FIX_RTL ? itemsCount - currentPage.value - 1 : currentPage.value,
189+
selectedIndex: currentPage.value,
191190
containerWidth,
192191
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
193192
});

src/components/tabController/useScrollToItem.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {useState, useCallback, useEffect, useRef, RefObject} from 'react';
33
import {LayoutChangeEvent} from 'react-native';
44
import {useSharedValue} from 'react-native-reanimated';
55
import {useScrollTo, ScrollToSupportedViews, ScrollToResultProps} from 'hooks';
6+
import {Constants} from '../../commons/new';
7+
8+
const FIX_RTL = Constants.isRTL && Constants.isIOS;
69

710
export enum OffsetType {
811
CENTER = 'CENTER',
@@ -177,12 +180,13 @@ const useScrollToItem = <T extends ScrollToSupportedViews>(props: ScrollToItemPr
177180

178181
const focusIndex = useCallback((index: number, animated = true) => {
179182
if (index >= 0 && offsets.CENTER.length > index) {
183+
const rtlIndex = FIX_RTL ? itemsCount - index - 1 : index;
180184
if (offsetType !== OffsetType.DYNAMIC) {
181-
scrollTo(offsets[offsetType][index], animated);
185+
scrollTo(offsets[offsetType][rtlIndex], animated);
182186
} else {
183187
const movingLeft = index < currentIndex.current;
184-
currentIndex.current = index;
185-
scrollTo(movingLeft ? offsets[OffsetType.RIGHT][index] : offsets[OffsetType.LEFT][index], animated);
188+
currentIndex.current = rtlIndex;
189+
scrollTo(movingLeft ? offsets[OffsetType.RIGHT][rtlIndex] : offsets[OffsetType.LEFT][rtlIndex], animated);
186190
}
187191
}
188192
},

0 commit comments

Comments
 (0)