Skip to content

Commit 8d6884d

Browse files
authored
Merge pull request #684 from glints-dev/feature/gm-2978
Gallery: Render children directly instead of creating new image tags
2 parents 68b80ac + a30540b commit 8d6884d

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

src/Display/Gallery/Gallery.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const GalleryItemLessThanDefaultImagesDisplayed = () => (
3636
</Gallery>
3737
);
3838

39+
const GalleryItemWithInvalidChildren = () => (
40+
<Gallery>
41+
<span>Content</span>
42+
</Gallery>
43+
);
44+
3945
describe('<Gallery /> focus', () => {
4046
it('Slider should be focused when Modal is open', () => {
4147
render(<Component />);
@@ -68,6 +74,14 @@ describe('<Gallery /> rendering', () => {
6874
);
6975
expect(asFragment()).toMatchSnapshot();
7076
});
77+
78+
it('should return console error when item is invalid children', () => {
79+
const error = jest.spyOn(console, 'error').mockImplementation();
80+
render(<GalleryItemWithInvalidChildren />);
81+
82+
expect(error).toBeCalledWith('Only img components allowed as children.');
83+
error.mockRestore();
84+
});
7185
});
7286

7387
describe('<Gallery /> open and close Slider modal', () => {

src/Display/Gallery/Gallery.tsx

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)