@@ -10,7 +10,6 @@ const MDBCarousel: React.FC<CarouselProps> = ({
1010 dark,
1111 children,
1212 carouselRef,
13- interval,
1413 keyboard,
1514 pause,
1615 touch,
@@ -23,8 +22,10 @@ const MDBCarousel: React.FC<CarouselProps> = ({
2322 const [ imagesCount , setImagesCount ] = useState ( 0 ) ;
2423 const [ activeItem , setActiveItem ] = useState ( 0 ) ;
2524 const [ prevState , setPrevState ] = useState ( 0 ) ;
25+ const [ clicked , setClicked ] = useState ( '' ) ;
2626 const [ startInterval , setStartInterval ] = useState ( true ) ;
2727 const [ clientTouch , setClientTouch ] = useState ( { initialX : 0 , initialY : 0 } ) ;
28+ const [ activeInterval , setActiveInterval ] = useState ( 5000 ) ;
2829
2930 const carouselInnerRef = useRef < HTMLElement > ( null ) ;
3031 const carouselReference = carouselRef ? carouselRef : carouselInnerRef ;
@@ -34,18 +35,19 @@ const MDBCarousel: React.FC<CarouselProps> = ({
3435 const setPrev = useCallback ( ( ) => {
3536 const prevIndex = activeItem === 0 ? imagesCount : activeItem - 1 ;
3637
38+ setClicked ( 'prev' ) ;
3739 setActiveItem ( prevIndex ) ;
3840 } , [ activeItem , imagesCount ] ) ;
3941
4042 const setNext = useCallback ( ( ) => {
4143 const nextIndex = activeItem === imagesCount ? 0 : activeItem + 1 ;
4244
4345 setActiveItem ( nextIndex ) ;
46+ setClicked ( 'next' ) ;
4447 } , [ activeItem , imagesCount ] ) ;
4548
4649 const handleKeydown = useCallback (
4750 ( event : KeyboardEvent ) => {
48- console . log ( event . key ) ;
4951 switch ( event . key ) {
5052 case 'ArrowLeft' :
5153 event . preventDefault ( ) ;
@@ -70,27 +72,30 @@ const MDBCarousel: React.FC<CarouselProps> = ({
7072
7173 setTimeout ( ( ) => {
7274 setIsChanging ( false ) ;
73- } , 700 ) ;
75+ } , 900 ) ;
7476 } else if ( ! isChanging && isNext ) {
7577 setNext ( ) ;
7678 setIsChanging ( true ) ;
7779
7880 setTimeout ( ( ) => {
7981 setIsChanging ( false ) ;
80- } , 700 ) ;
82+ } , 900 ) ;
8183 } else {
8284 return ;
8385 }
8486 } ;
8587
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 ) ;
9094
91- setTimeout ( ( ) => {
92- setIsChanging ( false ) ;
93- } , 700 ) ;
95+ setTimeout ( ( ) => {
96+ setIsChanging ( false ) ;
97+ } , 700 ) ;
98+ }
9499 }
95100 } ;
96101
@@ -105,14 +110,14 @@ const MDBCarousel: React.FC<CarouselProps> = ({
105110 } , [ handleKeydown , keyboard ] ) ;
106111
107112 useEffect ( ( ) => {
108- if ( interval && startInterval ) {
109- const cycleInterval = setInterval ( setNext , interval ) ;
113+ if ( activeInterval && startInterval ) {
114+ const cycleInterval = setInterval ( setNext , activeInterval ) ;
110115
111116 return ( ) => {
112117 clearInterval ( cycleInterval ) ;
113118 } ;
114119 }
115- } , [ interval , setNext , startInterval ] ) ;
120+ } , [ activeInterval , setNext , startInterval ] ) ;
116121
117122 useEffect ( ( ) => {
118123 const carouselImgList = carouselReference . current . querySelectorAll ( '.carousel-item-react img' ) ;
@@ -159,6 +164,8 @@ const MDBCarousel: React.FC<CarouselProps> = ({
159164 fade : fade ? true : false ,
160165 prev : prevState ,
161166 setPrev : setPrevState ,
167+ clicked : clicked ,
168+ setActiveInterval : setActiveInterval ,
162169 } }
163170 >
164171 < Tag
@@ -174,7 +181,11 @@ const MDBCarousel: React.FC<CarouselProps> = ({
174181 { showIndicators && (
175182 < ol className = 'carousel-indicators' >
176183 { 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+ />
178189 ) ) }
179190 </ ol >
180191 ) }
@@ -189,5 +200,5 @@ const MDBCarousel: React.FC<CarouselProps> = ({
189200 </ CarouselContext . Provider >
190201 ) ;
191202} ;
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 } ;
193204export default MDBCarousel ;
0 commit comments