@@ -3,13 +3,14 @@ import {
3
3
Animated ,
4
4
StyleSheet ,
5
5
View ,
6
- ScrollView ,
7
6
StyleProp ,
8
7
ViewStyle ,
9
8
TextStyle ,
10
9
LayoutChangeEvent ,
11
10
I18nManager ,
12
11
Platform ,
12
+ FlatList ,
13
+ ListRenderItemInfo ,
13
14
} from 'react-native' ;
14
15
import TabBarItem , { Props as TabBarItemProps } from './TabBarItem' ;
15
16
import TabBarIndicator , { Props as IndicatorProps } from './TabBarIndicator' ;
@@ -131,7 +132,7 @@ export default class TabBar<T extends Route> extends React.Component<
131
132
132
133
private scrollAmount = new Animated . Value ( 0 ) ;
133
134
134
- private scrollViewRef = React . createRef < ScrollView > ( ) ;
135
+ private flatListRef = React . createRef < FlatList > ( ) ;
135
136
136
137
private getFlattenedTabWidth = ( style : StyleProp < ViewStyle > ) => {
137
138
const tabStyle = StyleSheet . flatten ( style ) ;
@@ -166,7 +167,6 @@ export default class TabBar<T extends Route> extends React.Component<
166
167
if ( scrollEnabled ) {
167
168
return ( layout . width / 5 ) * 2 ;
168
169
}
169
-
170
170
return layout . width / routes . length ;
171
171
} ;
172
172
@@ -248,8 +248,8 @@ export default class TabBar<T extends Route> extends React.Component<
248
248
249
249
private resetScroll = ( index : number ) => {
250
250
if ( this . props . scrollEnabled ) {
251
- this . scrollViewRef . current ?. scrollTo ( {
252
- x : this . getScrollAmount ( this . props , this . state , index ) ,
251
+ this . flatListRef . current ?. scrollToOffset ( {
252
+ offset : this . getScrollAmount ( this . props , this . state , index ) ,
253
253
animated : true ,
254
254
} ) ;
255
255
}
@@ -367,7 +367,9 @@ export default class TabBar<T extends Route> extends React.Component<
367
367
} ) }
368
368
</ Animated . View >
369
369
< View style = { styles . scroll } >
370
- < Animated . ScrollView
370
+ < Animated . FlatList
371
+ data = { routes as Animated . WithAnimatedValue < T > [ ] }
372
+ keyExtractor = { ( item ) => item . key }
371
373
horizontal
372
374
accessibilityRole = "tablist"
373
375
keyboardShouldPersistTaps = "handled"
@@ -392,19 +394,7 @@ export default class TabBar<T extends Route> extends React.Component<
392
394
contentContainerStyle ,
393
395
] }
394
396
scrollEventThrottle = { 16 }
395
- onScroll = { Animated . event (
396
- [
397
- {
398
- nativeEvent : {
399
- contentOffset : { x : this . scrollAmount } ,
400
- } ,
401
- } ,
402
- ] ,
403
- { useNativeDriver : true }
404
- ) }
405
- ref = { this . scrollViewRef }
406
- >
407
- { routes . map ( ( route : T , index ) => {
397
+ renderItem = { ( { item : route , index } : ListRenderItemInfo < T > ) => {
408
398
const props : TabBarItemProps < T > & { key : string } = {
409
399
key : route . key ,
410
400
position : position ,
@@ -459,7 +449,20 @@ export default class TabBar<T extends Route> extends React.Component<
459
449
} ,
460
450
onLongPress : ( ) => onTabLongPress ?.( { route } ) ,
461
451
labelStyle : labelStyle ,
462
- style : tabStyle ,
452
+ style : [
453
+ tabStyle ,
454
+ // Calculate the deafult width for tab for FlatList to work.
455
+ this . getFlattenedTabWidth ( tabStyle ) === undefined && {
456
+ width : this . getComputedTabWidth (
457
+ index ,
458
+ layout ,
459
+ routes ,
460
+ scrollEnabled ,
461
+ tabWidths ,
462
+ this . getFlattenedTabWidth ( tabStyle )
463
+ ) ,
464
+ } ,
465
+ ] ,
463
466
} ;
464
467
465
468
return (
@@ -472,8 +475,19 @@ export default class TabBar<T extends Route> extends React.Component<
472
475
) }
473
476
</ React . Fragment >
474
477
) ;
475
- } ) }
476
- </ Animated . ScrollView >
478
+ } }
479
+ onScroll = { Animated . event (
480
+ [
481
+ {
482
+ nativeEvent : {
483
+ contentOffset : { x : this . scrollAmount } ,
484
+ } ,
485
+ } ,
486
+ ] ,
487
+ { useNativeDriver : true }
488
+ ) }
489
+ ref = { this . flatListRef }
490
+ />
477
491
</ View >
478
492
</ Animated . View >
479
493
) ;
0 commit comments