@@ -35,6 +35,13 @@ const Gallery = ({
3535
3636 const getCurrentIndex = ( index : number ) => setCurrentIndex ( index - 1 ) ;
3737
38+ const hasImage = React . Children . toArray ( children ) . some ( child => {
39+ return React . isValidElement ( child ) && child . type === 'img' ;
40+ } ) ;
41+ if ( ! hasImage ) {
42+ console . error ( 'Only img components allowed as children.' ) ;
43+ }
44+
3845 React . useEffect ( function componentDidMount ( ) {
3946 if ( React . Children . count ( children ) > imagesDisplayed )
4047 setImageLeft ( React . Children . count ( children ) - imagesDisplayed ) ;
@@ -62,7 +69,9 @@ const Gallery = ({
6269 imageLeft = { imageLeft }
6370 onClick = { ( ) => handleClick ( index ) }
6471 >
65- < img src = { data . props . src } alt = { index . toString ( 10 ) } />
72+ { React . cloneElement ( data , {
73+ alt : index . toString ( 10 ) ,
74+ } ) }
6675 </ GalleryItem >
6776 ) ) }
6877 </ GalleryItemWrapper >
@@ -84,11 +93,9 @@ const Gallery = ({
8493 ( data : React . ReactElement < React . HTMLProps < 'img' > > , index ) => (
8594 < Slider . Item key = { `${ data . props . src } _${ index } ` } >
8695 < GalleryImageWrapper role = "banner" tabIndex = { 0 } >
87- < img
88- src = { data . props . src }
89- key = { `${ data . props . src } _${ index } ` }
90- alt = { index . toString ( 10 ) }
91- />
96+ { React . cloneElement ( data , {
97+ alt : index . toString ( 10 ) ,
98+ } ) }
9299 </ GalleryImageWrapper >
93100 </ Slider . Item >
94101 )
@@ -97,17 +104,21 @@ const Gallery = ({
97104 < GalleryThumbnailWrapper >
98105 { React . Children . map (
99106 children ,
100- ( data : React . ReactElement < React . HTMLProps < 'img' > > , index ) => (
107+ (
108+ data : React . ReactElement <
109+ React . HTMLProps < 'img' > & { 'data-testid' : string }
110+ > ,
111+ index
112+ ) => (
101113 < div
102114 key = { `${ data . props . src } _${ index } ` }
103115 onClick = { ( ) => handleClickThumbs ( index ) }
104116 >
105- < img
106- data-testid = "gallery_thumbnail"
107- src = { data . props . src }
108- alt = { index . toString ( 10 ) }
109- className = { index === currentIndex ? 'active' : null }
110- />
117+ { React . cloneElement ( data , {
118+ className : index === currentIndex ? 'active' : null ,
119+ 'data-testid' : 'gallery_thumbnail' ,
120+ alt : index . toString ( 10 ) ,
121+ } ) }
111122 </ div >
112123 )
113124 ) }
0 commit comments