Skip to content

Commit 6f59d45

Browse files
authored
Merge pull request #577 from rebeccaalpert/sources-title-click
fix(SourcesCard): Add onClick event for title
2 parents 5823567 + 5b9f365 commit 6f59d45

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

packages/module/src/SourcesCard/SourcesCard.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,18 @@ describe('SourcesCard', () => {
242242
);
243243
expect(screen.getByRole('region')).toHaveAttribute('class', 'pf-v6-c-expandable-section__content');
244244
});
245+
246+
it('should call onClick appropriately', async () => {
247+
const spy = jest.fn();
248+
render(<SourcesCard sources={[{ title: 'How to make an apple pie', link: '', onClick: spy }]} />);
249+
await userEvent.click(screen.getByRole('link', { name: /How to make an apple pie/i }));
250+
expect(spy).toHaveBeenCalled();
251+
});
252+
253+
it('should apply titleProps appropriately', () => {
254+
render(
255+
<SourcesCard sources={[{ title: 'How to make an apple pie', link: '', titleProps: { className: 'test' } }]} />
256+
);
257+
expect(screen.getByRole('link', { name: /How to make an apple pie/i })).toHaveClass('test');
258+
});
245259
});

packages/module/src/SourcesCard/SourcesCard.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useState } from 'react';
66
// Import PatternFly components
77
import {
88
Button,
9+
ButtonProps,
910
ButtonVariant,
1011
Card,
1112
CardBody,
@@ -31,11 +32,20 @@ export interface SourcesCardProps extends CardProps {
3132
paginationAriaLabel?: string;
3233
/** Content rendered inside the paginated card */
3334
sources: {
35+
/** Title of sources card */
3436
title?: string;
37+
/** Link to source */
3538
link: string;
39+
/** Body of sources card */
3640
body?: React.ReactNode | string;
41+
/** Whether link is external */
3742
isExternal?: boolean;
43+
/** Whether sources card is expandable */
3844
hasShowMore?: boolean;
45+
/** onClick event applied to the title of the Sources card */
46+
onClick?: React.MouseEventHandler<HTMLButtonElement>;
47+
/** Any additional props applied to the title of the Sources card */
48+
titleProps?: ButtonProps;
3949
}[];
4050
/** Label for the English word "source" */
4151
sourceWord?: string;
@@ -107,6 +117,8 @@ const SourcesCard: FunctionComponent<SourcesCardProps> = ({
107117
isInline
108118
rel={sources[page - 1].isExternal ? 'noreferrer' : undefined}
109119
target={sources[page - 1].isExternal ? '_blank' : undefined}
120+
onClick={sources[page - 1].onClick ?? undefined}
121+
{...sources[page - 1].titleProps}
110122
>
111123
{renderTitle(sources[page - 1].title)}
112124
</Button>

0 commit comments

Comments
 (0)