1
1
import _ from 'lodash' ;
2
2
import React , { Component , RefObject , ReactNode , Key } from 'react' ;
3
- import { Animated , ScrollView , StyleSheet , LayoutChangeEvent , NativeSyntheticEvent , NativeScrollEvent } from 'react-native' ;
3
+ import {
4
+ Animated ,
5
+ ScrollView ,
6
+ StyleSheet ,
7
+ LayoutChangeEvent ,
8
+ NativeSyntheticEvent ,
9
+ NativeScrollEvent
10
+ } from 'react-native' ;
4
11
import { Constants } from '../../helpers' ;
5
12
import { Colors } from '../../style' ;
6
13
import { asBaseComponent } from '../../commons/new' ;
@@ -11,7 +18,7 @@ import * as presenter from './CarouselPresenter';
11
18
import { CarouselProps , CarouselState , PageControlPosition } from './types' ;
12
19
export { CarouselProps , PageControlPosition } ;
13
20
14
- type DefaultProps = Partial < CarouselProps >
21
+ type DefaultProps = Partial < CarouselProps > ;
15
22
16
23
/**
17
24
* @description : Carousel for scrolling pages horizontally
@@ -67,8 +74,10 @@ class Carousel extends Component<CarouselProps, CarouselState> {
67
74
const { pageWidth, pageHeight} = prevProps ;
68
75
const { pageWidth : nextPageWidth , pageHeight : nextPageHeight } = nextProps ;
69
76
70
- if ( ( ! _ . isUndefined ( nextPageWidth ) && pageWidth !== nextPageWidth )
71
- || ( ! _ . isUndefined ( nextPageHeight ) && pageHeight !== nextPageHeight ) ) {
77
+ if (
78
+ ( ! _ . isUndefined ( nextPageWidth ) && pageWidth !== nextPageWidth ) ||
79
+ ( ! _ . isUndefined ( nextPageHeight ) && pageHeight !== nextPageHeight )
80
+ ) {
72
81
const pageWidth = nextPageWidth as number ;
73
82
const pageHeight = nextPageHeight as number ;
74
83
@@ -171,7 +180,6 @@ class Carousel extends Component<CarouselProps, CarouselState> {
171
180
this . isAutoScrolled = true ;
172
181
this . goToNextPage ( ) ;
173
182
} , this . props . autoplayInterval ) ;
174
-
175
183
}
176
184
177
185
stopAutoPlay ( ) {
@@ -221,7 +229,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
221
229
222
230
shouldEnablePagination ( ) {
223
231
const { pagingEnabled, horizontal} = this . props ;
224
- return horizontal ? ( pagingEnabled && ! this . shouldUsePageWidth ( ) ) : true ;
232
+ return horizontal ? pagingEnabled && ! this . shouldUsePageWidth ( ) : true ;
225
233
}
226
234
227
235
onContainerLayout = ( {
@@ -261,7 +269,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
261
269
if ( index < pagesCount ) {
262
270
this . setState ( { currentStandingPage : index } ) ;
263
271
if ( currentStandingPage !== index ) {
264
- this . props . onChangePage ?.( index , currentStandingPage , this . isAutoScrolled ) ;
272
+ this . props . onChangePage ?.( index , currentStandingPage , { isAutoScrolled : this . isAutoScrolled } ) ;
265
273
this . isAutoScrolled = false ;
266
274
}
267
275
}
@@ -280,13 +288,12 @@ class Carousel extends Component<CarouselProps, CarouselState> {
280
288
}
281
289
282
290
this . goToPage ( nextPageIndex , true ) ;
283
-
291
+
284
292
// in case of a loop, after we advanced right to the cloned first page,
285
293
// we return silently to the real first page
286
294
if ( loop && currentPage === pagesCount ) {
287
295
this . goToPage ( 0 , false ) ;
288
296
}
289
-
290
297
}
291
298
292
299
onScroll = ( event : NativeSyntheticEvent < NativeScrollEvent > ) => {
@@ -325,16 +332,17 @@ class Carousel extends Component<CarouselProps, CarouselState> {
325
332
} ;
326
333
327
334
// @ts -ignore
328
- onScrollEvent = Animated . event ( [ { nativeEvent : { contentOffset : { y : this . props ?. animatedScrollOffset ?. y , x : this . props ?. animatedScrollOffset ?. x } } } ] , {
329
- useNativeDriver : true ,
330
- listener : this . onScroll
331
- } ) ;
335
+ onScrollEvent = Animated . event ( [ { nativeEvent : { contentOffset : { y : this . props ?. animatedScrollOffset ?. y , x : this . props ?. animatedScrollOffset ?. x } } } ] ,
336
+ {
337
+ useNativeDriver : true ,
338
+ listener : this . onScroll
339
+ } ) ;
332
340
333
341
renderChild = ( child : ReactNode , key : Key ) : JSX . Element | undefined => {
334
342
if ( child ) {
335
343
const { pageWidth, pageHeight} = this . state ;
336
344
const { horizontal} = this . props ;
337
- const paddingLeft = horizontal ? this . shouldUsePageWidth ( ) ? this . getItemSpacings ( this . props ) : undefined : 0 ;
345
+ const paddingLeft = horizontal ? ( this . shouldUsePageWidth ( ) ? this . getItemSpacings ( this . props ) : undefined ) : 0 ;
338
346
const index = Number ( key ) ;
339
347
const length = presenter . getChildrenLength ( this . props ) ;
340
348
const containerMarginHorizontal = this . getContainerMarginHorizontal ( ) ;
@@ -430,7 +438,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
430
438
const { containerStyle, children} = this . props ;
431
439
432
440
return (
433
- < View style = { containerStyle } onLayout = { this . onContainerLayout } >
441
+ < View style = { containerStyle } onLayout = { this . onContainerLayout } >
434
442
< ScrollView
435
443
ref = { this . carousel }
436
444
showsVerticalScrollIndicator = { false }
0 commit comments