@@ -87,12 +87,22 @@ export default class Carousel extends BaseComponent {
87
87
/**
88
88
* Whether to layout Carousel for accessibility
89
89
*/
90
- allowAccessibleLayout : PropTypes . bool
90
+ allowAccessibleLayout : PropTypes . bool ,
91
+ /**
92
+ * Whether to switch automatically between the pages
93
+ */
94
+ autoplay : PropTypes . bool ,
95
+ /**
96
+ * the amount of ms to wait before switching to the next page, in case autoplay is on
97
+ */
98
+ autoplayInterval : PropTypes . number
91
99
} ;
92
100
93
101
static defaultProps = {
94
102
initialPage : 0 ,
95
- pagingEnabled : true
103
+ pagingEnabled : true ,
104
+ autoplay : false ,
105
+ autoplayInterval : 4000
96
106
} ;
97
107
98
108
static pageControlPositions = PAGE_CONTROL_POSITIONS ;
@@ -115,10 +125,24 @@ export default class Carousel extends BaseComponent {
115
125
116
126
componentDidMount ( ) {
117
127
Constants . addDimensionsEventListener ( this . onOrientationChanged ) ;
128
+
129
+ if ( this . props . autoplay ) {
130
+ this . startAutoPlay ( ) ;
131
+ }
118
132
}
119
133
120
134
componentWillUnmount ( ) {
121
135
Constants . removeDimensionsEventListener ( this . onOrientationChanged ) ;
136
+ clearInterval ( this . autoplayTimer ) ;
137
+ }
138
+
139
+ componentDidUpdate ( prevProps , prevState ) {
140
+ const { autoplay} = this . props ;
141
+ if ( autoplay && ! prevProps . autoplay ) {
142
+ this . startAutoPlay ( ) ;
143
+ } else if ( ! autoplay && prevProps . autoplay ) {
144
+ this . stopAutoPlay ( ) ;
145
+ }
122
146
}
123
147
124
148
onOrientationChanged = ( ) => {
@@ -168,6 +192,23 @@ export default class Carousel extends BaseComponent {
168
192
}
169
193
} ;
170
194
195
+
196
+
197
+ startAutoPlay ( ) {
198
+ this . autoplayTimer = setInterval ( ( ) => {
199
+ this . goToNextPage ( ) ;
200
+ } , this . props . autoplayInterval ) ;
201
+ }
202
+
203
+ stopAutoPlay ( ) {
204
+ clearInterval ( this . autoplayTimer ) ;
205
+ }
206
+
207
+ resetAutoPlay ( ) {
208
+ this . stopAutoPlay ( ) ;
209
+ this . startAutoPlay ( ) ;
210
+ }
211
+
171
212
goToPage ( pageIndex , animated = true ) {
172
213
this . setState ( { currentPage : this . getCalcIndex ( pageIndex ) } , ( ) => this . updateOffset ( animated ) ) ;
173
214
}
@@ -243,13 +284,34 @@ export default class Carousel extends BaseComponent {
243
284
}
244
285
} ;
245
286
287
+ goToNextPage ( ) {
288
+ const { currentPage} = this . state ;
289
+ const pagesCount = presenter . getChildrenLength ( this . getThemeProps ( ) ) ;
290
+ const { loop} = this . getThemeProps ( ) ;
291
+
292
+ let nextPageIndex ;
293
+ if ( loop ) {
294
+ nextPageIndex = currentPage + 1 ;
295
+ } else {
296
+ nextPageIndex = Math . min ( pagesCount - 1 , currentPage + 1 ) ;
297
+ }
298
+
299
+ this . goToPage ( nextPageIndex , true ) ;
300
+
301
+ // in case of a loop, after we advanced right to the cloned first page,
302
+ // we return silently to the real first page
303
+ if ( loop && currentPage === pagesCount ) {
304
+ this . goToPage ( 0 , false ) ;
305
+ }
306
+ }
307
+
246
308
onScroll = event => {
247
309
if ( ! this . skippedInitialScroll ) {
248
310
this . skippedInitialScroll = true ;
249
311
return ;
250
312
}
251
313
252
- const { loop} = this . getThemeProps ( ) ;
314
+ const { loop, autoplay } = this . getThemeProps ( ) ;
253
315
const { pageWidth} = this . state ;
254
316
const offsetX = event . nativeEvent . contentOffset . x ;
255
317
@@ -265,6 +327,10 @@ export default class Carousel extends BaseComponent {
265
327
this . updateOffset ( ) ;
266
328
}
267
329
330
+ if ( autoplay ) { // reset the timer to avoid auto scroll immediately after manual one
331
+ this . resetAutoPlay ( ) ;
332
+ }
333
+
268
334
_ . invoke ( this . props , 'onScroll' , event ) ;
269
335
} ;
270
336
0 commit comments