Skip to content

Commit 387bfc2

Browse files
mapixluowf
andauthored
support target for carousel image click (#975)
* support target for carousel image click * add tests for carousel target property --------- Co-authored-by: luowf <louwf@dp.tech>
1 parent 75b7d32 commit 387bfc2

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/components/carousel/Carousel.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ const Carousel = props => {
3232

3333
const useLink = item.href && true;
3434
const additionalProps = useLink
35-
? {href: item.href, external_link: item.external_link}
35+
? {
36+
href: item.href,
37+
external_link: item.external_link,
38+
target: item.target || '_self'
39+
}
3640
: {};
3741

3842
return (
@@ -167,6 +171,10 @@ Carousel.propTypes = {
167171
* with the external_link argument.
168172
*/
169173
href: PropTypes.string,
174+
/**
175+
* Optional target attribute for the link. Only applies if `href` is set, default `_self`.
176+
*/
177+
target: PropTypes.string,
170178
/**
171179
* If true, the browser will treat this as an external link,
172180
* forcing a page refresh at the new location. If false,

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,45 @@ describe('Carousel', () => {
7777
expect(carouselItem).toHaveAttribute('href', '/test');
7878
expect(carouselItem.tagName.toLowerCase()).toEqual('a');
7979
});
80+
81+
test('carousel item external target', () => {
82+
const linkedSlides = [
83+
{
84+
key: '0',
85+
src: '',
86+
alt: 'z',
87+
href: 'http://www.example.com',
88+
target: '_blank'
89+
},
90+
{
91+
key: '1',
92+
src: '',
93+
alt: 'z',
94+
href: '/test',
95+
target: '_self',
96+
external_link: true
97+
},
98+
{
99+
key: '2',
100+
src: '',
101+
alt: 'z',
102+
href: 'http://www.example.com'
103+
},
104+
...slides
105+
];
106+
107+
const carousel = render(<Carousel items={linkedSlides} />);
108+
const carouselItems = carousel.container.querySelectorAll('.carousel-item');
109+
const blankTargetItem = carouselItems[0];
110+
expect(blankTargetItem).toHaveAttribute('target', '_blank');
111+
expect(blankTargetItem.tagName.toLowerCase()).toEqual('a');
112+
113+
const selfTargetItem = carouselItems[1];
114+
expect(selfTargetItem).toHaveAttribute('target', '_self');
115+
expect(selfTargetItem.tagName.toLowerCase()).toEqual('a');
116+
117+
const defaultTargetItem = carouselItems[2];
118+
expect(defaultTargetItem).toHaveAttribute('target', '_self');
119+
expect(defaultTargetItem.tagName.toLowerCase()).toEqual('a');
120+
});
80121
});

0 commit comments

Comments
 (0)