@@ -52,7 +52,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
52
52
const defaultPageWidth = props . loop || ! props . pageWidth ? Constants . screenWidth : props . pageWidth ;
53
53
const pageHeight = props . pageHeight ?? Constants . screenHeight ;
54
54
this . isAutoScrolled = false ;
55
-
55
+
56
56
this . state = {
57
57
containerWidth : undefined ,
58
58
// @ts -ignore (defaultProps)
@@ -195,28 +195,30 @@ class Carousel extends Component<CarouselProps, CarouselState> {
195
195
}
196
196
197
197
goToPage ( pageIndex : number , animated = true ) {
198
- this . setState ( { currentPage : this . getCalcIndex ( pageIndex ) } , ( ) => this . updateOffset ( animated ) ) ;
198
+ this . setState ( { currentPage : pageIndex } , ( ) => this . updateOffset ( animated ) ) ;
199
199
}
200
200
201
201
goToNextPage ( ) {
202
202
const { currentPage} = this . state ;
203
203
const pagesCount = presenter . getChildrenLength ( this . props ) ;
204
204
const { loop} = this . props ;
205
205
let nextPageIndex ;
206
-
207
206
if ( loop ) {
207
+ if ( currentPage === pagesCount + 1 ) {
208
+ this . goToPage ( 0 , false ) ;
209
+ return ;
210
+ }
208
211
nextPageIndex = currentPage + 1 ;
209
212
} else {
210
213
nextPageIndex = Math . min ( pagesCount - 1 , currentPage + 1 ) ;
211
214
}
212
215
213
216
this . goToPage ( nextPageIndex , true ) ;
214
-
215
- // in case of a loop, after we advanced right to the cloned first page,
216
- // we return silently to the real first page
217
- if ( loop && currentPage === pagesCount ) {
218
- this . goToPage ( 0 , false ) ;
219
- }
217
+ // // in case of a loop, after we advanced right to the cloned first page,
218
+ // // we return silently to the real first page
219
+ // if (loop && currentPage === pagesCount) {
220
+ // this.goToPage(0, false);
221
+ // }
220
222
}
221
223
222
224
getCalcIndex ( index : number ) : number {
@@ -239,10 +241,8 @@ class Carousel extends Component<CarouselProps, CarouselState> {
239
241
if ( containerWidth ) {
240
242
const spacings = pageWidth === containerWidth ? 0 : this . getItemSpacings ( this . props ) ;
241
243
const initialBreak = pageWidth - ( containerWidth - pageWidth - spacings ) / 2 ;
242
- const snapToOffsets = _ . times (
243
- presenter . getChildrenLength ( this . props ) ,
244
- index => initialBreak + index * pageWidth + this . getContainerMarginHorizontal ( )
245
- ) ;
244
+ const snapToOffsets = _ . times ( presenter . getChildrenLength ( this . props ) ,
245
+ index => initialBreak + index * pageWidth + this . getContainerMarginHorizontal ( ) ) ;
246
246
return snapToOffsets ;
247
247
}
248
248
} ;
@@ -307,14 +307,13 @@ class Carousel extends Component<CarouselProps, CarouselState> {
307
307
onMomentumScrollEnd = ( ) => {
308
308
// finished full page scroll
309
309
const { currentStandingPage, currentPage} = this . state ;
310
- const index = this . getCalcIndex ( currentPage ) ;
311
310
const pagesCount = presenter . getChildrenLength ( this . props ) ;
312
311
313
- if ( index < pagesCount ) {
314
- this . setState ( { currentStandingPage : index } ) ;
312
+ if ( currentPage < pagesCount ) {
313
+ this . setState ( { currentStandingPage : currentPage } ) ;
315
314
316
- if ( currentStandingPage !== index ) {
317
- this . props . onChangePage ?.( index , currentStandingPage , { isAutoScrolled : this . isAutoScrolled } ) ;
315
+ if ( currentStandingPage !== currentPage ) {
316
+ this . props . onChangePage ?.( currentPage , currentStandingPage , { isAutoScrolled : this . isAutoScrolled } ) ;
318
317
this . isAutoScrolled = false ;
319
318
}
320
319
}
@@ -355,10 +354,11 @@ class Carousel extends Component<CarouselProps, CarouselState> {
355
354
} ;
356
355
357
356
onScrollEvent = Animated . event ( [
358
- { nativeEvent :
359
- { contentOffset :
360
- // @ts -ignore
361
- { y : this . props ?. animatedScrollOffset ?. y , x : this . props ?. animatedScrollOffset ?. x }
357
+ {
358
+ nativeEvent : {
359
+ contentOffset :
360
+ // @ts -ignore
361
+ { y : this . props ?. animatedScrollOffset ?. y , x : this . props ?. animatedScrollOffset ?. x }
362
362
}
363
363
}
364
364
] ,
@@ -404,9 +404,10 @@ class Carousel extends Component<CarouselProps, CarouselState> {
404
404
} ;
405
405
406
406
renderChildren ( ) {
407
- const { children, loop} = this . props ;
407
+ const { children : propsChildren , loop} = this . props ;
408
408
const length = presenter . getChildrenLength ( this . props ) ;
409
-
409
+ const children =
410
+ Constants . isRTL && Constants . isAndroid ? React . Children . toArray ( propsChildren ) . reverse ( ) : propsChildren ;
410
411
const childrenArray = React . Children . map ( children , ( child , index ) => {
411
412
return this . renderChild ( child , `${ index } ` ) ;
412
413
} ) ;
@@ -423,6 +424,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
423
424
424
425
renderPageControl ( ) {
425
426
const { pageControlPosition, pageControlProps = { } } = this . props ;
427
+ const { currentStandingPage} = this . state ;
426
428
427
429
if ( pageControlPosition ) {
428
430
const {
@@ -447,7 +449,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
447
449
color = { color }
448
450
{ ...others }
449
451
numOfPages = { pagesCount }
450
- currentPage = { this . getCalcIndex ( this . state . currentPage ) }
452
+ currentPage = { currentStandingPage }
451
453
/>
452
454
) ;
453
455
}
@@ -471,7 +473,6 @@ class Carousel extends Component<CarouselProps, CarouselState> {
471
473
472
474
renderAccessibleLayout ( ) {
473
475
const { containerStyle, children, testID} = this . props ;
474
-
475
476
return (
476
477
< View style = { containerStyle } onLayout = { this . onContainerLayout } >
477
478
< ScrollView
@@ -492,20 +493,17 @@ class Carousel extends Component<CarouselProps, CarouselState> {
492
493
}
493
494
494
495
renderCarousel ( ) {
495
- const { containerStyle, animated, horizontal, animatedScrollOffset, ...others } = this . props ;
496
+ const { containerStyle, animated, horizontal, animatedScrollOffset, style , ...others } = this . props ;
496
497
const scrollContainerStyle = this . shouldUsePageWidth ( )
497
498
? { paddingRight : this . getItemSpacings ( this . props ) }
498
499
: undefined ;
499
500
const snapToOffsets = this . getSnapToOffsets ( ) ;
500
501
const marginBottom = Math . max ( 0 , this . getContainerPaddingVertical ( ) - 16 ) ;
501
502
const ScrollContainer = animatedScrollOffset ? Animated . ScrollView : ScrollView ;
502
503
const contentOffset = this . getInitialContentOffset ( snapToOffsets ) ;
504
+ const _style = Constants . isRTL && Constants . isAndroid ? [ styles . invertedView , style ] : style ;
503
505
return (
504
- < View
505
- animated = { animated }
506
- style = { [ { marginBottom} , containerStyle ] }
507
- onLayout = { this . onContainerLayout }
508
- >
506
+ < View animated = { animated } style = { [ { marginBottom} , containerStyle ] } onLayout = { this . onContainerLayout } >
509
507
< ScrollContainer
510
508
showsHorizontalScrollIndicator = { false }
511
509
showsVerticalScrollIndicator = { false }
@@ -521,6 +519,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
521
519
contentOffset = { contentOffset }
522
520
// onContentSizeChange={this.onContentSizeChange}
523
521
onMomentumScrollEnd = { this . onMomentumScrollEnd }
522
+ style = { _style }
524
523
>
525
524
{ this . renderChildren ( ) }
526
525
</ ScrollContainer >
@@ -556,5 +555,8 @@ const styles = StyleSheet.create({
556
555
hiddenText : {
557
556
position : 'absolute' ,
558
557
width : 1
558
+ } ,
559
+ invertedView : {
560
+ transform : [ { scaleX : - 1 } ]
559
561
}
560
562
} ) ;
0 commit comments