@@ -10,7 +10,6 @@ const MDBCarousel: React.FC<CarouselProps> = ({
10
10
dark,
11
11
children,
12
12
carouselRef,
13
- interval,
14
13
keyboard,
15
14
pause,
16
15
touch,
@@ -23,8 +22,10 @@ const MDBCarousel: React.FC<CarouselProps> = ({
23
22
const [ imagesCount , setImagesCount ] = useState ( 0 ) ;
24
23
const [ activeItem , setActiveItem ] = useState ( 0 ) ;
25
24
const [ prevState , setPrevState ] = useState ( 0 ) ;
25
+ const [ clicked , setClicked ] = useState ( '' ) ;
26
26
const [ startInterval , setStartInterval ] = useState ( true ) ;
27
27
const [ clientTouch , setClientTouch ] = useState ( { initialX : 0 , initialY : 0 } ) ;
28
+ const [ activeInterval , setActiveInterval ] = useState ( 5000 ) ;
28
29
29
30
const carouselInnerRef = useRef < HTMLElement > ( null ) ;
30
31
const carouselReference = carouselRef ? carouselRef : carouselInnerRef ;
@@ -34,18 +35,19 @@ const MDBCarousel: React.FC<CarouselProps> = ({
34
35
const setPrev = useCallback ( ( ) => {
35
36
const prevIndex = activeItem === 0 ? imagesCount : activeItem - 1 ;
36
37
38
+ setClicked ( 'prev' ) ;
37
39
setActiveItem ( prevIndex ) ;
38
40
} , [ activeItem , imagesCount ] ) ;
39
41
40
42
const setNext = useCallback ( ( ) => {
41
43
const nextIndex = activeItem === imagesCount ? 0 : activeItem + 1 ;
42
44
43
45
setActiveItem ( nextIndex ) ;
46
+ setClicked ( 'next' ) ;
44
47
} , [ activeItem , imagesCount ] ) ;
45
48
46
49
const handleKeydown = useCallback (
47
50
( event : KeyboardEvent ) => {
48
- console . log ( event . key ) ;
49
51
switch ( event . key ) {
50
52
case 'ArrowLeft' :
51
53
event . preventDefault ( ) ;
@@ -70,27 +72,30 @@ const MDBCarousel: React.FC<CarouselProps> = ({
70
72
71
73
setTimeout ( ( ) => {
72
74
setIsChanging ( false ) ;
73
- } , 700 ) ;
75
+ } , 900 ) ;
74
76
} else if ( ! isChanging && isNext ) {
75
77
setNext ( ) ;
76
78
setIsChanging ( true ) ;
77
79
78
80
setTimeout ( ( ) => {
79
81
setIsChanging ( false ) ;
80
- } , 700 ) ;
82
+ } , 900 ) ;
81
83
} else {
82
84
return ;
83
85
}
84
86
} ;
85
87
86
- const handleIndicatorClick = ( i : number ) => {
87
- if ( ! isChanging ) {
88
- setActiveItem ( i ) ;
89
- setIsChanging ( true ) ;
88
+ const handleIndicatorClick = ( i : number , e : any ) => {
89
+ if ( e . target ) {
90
+ if ( ! isChanging && ! e . target . classList . contains ( 'active' ) ) {
91
+ setClicked ( 'indicator' ) ;
92
+ setActiveItem ( i ) ;
93
+ setIsChanging ( true ) ;
90
94
91
- setTimeout ( ( ) => {
92
- setIsChanging ( false ) ;
93
- } , 700 ) ;
95
+ setTimeout ( ( ) => {
96
+ setIsChanging ( false ) ;
97
+ } , 700 ) ;
98
+ }
94
99
}
95
100
} ;
96
101
@@ -105,14 +110,14 @@ const MDBCarousel: React.FC<CarouselProps> = ({
105
110
} , [ handleKeydown , keyboard ] ) ;
106
111
107
112
useEffect ( ( ) => {
108
- if ( interval && startInterval ) {
109
- const cycleInterval = setInterval ( setNext , interval ) ;
113
+ if ( activeInterval && startInterval ) {
114
+ const cycleInterval = setInterval ( setNext , activeInterval ) ;
110
115
111
116
return ( ) => {
112
117
clearInterval ( cycleInterval ) ;
113
118
} ;
114
119
}
115
- } , [ interval , setNext , startInterval ] ) ;
120
+ } , [ activeInterval , setNext , startInterval ] ) ;
116
121
117
122
useEffect ( ( ) => {
118
123
const carouselImgList = carouselReference . current . querySelectorAll ( '.carousel-item-react img' ) ;
@@ -159,6 +164,8 @@ const MDBCarousel: React.FC<CarouselProps> = ({
159
164
fade : fade ? true : false ,
160
165
prev : prevState ,
161
166
setPrev : setPrevState ,
167
+ clicked : clicked ,
168
+ setActiveInterval : setActiveInterval ,
162
169
} }
163
170
>
164
171
< Tag
@@ -174,7 +181,11 @@ const MDBCarousel: React.FC<CarouselProps> = ({
174
181
{ showIndicators && (
175
182
< ol className = 'carousel-indicators' >
176
183
{ Array . from ( Array ( imagesCount + 1 ) ) . map ( ( item , i ) => (
177
- < MDBCarouselIndicatorItem key = { i } active = { activeItem === i } onClick = { ( ) => handleIndicatorClick ( i ) } />
184
+ < MDBCarouselIndicatorItem
185
+ key = { i }
186
+ active = { activeItem === i }
187
+ onClick = { ( e : MouseEvent ) => handleIndicatorClick ( i , e ) }
188
+ />
178
189
) ) }
179
190
</ ol >
180
191
) }
@@ -189,5 +200,5 @@ const MDBCarousel: React.FC<CarouselProps> = ({
189
200
</ CarouselContext . Provider >
190
201
) ;
191
202
} ;
192
- MDBCarousel . defaultProps = { tag : 'div' , interval : 5000 , fade : false , pause : true , touch : true , keyboard : false } ;
203
+ MDBCarousel . defaultProps = { tag : 'div' , fade : false , pause : true , touch : true , keyboard : false } ;
193
204
export default MDBCarousel ;
0 commit comments