1
1
import clsx from 'clsx' ;
2
- import React , { useCallback , useEffect , useRef , useState } from 'react' ;
2
+ import React , { ReactElement , useCallback , useEffect , useRef , useState } from 'react' ;
3
3
import type { CarouselProps } from './types' ;
4
4
5
5
import CarouselIndicators from './CarouselIndicators/CarouselIndicators' ;
@@ -8,13 +8,14 @@ import { CarouselContext } from './utils/CarouselContext';
8
8
import { getCarouselItems , isVisible , queueCallback , reflow } from './utils/utils' ;
9
9
10
10
const MDBCarousel : React . FC < CarouselProps > = ( {
11
- fade,
11
+ fade = false ,
12
12
className,
13
+ carouselInnerClassName,
13
14
dark,
14
15
children,
15
- interval,
16
- keyboard,
17
- touch,
16
+ interval = 5000 ,
17
+ keyboard = false ,
18
+ touch = true ,
18
19
showControls,
19
20
showIndicators,
20
21
onSlide,
@@ -34,14 +35,7 @@ const MDBCarousel: React.FC<CarouselProps> = ({
34
35
const carouselRef = useRef < HTMLDivElement > ( null ) ;
35
36
36
37
const classes = clsx ( 'carousel' , 'slide' , fade && 'carousel-fade' , dark && 'carousel-dark' , className ) ;
37
-
38
- const pauseInterval = ( ) => {
39
- if ( carouselInterval . current ) {
40
- clearInterval ( carouselInterval . current ) ;
41
-
42
- carouselInterval . current = null ;
43
- }
44
- } ;
38
+ const carouselInnerClasses = clsx ( 'carousel-inner' , carouselInnerClassName ) ;
45
39
46
40
const setElementActive = useCallback (
47
41
( direction : string , newIndex ?: number ) => {
@@ -59,8 +53,17 @@ const MDBCarousel: React.FC<CarouselProps> = ({
59
53
[ active , itemsLength ]
60
54
) ;
61
55
56
+ const pauseInterval = useCallback ( ( ) => {
57
+ if ( carouselInterval . current ) {
58
+ clearInterval ( carouselInterval . current ) ;
59
+
60
+ carouselInterval . current = null ;
61
+ }
62
+ } , [ ] ) ;
63
+
62
64
const slide = useCallback (
63
65
( direction : string , nextElement : HTMLDivElement , index ?: number ) => {
66
+ if ( ! items . current || items . current . length < 2 ) return ;
64
67
setIsTransitioning ( true ) ;
65
68
const carouselElements = items . current as HTMLDivElement [ ] ;
66
69
const activeElement = carouselElements [ active ] ;
@@ -114,7 +117,7 @@ const MDBCarousel: React.FC<CarouselProps> = ({
114
117
}
115
118
} ,
116
119
117
- [ carouselRef , active , setElementActive ]
120
+ [ carouselRef , active , setElementActive , pauseInterval ]
118
121
) ;
119
122
120
123
const block = ( timer : number ) => {
@@ -194,14 +197,15 @@ const MDBCarousel: React.FC<CarouselProps> = ({
194
197
} , [ carouselRef , changeStep ] ) ;
195
198
196
199
const startInterval = useCallback ( ( ) => {
200
+ const individualInterval = ( children as ReactElement [ ] ) ?. [ active ] ?. props ?. interval ;
197
201
if ( carouselInterval . current ) {
198
202
clearInterval ( carouselInterval . current ) ;
199
203
200
204
carouselInterval . current = null ;
201
205
}
202
206
203
- carouselInterval . current = setInterval ( changeNext , interval ) ;
204
- } , [ changeNext , interval ] ) ;
207
+ carouselInterval . current = setInterval ( changeNext , individualInterval || interval ) ;
208
+ } , [ changeNext , interval , children , active ] ) ;
205
209
206
210
const startTouch = ( e : React . TouchEvent < HTMLDivElement > ) => {
207
211
if ( ! touch ) return ;
@@ -277,12 +281,16 @@ const MDBCarousel: React.FC<CarouselProps> = ({
277
281
} , [ carouselRef ] ) ;
278
282
279
283
useEffect ( ( ) => {
280
- onSlide ?.( ) ;
284
+ isTransitioning && onSlide ?.( ) ;
281
285
} , [ isTransitioning , onSlide ] ) ;
282
286
283
287
useEffect ( ( ) => {
284
288
startInterval ( ) ;
285
- } , [ startInterval ] ) ;
289
+
290
+ return ( ) => {
291
+ pauseInterval ( ) ;
292
+ } ;
293
+ } , [ startInterval , pauseInterval ] ) ;
286
294
287
295
return (
288
296
< div
@@ -295,7 +303,7 @@ const MDBCarousel: React.FC<CarouselProps> = ({
295
303
ref = { carouselRef }
296
304
{ ...props }
297
305
>
298
- < div className = 'carousel-inner' >
306
+ < div className = { carouselInnerClasses } >
299
307
< CarouselContext . Provider value = { { active } } >
300
308
{ showIndicators && < CarouselIndicators to = { changeTo } imagesCount = { itemsLength } /> }
301
309
{ children }
@@ -306,6 +314,4 @@ const MDBCarousel: React.FC<CarouselProps> = ({
306
314
) ;
307
315
} ;
308
316
309
- MDBCarousel . defaultProps = { fade : false , interval : 5000 , touch : true , keyboard : false } ;
310
-
311
317
export default MDBCarousel ;
0 commit comments