Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiCard] Harden Click Event Criteria to Prevent Duplicate onClick Events #6551

Merged
merged 3 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/components/card/card.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ describe('EuiCard', () => {
component.find('button').simulate('click');
expect(handler.mock.calls.length).toEqual(1);
});

it('should only call onClick once when title is a React node', () => {
const handler = jest.fn();
const component = mount(
<EuiCard
title={<span>Hoi</span>}
description="There"
onClick={handler}
/>
);
component.find('button').simulate('click');
expect(handler.mock.calls.length).toEqual(1);
});
breehall marked this conversation as resolved.
Show resolved Hide resolved
});

test('titleElement', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/card/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const EuiCard: FunctionComponent<EuiCardProps> = ({
*/
let link: HTMLAnchorElement | HTMLButtonElement | null = null;
const outerOnClick = (e: React.MouseEvent<HTMLDivElement>) => {
if (link && link !== e.target) {
if (link && link !== e.target && !link.contains(e.target as Node)) {
link.click();
}
};
Expand Down
3 changes: 3 additions & 0 deletions upcoming_changelogs/6551.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed `EuiCard` to ensure `onClick` method only runs once when `title` contains a React node