Skip to content

Commit

Permalink
Carousel item href (#971)
Browse files Browse the repository at this point in the history
* Add href attribute to carousel items

* Add test for carousel item href
  • Loading branch information
tcbegley committed Sep 4, 2023
1 parent 696f5fb commit 34b033f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/components/carousel/Carousel.js
Expand Up @@ -28,8 +28,14 @@ const Carousel = props => {
? item.imgClassName
: 'd-block w-100';

const additionalProps = item.href ? {href: item.href} : {};

return (
<RBCarousel.Item key={item.key}>
<RBCarousel.Item
key={item.key}
as={item.href ? 'a' : 'div'}
{...additionalProps}
>
<img
src={item.src}
className={item.img_class_name || item.imgClassName}
Expand Down Expand Up @@ -148,7 +154,11 @@ Carousel.propTypes = {
*
* The class name for the header and caption container
*/
captionClassName: PropTypes.string
captionClassName: PropTypes.string,
/**
* Optional hyperlink to add to the item.
*/
href: PropTypes.string
})
).isRequired,

Expand Down
16 changes: 15 additions & 1 deletion src/components/carousel/__tests__/Carousel.test.js
Expand Up @@ -48,7 +48,7 @@ describe('Carousel', () => {

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

Expand All @@ -63,4 +63,18 @@ describe('Carousel', () => {
expect(mockSetProps.mock.calls).toHaveLength(1);
expect(mockSetProps.mock.calls[0][0].active_index).toBe(1);
});

test('carousel item accepts href', () => {
const linkedSlides = [
{key: '0', src: '', alt: 'z', href: '/test'},
...slides
];

const carousel = render(<Carousel items={linkedSlides} />);
const {firstChild: carouselItem} = carousel.container.querySelector(
'.carousel-inner'
);
expect(carouselItem).toHaveAttribute('href', '/test');
expect(carouselItem.tagName.toLowerCase()).toEqual('a');
});
});

0 comments on commit 34b033f

Please sign in to comment.