Skip to content

V7_TabController - remove 'selectedIndex' prop #2481

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 1 commit into from
Feb 15, 2023
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
@@ -1,3 +1,4 @@
import _ from 'lodash';
import React, {Component} from 'react';
import {ActivityIndicator, StyleSheet} from 'react-native';
import {
Expand All @@ -11,7 +12,6 @@ import {
TabControllerImperativeMethods
} from 'react-native-ui-lib';
import {gestureHandlerRootHOC} from 'react-native-gesture-handler';
import _ from 'lodash';

import Tab1 from './tab1';
import Tab2 from './tab2';
Expand Down Expand Up @@ -162,14 +162,13 @@ class TabControllerScreen extends Component<{}, State> {
}

render() {
const {key, initialIndex, /* selectedIndex, */ asCarousel, centerSelected, fewItems, items} = this.state;
const {key, initialIndex, asCarousel, centerSelected, fewItems, items} = this.state;
return (
<View flex bg-$backgroundDefault>
<TabController
key={key}
ref={this.tabController}
asCarousel={asCarousel}
// selectedIndex={selectedIndex}
initialIndex={initialIndex}
onChangeIndex={this.onChangeIndex}
items={items}
Expand Down
6 changes: 2 additions & 4 deletions src/components/tabController/TabBar.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import _ from 'lodash';
import React, {useMemo, useContext, useState, useRef, ReactNode} from 'react';
import {StyleSheet, Platform, StyleProp, ViewStyle} from 'react-native';
import Reanimated, {runOnJS, useAnimatedReaction, useAnimatedStyle, interpolate} from 'react-native-reanimated';
import _ from 'lodash';

import TabBarContext from './TabBarContext';
import TabBarItem, {TabControllerItemProps} from './TabBarItem';
import {
Expand Down Expand Up @@ -171,7 +170,6 @@ const TabBar = (props: Props) => {
currentPage,
targetPage,
initialIndex,
selectedIndex,
containerWidth: contextContainerWidth
} = context;
const containerWidth: number = useMemo(() => {
Expand All @@ -196,7 +194,7 @@ const TabBar = (props: Props) => {
// @ts-expect-error TODO: typing bug
scrollViewRef: tabBar,
itemsCount,
selectedIndex: selectedIndex || initialIndex,
selectedIndex: initialIndex,
containerWidth,
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
});
Expand Down
2 changes: 0 additions & 2 deletions src/components/tabController/TabBarContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import Reanimated from 'react-native-reanimated';

interface TabControllerContext {
initialIndex?: number;
// DEPRECATED: use initialIndex instead
selectedIndex?: number;
items?: any[];
itemsCount: number;
asCarousel?: boolean;
Expand Down
1 change: 0 additions & 1 deletion src/components/tabController/apis/tabController.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"props": [
{"name": "items", "type": "TabControllerItemProps[]", "description": "The list of tab bar items"},
{"name": "initialIndex", "type": "number", "description": "Initial selected index", "default": "0"},
{"name": "selectedIndex", "type": "number", "description": "The current selected index", "deprecated": true},
{
"name": "onChangeIndex",
"type": "(index: number, prevIndex: number | null) => void",
Expand Down
33 changes: 7 additions & 26 deletions src/components/tabController/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// TODO: support commented props
import React, {PropsWithChildren, useMemo, useEffect, useState, useCallback} from 'react';
import _ from 'lodash';
import React, {PropsWithChildren, useMemo, useEffect, useState, useCallback} from 'react';
import {useAnimatedReaction, useSharedValue, withTiming, runOnJS} from 'react-native-reanimated';
import {useOrientation, useThemeProps} from '../../hooks';
import {Constants} from '../../commons/new';
import {LogService} from '../../services';
import TabBarContext from './TabBarContext';
import TabBar from './TabBar';
import TabBarItem, {TabControllerItemProps} from './TabBarItem';
Expand All @@ -13,8 +12,6 @@ import PageCarousel from './PageCarousel';
import useImperativeTabControllerHandle, {TabControllerImperativeMethods} from './useImperativeTabControllerHandle';
export {TabControllerItemProps, TabControllerImperativeMethods};

// TODO: should migrate selectedIndex to initialIndex (and make this prop uncontrolled)

interface TabControllerStatics {
TabBar: typeof TabBar;
TabBarItem: typeof TabBarItem;
Expand All @@ -31,10 +28,6 @@ export interface TabControllerProps extends ThemeComponent {
* Initial selected index
*/
initialIndex?: number;
/**
* DEPRECATED: use initialIndex instead
*/
selectedIndex?: number;
/**
* callback for when index has change (will not be called on ignored items)
*/
Expand Down Expand Up @@ -70,7 +63,6 @@ const TabController = React.forwardRef((props: PropsWithChildren<TabControllerPr
const themeProps = useThemeProps(props, 'TabController');
const {
initialIndex = 0,
selectedIndex,
asCarousel = false,
items,
onChangeIndex = _.noop,
Expand Down Expand Up @@ -98,29 +90,19 @@ const TabController = React.forwardRef((props: PropsWithChildren<TabControllerPr
return _.filter<TabControllerItemProps[]>(items, (item: TabControllerItemProps) => item.ignore);
}, [items]);

/* backwards compatibility for `selectedIndex` prop. this line eventually should be removed */
const _initialIndex = selectedIndex || initialIndex;

/* currentPage - static page index */
const currentPage = useSharedValue(_initialIndex);
const currentPage = useSharedValue(initialIndex);
/* targetPage - transitioned page index (can be a fraction when transitioning between pages) */
const targetPage = useSharedValue(_initialIndex);
// const carouselOffset = useSharedValue(initialIndex * Math.round(pageWidth));
const targetPage = useSharedValue(initialIndex);

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

useEffect(() => {
if (!_.isUndefined(selectedIndex)) {
LogService.deprecationWarn({component: 'TabController', oldProp: 'selectedIndex', newProp: 'initialIndex'});
}
}, [selectedIndex]);

useEffect(() => {
setCurrentIndex(_initialIndex);
}, [_initialIndex]);
setCurrentIndex(initialIndex);
}, [initialIndex]);

useAnimatedReaction(() => {
return currentPage.value;
Expand All @@ -137,7 +119,7 @@ const TabController = React.forwardRef((props: PropsWithChildren<TabControllerPr
const context = useMemo(() => {
return {
/* Pass Props */
initialIndex: _initialIndex,
initialIndex,
asCarousel,
pageWidth,
/* Items */
Expand All @@ -147,13 +129,12 @@ const TabController = React.forwardRef((props: PropsWithChildren<TabControllerPr
/* Animated Values */
targetPage,
currentPage,
// carouselOffset,
containerWidth: screenWidth,
/* Callbacks */
onChangeIndex,
setCurrentIndex
};
}, [_initialIndex, asCarousel, items, onChangeIndex, screenWidth]);
}, [initialIndex, asCarousel, items, onChangeIndex, screenWidth]);

return <TabBarContext.Provider value={context}>{children}</TabBarContext.Provider>;
});
Expand Down
2 changes: 1 addition & 1 deletion typings/incubator/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {BadgeProps} from '../components/Badge';

export namespace Incubator {
export interface TabControllerProps {
selectedIndex?: number;
initialIndex?: number;
onChangeIndex?: (index: number) => void;
asCarousel?: boolean;
}
Expand Down