Skip to content

Commit 34b033f

Browse files
authored
Carousel item href (#971)
* Add href attribute to carousel items * Add test for carousel item href
1 parent 696f5fb commit 34b033f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/components/carousel/Carousel.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ const Carousel = props => {
2828
? item.imgClassName
2929
: 'd-block w-100';
3030

31+
const additionalProps = item.href ? {href: item.href} : {};
32+
3133
return (
32-
<RBCarousel.Item key={item.key}>
34+
<RBCarousel.Item
35+
key={item.key}
36+
as={item.href ? 'a' : 'div'}
37+
{...additionalProps}
38+
>
3339
<img
3440
src={item.src}
3541
className={item.img_class_name || item.imgClassName}
@@ -148,7 +154,11 @@ Carousel.propTypes = {
148154
*
149155
* The class name for the header and caption container
150156
*/
151-
captionClassName: PropTypes.string
157+
captionClassName: PropTypes.string,
158+
/**
159+
* Optional hyperlink to add to the item.
160+
*/
161+
href: PropTypes.string
152162
})
153163
).isRequired,
154164

src/components/carousel/__tests__/Carousel.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Carousel', () => {
4848

4949
test('tracks most recently clicked slide with "active_index" prop', () => {
5050
const mockSetProps = jest.fn();
51-
const {container, getByText, rerender} = render(
51+
const {container} = render(
5252
<Carousel items={slides} setProps={mockSetProps} active_index={0} />
5353
);
5454

@@ -63,4 +63,18 @@ describe('Carousel', () => {
6363
expect(mockSetProps.mock.calls).toHaveLength(1);
6464
expect(mockSetProps.mock.calls[0][0].active_index).toBe(1);
6565
});
66+
67+
test('carousel item accepts href', () => {
68+
const linkedSlides = [
69+
{key: '0', src: '', alt: 'z', href: '/test'},
70+
...slides
71+
];
72+
73+
const carousel = render(<Carousel items={linkedSlides} />);
74+
const {firstChild: carouselItem} = carousel.container.querySelector(
75+
'.carousel-inner'
76+
);
77+
expect(carouselItem).toHaveAttribute('href', '/test');
78+
expect(carouselItem.tagName.toLowerCase()).toEqual('a');
79+
});
6680
});

0 commit comments

Comments
 (0)