1
1
// TODO: support commented props
2
- import React , { PropsWithChildren , useMemo } from 'react' ;
2
+ import React , { PropsWithChildren , useMemo , useEffect } from 'react' ;
3
3
import _ from 'lodash' ;
4
4
import { useAnimatedReaction , useSharedValue , withTiming , runOnJS } from 'react-native-reanimated' ;
5
5
import { Constants } from '../../helpers' ;
6
6
import { asBaseComponent } from '../../commons/new' ;
7
+ import { LogService } from '../../services' ;
7
8
import TabBarContext from './TabBarContext' ;
8
9
import TabBar from './TabBar' ;
9
10
import TabBarItem , { TabControllerItemProps } from './TabBarItem' ;
@@ -20,7 +21,11 @@ export interface TabControllerProps {
20
21
/**
21
22
* Initial selected index
22
23
*/
23
- selectedIndex : number ;
24
+ initialIndex ?: number ;
25
+ /**
26
+ * DEPRECATED: use initialIndex instead
27
+ */
28
+ selectedIndex ?: number ;
24
29
/**
25
30
* callback for when index has change (will not be called on ignored items)
26
31
*/
@@ -43,7 +48,8 @@ export interface TabControllerProps {
43
48
* @importantLink : https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#with-wix-react-native-navigation-https-githubcom-wix-react-native-navigation
44
49
*/
45
50
function TabController ( {
46
- selectedIndex = 0 ,
51
+ initialIndex = 0 ,
52
+ selectedIndex,
47
53
asCarousel = false ,
48
54
items,
49
55
onChangeIndex = _ . noop ,
@@ -58,13 +64,26 @@ function TabController({
58
64
return _ . filter < TabControllerItemProps [ ] > ( items , ( item : TabControllerItemProps ) => item . ignore ) ;
59
65
} , [ items ] ) ;
60
66
67
+ /* backwards compatibility for `selectedIndex` prop. this line eventually should be removed */
68
+ initialIndex = selectedIndex || initialIndex ;
69
+
61
70
/* currentPage - static page index */
62
- const currentPage = useSharedValue ( selectedIndex ) ;
71
+ const currentPage = useSharedValue ( initialIndex ) ;
63
72
/* 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 ) ) ;
66
75
const containerWidth = useSharedValue ( pageWidth ) ;
67
76
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
+
68
87
useAnimatedReaction ( ( ) => {
69
88
return currentPage . value ;
70
89
} ,
@@ -78,7 +97,7 @@ function TabController({
78
97
const context = useMemo ( ( ) => {
79
98
return {
80
99
/* Pass Props */
81
- selectedIndex ,
100
+ initialIndex ,
82
101
asCarousel,
83
102
pageWidth,
84
103
/* Items */
@@ -92,7 +111,7 @@ function TabController({
92
111
/* Callbacks */
93
112
onChangeIndex
94
113
} ;
95
- } , [ /* selectedIndex ,*/ asCarousel , items , onChangeIndex ] ) ;
114
+ } , [ /* initialIndex ,*/ initialIndex , asCarousel , items , onChangeIndex ] ) ;
96
115
97
116
return < TabBarContext . Provider value = { context } > { children } </ TabBarContext . Provider > ;
98
117
}
0 commit comments