Skip to content

Commit 20d5586

Browse files
authored
Infra/tab controller2 initial index (#1438)
* selectedIndex -> InitialIndex, also fixed change index will change the current page * Type change * typescript type change * ts build changes * comment for weird code
1 parent 3261523 commit 20d5586

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

generatedTypes/components/tabController2/TabBarContext.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import Reanimated from 'react-native-reanimated';
33
interface TabControllerContext {
4+
initialIndex?: number;
45
selectedIndex?: number;
56
items?: any[];
67
asCarousel?: boolean;

generatedTypes/components/tabController2/index.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export interface TabControllerProps {
99
/**
1010
* Initial selected index
1111
*/
12-
selectedIndex: number;
12+
initialIndex?: number;
13+
/**
14+
* DEPRECATED: use initialIndex instead
15+
*/
16+
selectedIndex?: number;
1317
/**
1418
* callback for when index has change (will not be called on ignored items)
1519
*/
@@ -30,7 +34,7 @@ export interface TabControllerProps {
3034
* @important: On Android, if using react-native-navigation, make sure to wrap your screen with gestureHandlerRootHOC
3135
* @importantLink: https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#with-wix-react-native-navigation-https-githubcom-wix-react-native-navigation
3236
*/
33-
declare function TabController({ selectedIndex, asCarousel, items, onChangeIndex, carouselPageWidth, children }: PropsWithChildren<TabControllerProps>): JSX.Element;
37+
declare function TabController({ initialIndex, selectedIndex, asCarousel, items, onChangeIndex, carouselPageWidth, children }: PropsWithChildren<TabControllerProps>): JSX.Element;
3438
declare namespace TabController {
3539
var TabBar: React.ComponentClass<import("./TabBar").TabControllerBarProps & {
3640
useCustomTheme?: boolean | undefined;

src/components/tabController2/TabBarContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from 'react';
22
import Reanimated from 'react-native-reanimated';
33

44
interface TabControllerContext {
5+
initialIndex?: number;
6+
// DEPRECATED: use initialIndex instead
57
selectedIndex?: number;
68
items?: any[];
79
asCarousel?: boolean;

src/components/tabController2/index.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// TODO: support commented props
2-
import React, {PropsWithChildren, useMemo} from 'react';
2+
import React, {PropsWithChildren, useMemo, useEffect} from 'react';
33
import _ from 'lodash';
44
import {useAnimatedReaction, useSharedValue, withTiming, runOnJS} from 'react-native-reanimated';
55
import {Constants} from '../../helpers';
66
import {asBaseComponent} from '../../commons/new';
7+
import {LogService} from '../../services';
78
import TabBarContext from './TabBarContext';
89
import TabBar from './TabBar';
910
import TabBarItem, {TabControllerItemProps} from './TabBarItem';
@@ -20,7 +21,11 @@ export interface TabControllerProps {
2021
/**
2122
* Initial selected index
2223
*/
23-
selectedIndex: number;
24+
initialIndex?: number;
25+
/**
26+
* DEPRECATED: use initialIndex instead
27+
*/
28+
selectedIndex?: number;
2429
/**
2530
* callback for when index has change (will not be called on ignored items)
2631
*/
@@ -43,7 +48,8 @@ export interface TabControllerProps {
4348
* @importantLink: https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#with-wix-react-native-navigation-https-githubcom-wix-react-native-navigation
4449
*/
4550
function TabController({
46-
selectedIndex = 0,
51+
initialIndex = 0,
52+
selectedIndex,
4753
asCarousel = false,
4854
items,
4955
onChangeIndex = _.noop,
@@ -58,13 +64,26 @@ function TabController({
5864
return _.filter<TabControllerItemProps[]>(items, (item: TabControllerItemProps) => item.ignore);
5965
}, [items]);
6066

67+
/* backwards compatibility for `selectedIndex` prop. this line eventually should be removed */
68+
initialIndex = selectedIndex || initialIndex;
69+
6170
/* currentPage - static page index */
62-
const currentPage = useSharedValue(selectedIndex);
71+
const currentPage = useSharedValue(initialIndex);
6372
/* targetPage - transitioned page index (can be a fraction when transitioning between pages) */
64-
const targetPage = useSharedValue(selectedIndex);
65-
const carouselOffset = useSharedValue(selectedIndex * Math.round(pageWidth));
73+
const targetPage = useSharedValue(initialIndex);
74+
const carouselOffset = useSharedValue(initialIndex * Math.round(pageWidth));
6675
const containerWidth = useSharedValue(pageWidth);
6776

77+
useEffect(() => {
78+
if (!_.isUndefined(selectedIndex)) {
79+
LogService.deprecationWarn({component: 'TabController2', oldProp: 'selectedIndex', newProp: 'initialIndex'});
80+
}
81+
}, [selectedIndex]);
82+
83+
useEffect(() => {
84+
currentPage.value = initialIndex;
85+
}, [initialIndex]);
86+
6887
useAnimatedReaction(() => {
6988
return currentPage.value;
7089
},
@@ -78,7 +97,7 @@ function TabController({
7897
const context = useMemo(() => {
7998
return {
8099
/* Pass Props */
81-
selectedIndex,
100+
initialIndex,
82101
asCarousel,
83102
pageWidth,
84103
/* Items */
@@ -92,7 +111,7 @@ function TabController({
92111
/* Callbacks */
93112
onChangeIndex
94113
};
95-
}, [/* selectedIndex,*/asCarousel, items, onChangeIndex]);
114+
}, [/* initialIndex,*/initialIndex, asCarousel, items, onChangeIndex]);
96115

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

0 commit comments

Comments
 (0)