@@ -300,18 +300,24 @@ export default class Carousel extends Component {
300
300
* @param {Boolean } autoSlide - The source of slide transition, should be true for autoPlay and false for user click.
301
301
*/
302
302
goToSlide = ( index , direction , autoSlide = false ) => {
303
- const { beforeChange, transitionDuration, transition, onSlideTransitioned } = this . props ;
303
+ const { beforeChange, transitionDuration, transition, onSlideTransitioned, children } = this . props ;
304
+ const { currentSlide } = this . state ;
305
+ const lastIndex = Children . count ( children ) - 1 ;
306
+
307
+ const newIndex = index < 0 ? lastIndex + index + 1 :
308
+ index <= lastIndex ? index : index - lastIndex - 1 ;
309
+
310
+ direction = direction || ( index > currentSlide ? 'right' : 'left' ) ;
304
311
305
312
if ( onSlideTransitioned ) {
306
313
onSlideTransitioned ( {
307
314
autoPlay : autoSlide ,
308
- index,
315
+ index : newIndex ,
309
316
direction
310
317
} ) ;
311
318
}
312
319
313
- const { currentSlide } = this . state ;
314
- if ( currentSlide === index ) {
320
+ if ( currentSlide === newIndex ) {
315
321
return ;
316
322
}
317
323
@@ -326,7 +332,7 @@ export default class Carousel extends Component {
326
332
transitionDuration
327
333
} , ( ) => {
328
334
this . setState ( {
329
- currentSlide : index ,
335
+ currentSlide : newIndex ,
330
336
direction,
331
337
transitioningFrom : currentSlide
332
338
} , ( ) => {
@@ -343,20 +349,16 @@ export default class Carousel extends Component {
343
349
* @param {Object } e - The event that calls nextSlide, will be undefined for autoPlay.
344
350
*/
345
351
nextSlide = ( e ) => {
346
- const { children } = this . props ;
347
352
const { currentSlide } = this . state ;
348
- const newIndex = currentSlide < Children . count ( children ) - 1 ? currentSlide + 1 : 0 ;
349
- this . goToSlide ( newIndex , 'right' , typeof e !== 'object' ) ;
353
+ this . goToSlide ( currentSlide + 1 , 'right' , typeof e !== 'object' ) ;
350
354
}
351
355
352
356
/**
353
357
* Transitions to the previous slide moving from right to left.
354
358
*/
355
359
prevSlide = ( ) => {
356
- const { children } = this . props ;
357
360
const { currentSlide } = this . state ;
358
- const newIndex = currentSlide > 0 ? currentSlide - 1 : Children . count ( children ) - 1 ;
359
- this . goToSlide ( newIndex , 'left' ) ;
361
+ this . goToSlide ( currentSlide - 1 , 'left' ) ;
360
362
}
361
363
362
364
/**
@@ -585,6 +587,7 @@ export default class Carousel extends Component {
585
587
style = { loadingSlideStyle }
586
588
data-index = { index }
587
589
className = { classnames ( slideClasses , LOADING_CLASS ) }
590
+ onClick = { this . handleSlideClick }
588
591
> </ li >
589
592
) ;
590
593
} ) ;
0 commit comments