@@ -53,6 +53,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
53
53
const defaultPageWidth = props . loop || ! props . pageWidth ? Constants . screenWidth : props . pageWidth ;
54
54
const pageHeight = props . pageHeight ?? Constants . screenHeight ;
55
55
this . isAutoScrolled = false ;
56
+
56
57
this . state = {
57
58
containerWidth : undefined ,
58
59
// @ts -ignore (defaultProps)
@@ -198,6 +199,27 @@ class Carousel extends Component<CarouselProps, CarouselState> {
198
199
this . setState ( { currentPage : this . getCalcIndex ( pageIndex ) } , ( ) => this . updateOffset ( animated ) ) ;
199
200
}
200
201
202
+ goToNextPage ( ) {
203
+ const { currentPage} = this . state ;
204
+ const pagesCount = presenter . getChildrenLength ( this . props ) ;
205
+ const { loop} = this . props ;
206
+ let nextPageIndex ;
207
+
208
+ if ( loop ) {
209
+ nextPageIndex = currentPage + 1 ;
210
+ } else {
211
+ nextPageIndex = Math . min ( pagesCount - 1 , currentPage + 1 ) ;
212
+ }
213
+
214
+ this . goToPage ( nextPageIndex , true ) ;
215
+
216
+ // in case of a loop, after we advanced right to the cloned first page,
217
+ // we return silently to the real first page
218
+ if ( loop && currentPage === pagesCount ) {
219
+ this . goToPage ( 0 , false ) ;
220
+ }
221
+ }
222
+
201
223
getCalcIndex ( index : number ) : number {
202
224
// to handle scrollView index issue in Android's RTL layout
203
225
if ( Constants . isRTL && Constants . isAndroid ) {
@@ -217,8 +239,10 @@ class Carousel extends Component<CarouselProps, CarouselState> {
217
239
if ( containerWidth ) {
218
240
const spacings = pageWidth === containerWidth ? 0 : this . getItemSpacings ( this . props ) ;
219
241
const initialBreak = pageWidth - ( containerWidth - pageWidth - spacings ) / 2 ;
220
- const snapToOffsets = _ . times ( presenter . getChildrenLength ( this . props ) ,
221
- index => initialBreak + index * pageWidth + this . getContainerMarginHorizontal ( ) ) ;
242
+ const snapToOffsets = _ . times (
243
+ presenter . getChildrenLength ( this . props ) ,
244
+ index => initialBreak + index * pageWidth + this . getContainerMarginHorizontal ( )
245
+ ) ;
222
246
return snapToOffsets ;
223
247
}
224
248
} ;
@@ -233,6 +257,18 @@ class Carousel extends Component<CarouselProps, CarouselState> {
233
257
return horizontal ? pagingEnabled && ! this . shouldUsePageWidth ( ) : true ;
234
258
}
235
259
260
+ shouldAllowAccessibilityLayout ( ) {
261
+ const { allowAccessibleLayout} = this . props ;
262
+ return allowAccessibleLayout && Constants . accessibility . isScreenReaderEnabled ;
263
+ }
264
+
265
+ onContentSizeChange = ( ) => {
266
+ // this is to handle initial scroll position (content offset)
267
+ if ( Constants . isAndroid ) {
268
+ this . updateOffset ( ) ;
269
+ }
270
+ } ;
271
+
236
272
onContainerLayout = ( {
237
273
nativeEvent : {
238
274
layout : { width : containerWidth , height : containerHeight }
@@ -249,54 +285,22 @@ class Carousel extends Component<CarouselProps, CarouselState> {
249
285
this . setState ( { containerWidth, pageWidth, pageHeight, initialOffset} ) ;
250
286
} ;
251
287
252
- shouldAllowAccessibilityLayout ( ) {
253
- const { allowAccessibleLayout} = this . props ;
254
- return allowAccessibleLayout && Constants . accessibility . isScreenReaderEnabled ;
255
- }
256
-
257
- onContentSizeChange = ( ) => {
258
- // this is to handle initial scroll position (content offset)
259
- if ( Constants . isAndroid ) {
260
- this . updateOffset ( ) ;
261
- }
262
- } ;
263
-
264
288
onMomentumScrollEnd = ( ) => {
265
289
// finished full page scroll
266
290
const { currentStandingPage, currentPage} = this . state ;
267
291
const index = this . getCalcIndex ( currentPage ) ;
268
-
269
292
const pagesCount = presenter . getChildrenLength ( this . props ) ;
293
+
270
294
if ( index < pagesCount ) {
271
295
this . setState ( { currentStandingPage : index } ) ;
296
+
272
297
if ( currentStandingPage !== index ) {
273
298
this . props . onChangePage ?.( index , currentStandingPage , { isAutoScrolled : this . isAutoScrolled } ) ;
274
299
this . isAutoScrolled = false ;
275
300
}
276
301
}
277
302
} ;
278
303
279
- goToNextPage ( ) {
280
- const { currentPage} = this . state ;
281
- const pagesCount = presenter . getChildrenLength ( this . props ) ;
282
- const { loop} = this . props ;
283
-
284
- let nextPageIndex ;
285
- if ( loop ) {
286
- nextPageIndex = currentPage + 1 ;
287
- } else {
288
- nextPageIndex = Math . min ( pagesCount - 1 , currentPage + 1 ) ;
289
- }
290
-
291
- this . goToPage ( nextPageIndex , true ) ;
292
-
293
- // in case of a loop, after we advanced right to the cloned first page,
294
- // we return silently to the real first page
295
- if ( loop && currentPage === pagesCount ) {
296
- this . goToPage ( 0 , false ) ;
297
- }
298
- }
299
-
300
304
onScroll = ( event : NativeSyntheticEvent < NativeScrollEvent > ) => {
301
305
if ( ! this . skippedInitialScroll ) {
302
306
this . skippedInitialScroll = true ;
@@ -307,7 +311,6 @@ class Carousel extends Component<CarouselProps, CarouselState> {
307
311
const { pageWidth, pageHeight} = this . state ;
308
312
const offsetX = event . nativeEvent . contentOffset . x ;
309
313
const offsetY = event . nativeEvent . contentOffset . y ;
310
-
311
314
const offset = horizontal ? offsetX : offsetY ;
312
315
const pageSize = horizontal ? pageWidth : pageHeight ;
313
316
@@ -333,11 +336,17 @@ class Carousel extends Component<CarouselProps, CarouselState> {
333
336
} ;
334
337
335
338
// @ts -ignore
336
- onScrollEvent = Animated . event ( [ { nativeEvent : { contentOffset : { y : this . props ?. animatedScrollOffset ?. y , x : this . props ?. animatedScrollOffset ?. x } } } ] ,
337
- {
338
- useNativeDriver : true ,
339
- listener : this . onScroll
340
- } ) ;
339
+ onScrollEvent = Animated . event ( [
340
+ { nativeEvent :
341
+ { contentOffset :
342
+ { y : this . props ?. animatedScrollOffset ?. y , x : this . props ?. animatedScrollOffset ?. x }
343
+ }
344
+ }
345
+ ] ,
346
+ {
347
+ useNativeDriver : true ,
348
+ listener : this . onScroll
349
+ } ) ;
341
350
342
351
renderChild = ( child : ReactNode , key : Key ) : JSX . Element | undefined => {
343
352
if ( child ) {
@@ -436,11 +445,12 @@ class Carousel extends Component<CarouselProps, CarouselState> {
436
445
}
437
446
438
447
renderAccessibleLayout ( ) {
439
- const { containerStyle, children} = this . props ;
448
+ const { containerStyle, children, testID } = this . props ;
440
449
441
450
return (
442
451
< View style = { containerStyle } onLayout = { this . onContainerLayout } >
443
452
< ScrollView
453
+ testID = { `${ testID } .scrollView` }
444
454
ref = { this . carousel }
445
455
showsVerticalScrollIndicator = { false }
446
456
pagingEnabled
@@ -457,7 +467,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
457
467
}
458
468
459
469
renderCarousel ( ) {
460
- const { containerStyle, animated, horizontal, animatedScrollOffset, ...others } = this . props ;
470
+ const { containerStyle, animated, horizontal, animatedScrollOffset, testID , ...others } = this . props ;
461
471
const { initialOffset} = this . state ;
462
472
const scrollContainerStyle = this . shouldUsePageWidth ( )
463
473
? { paddingRight : this . getItemSpacings ( this . props ) }
@@ -466,8 +476,14 @@ class Carousel extends Component<CarouselProps, CarouselState> {
466
476
const marginBottom = Math . max ( 0 , this . getContainerPaddingVertical ( ) - 16 ) ;
467
477
const ScrollContainer = animatedScrollOffset ? Animated . ScrollView : ScrollView ;
468
478
return (
469
- < View animated = { animated } style = { [ { marginBottom} , containerStyle ] } onLayout = { this . onContainerLayout } >
479
+ < View
480
+ animated = { animated }
481
+ style = { [ { marginBottom} , containerStyle ] }
482
+ onLayout = { this . onContainerLayout }
483
+ testID = { testID }
484
+ >
470
485
< ScrollContainer
486
+ testID = { `${ testID } .scrollView` }
471
487
showsHorizontalScrollIndicator = { false }
472
488
showsVerticalScrollIndicator = { false }
473
489
decelerationRate = "fast"
@@ -502,7 +518,7 @@ export default asBaseComponent<CarouselProps, Carousel & {pageControlPositions:
502
518
const styles = StyleSheet . create ( {
503
519
counter : {
504
520
paddingHorizontal : 8 ,
505
- paddingVertical : 3 , // height: 24,
521
+ paddingVertical : 3 ,
506
522
borderRadius : 20 ,
507
523
backgroundColor : Colors . rgba ( Colors . black , 0.6 ) ,
508
524
position : 'absolute' ,
0 commit comments