Skip to content

Commit ed37af1

Browse files
authored
TabController - remove 'selectedIndex' prop (#2481)
1 parent e6ad743 commit ed37af1

File tree

6 files changed

+12
-37
lines changed

6 files changed

+12
-37
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import React, {Component} from 'react';
23
import {ActivityIndicator, StyleSheet} from 'react-native';
34
import {
@@ -11,7 +12,6 @@ import {
1112
TabControllerImperativeMethods
1213
} from 'react-native-ui-lib';
1314
import {gestureHandlerRootHOC} from 'react-native-gesture-handler';
14-
import _ from 'lodash';
1515

1616
import Tab1 from './tab1';
1717
import Tab2 from './tab2';
@@ -162,14 +162,13 @@ class TabControllerScreen extends Component<{}, State> {
162162
}
163163

164164
render() {
165-
const {key, initialIndex, /* selectedIndex, */ asCarousel, centerSelected, fewItems, items} = this.state;
165+
const {key, initialIndex, asCarousel, centerSelected, fewItems, items} = this.state;
166166
return (
167167
<View flex bg-$backgroundDefault>
168168
<TabController
169169
key={key}
170170
ref={this.tabController}
171171
asCarousel={asCarousel}
172-
// selectedIndex={selectedIndex}
173172
initialIndex={initialIndex}
174173
onChangeIndex={this.onChangeIndex}
175174
items={items}

src/components/tabController/TabBar.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import _ from 'lodash';
12
import React, {useMemo, useContext, useState, useRef, ReactNode} from 'react';
23
import {StyleSheet, Platform, StyleProp, ViewStyle} from 'react-native';
34
import Reanimated, {runOnJS, useAnimatedReaction, useAnimatedStyle, interpolate} from 'react-native-reanimated';
4-
import _ from 'lodash';
5-
65
import TabBarContext from './TabBarContext';
76
import TabBarItem, {TabControllerItemProps} from './TabBarItem';
87
import {
@@ -171,7 +170,6 @@ const TabBar = (props: Props) => {
171170
currentPage,
172171
targetPage,
173172
initialIndex,
174-
selectedIndex,
175173
containerWidth: contextContainerWidth
176174
} = context;
177175
const containerWidth: number = useMemo(() => {
@@ -196,7 +194,7 @@ const TabBar = (props: Props) => {
196194
// @ts-expect-error TODO: typing bug
197195
scrollViewRef: tabBar,
198196
itemsCount,
199-
selectedIndex: selectedIndex || initialIndex,
197+
selectedIndex: initialIndex,
200198
containerWidth,
201199
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
202200
});

src/components/tabController/TabBarContext.ts

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

44
interface TabControllerContext {
55
initialIndex?: number;
6-
// DEPRECATED: use initialIndex instead
7-
selectedIndex?: number;
86
items?: any[];
97
itemsCount: number;
108
asCarousel?: boolean;

src/components/tabController/apis/tabController.api.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"props": [
99
{"name": "items", "type": "TabControllerItemProps[]", "description": "The list of tab bar items"},
1010
{"name": "initialIndex", "type": "number", "description": "Initial selected index", "default": "0"},
11-
{"name": "selectedIndex", "type": "number", "description": "The current selected index", "deprecated": true},
1211
{
1312
"name": "onChangeIndex",
1413
"type": "(index: number, prevIndex: number | null) => void",

src/components/tabController/index.tsx

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// TODO: support commented props
2-
import React, {PropsWithChildren, useMemo, useEffect, useState, useCallback} from 'react';
32
import _ from 'lodash';
3+
import React, {PropsWithChildren, useMemo, useEffect, useState, useCallback} from 'react';
44
import {useAnimatedReaction, useSharedValue, withTiming, runOnJS} from 'react-native-reanimated';
55
import {useOrientation, useThemeProps} from '../../hooks';
66
import {Constants} from '../../commons/new';
7-
import {LogService} from '../../services';
87
import TabBarContext from './TabBarContext';
98
import TabBar from './TabBar';
109
import TabBarItem, {TabControllerItemProps} from './TabBarItem';
@@ -13,8 +12,6 @@ import PageCarousel from './PageCarousel';
1312
import useImperativeTabControllerHandle, {TabControllerImperativeMethods} from './useImperativeTabControllerHandle';
1413
export {TabControllerItemProps, TabControllerImperativeMethods};
1514

16-
// TODO: should migrate selectedIndex to initialIndex (and make this prop uncontrolled)
17-
1815
interface TabControllerStatics {
1916
TabBar: typeof TabBar;
2017
TabBarItem: typeof TabBarItem;
@@ -31,10 +28,6 @@ export interface TabControllerProps extends ThemeComponent {
3128
* Initial selected index
3229
*/
3330
initialIndex?: number;
34-
/**
35-
* DEPRECATED: use initialIndex instead
36-
*/
37-
selectedIndex?: number;
3831
/**
3932
* callback for when index has change (will not be called on ignored items)
4033
*/
@@ -70,7 +63,6 @@ const TabController = React.forwardRef((props: PropsWithChildren<TabControllerPr
7063
const themeProps = useThemeProps(props, 'TabController');
7164
const {
7265
initialIndex = 0,
73-
selectedIndex,
7466
asCarousel = false,
7567
items,
7668
onChangeIndex = _.noop,
@@ -98,29 +90,19 @@ const TabController = React.forwardRef((props: PropsWithChildren<TabControllerPr
9890
return _.filter<TabControllerItemProps[]>(items, (item: TabControllerItemProps) => item.ignore);
9991
}, [items]);
10092

101-
/* backwards compatibility for `selectedIndex` prop. this line eventually should be removed */
102-
const _initialIndex = selectedIndex || initialIndex;
103-
10493
/* currentPage - static page index */
105-
const currentPage = useSharedValue(_initialIndex);
94+
const currentPage = useSharedValue(initialIndex);
10695
/* targetPage - transitioned page index (can be a fraction when transitioning between pages) */
107-
const targetPage = useSharedValue(_initialIndex);
108-
// const carouselOffset = useSharedValue(initialIndex * Math.round(pageWidth));
96+
const targetPage = useSharedValue(initialIndex);
10997

11098
const setCurrentIndex = useCallback((index: number) => {
11199
'worklet';
112100
currentPage.value = index;
113101
}, []);
114102

115103
useEffect(() => {
116-
if (!_.isUndefined(selectedIndex)) {
117-
LogService.deprecationWarn({component: 'TabController', oldProp: 'selectedIndex', newProp: 'initialIndex'});
118-
}
119-
}, [selectedIndex]);
120-
121-
useEffect(() => {
122-
setCurrentIndex(_initialIndex);
123-
}, [_initialIndex]);
104+
setCurrentIndex(initialIndex);
105+
}, [initialIndex]);
124106

125107
useAnimatedReaction(() => {
126108
return currentPage.value;
@@ -137,7 +119,7 @@ const TabController = React.forwardRef((props: PropsWithChildren<TabControllerPr
137119
const context = useMemo(() => {
138120
return {
139121
/* Pass Props */
140-
initialIndex: _initialIndex,
122+
initialIndex,
141123
asCarousel,
142124
pageWidth,
143125
/* Items */
@@ -147,13 +129,12 @@ const TabController = React.forwardRef((props: PropsWithChildren<TabControllerPr
147129
/* Animated Values */
148130
targetPage,
149131
currentPage,
150-
// carouselOffset,
151132
containerWidth: screenWidth,
152133
/* Callbacks */
153134
onChangeIndex,
154135
setCurrentIndex
155136
};
156-
}, [_initialIndex, asCarousel, items, onChangeIndex, screenWidth]);
137+
}, [initialIndex, asCarousel, items, onChangeIndex, screenWidth]);
157138

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

typings/incubator/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {BadgeProps} from '../components/Badge';
1212

1313
export namespace Incubator {
1414
export interface TabControllerProps {
15-
selectedIndex?: number;
15+
initialIndex?: number;
1616
onChangeIndex?: (index: number) => void;
1717
asCarousel?: boolean;
1818
}

0 commit comments

Comments
 (0)