1
1
// TODO: support commented props
2
- import React , { useCallback , useContext , useEffect , useRef , useMemo , ReactElement } from 'react' ;
2
+ import React , { useCallback , useContext , useEffect , useRef , useMemo , ReactElement , useState } from 'react' ;
3
3
import { StyleSheet , TextStyle , LayoutChangeEvent , StyleProp , ViewStyle , TextProps } from 'react-native' ;
4
4
import _ from 'lodash' ;
5
- import Reanimated , { runOnJS , useAnimatedStyle , useSharedValue } from 'react-native-reanimated' ;
5
+ import Reanimated , { runOnJS , useAnimatedReaction , useAnimatedStyle , useSharedValue } from 'react-native-reanimated' ;
6
6
import { Gesture , GestureDetector } from 'react-native-gesture-handler' ;
7
7
import { Colors , Typography , Spacings } from '../../style' ;
8
8
import Badge , { BadgeProps } from '../badge' ;
@@ -144,6 +144,7 @@ export default function TabBarItem({
144
144
// JSON.parse(JSON.stringify is due to an issue with reanimated
145
145
const sharedLabelStyle = useSharedValue ( JSON . parse ( JSON . stringify ( StyleSheet . flatten ( labelStyle ) ) ) ) ;
146
146
const sharedSelectedLabelStyle = useSharedValue ( JSON . parse ( JSON . stringify ( StyleSheet . flatten ( selectedLabelStyle ) ) ) ) ;
147
+ const [ isSelected , setIsSelected ] = useState ( currentPage . value === index ) ;
147
148
148
149
// NOTE: We clone these color values in refs because they might contain a PlatformColor value
149
150
// which throws an error (see https://github.com/software-mansion/react-native-reanimated/issues/3164)
@@ -157,6 +158,12 @@ export default function TabBarItem({
157
158
}
158
159
} , [ ] ) ;
159
160
161
+ useAnimatedReaction ( ( ) => currentPage . value === index , ( isSelected , prevIsSelected ) => {
162
+ if ( isSelected !== prevIsSelected ) {
163
+ runOnJS ( setIsSelected ) ( isSelected ) ;
164
+ }
165
+ } ) ;
166
+
160
167
const onLayout = useCallback ( ( event : LayoutChangeEvent ) => {
161
168
const { width} = event . nativeEvent . layout ;
162
169
@@ -201,6 +208,8 @@ export default function TabBarItem({
201
208
return [ styles . tabItem , { flex} , style , constantWidthStyle , pressStyle ] ;
202
209
} , [ style , spreadItems ] ) ;
203
210
211
+ const accessibilityState = useMemo ( ( ) => ( { selected : isSelected } ) , [ isSelected ] ) ;
212
+
204
213
const gesture = Gesture . Tap ( )
205
214
. maxDuration ( 60000 )
206
215
. onEnd ( ( ) => {
@@ -226,6 +235,9 @@ export default function TabBarItem({
226
235
style = { _style }
227
236
onLayout = { onLayout }
228
237
testID = { testID }
238
+ accessible
239
+ accessibilityRole = "tab"
240
+ accessibilityState = { accessibilityState }
229
241
>
230
242
{ leadingAccessory }
231
243
{ icon && (
0 commit comments