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