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

Track events without onClick handler #1184

Merged
merged 2 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/mighty-dragons-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sumup/circuit-ui': major
---

Fixed the `useClickEvent` hook to track events even if no custom `onClick` handler is defined.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const Label = styled(Body)(labelStyles);
export function PrimaryLink({
icon: Icon,
label,
onClick = () => {}, // Can't be undefined in order to trigger useClickEvent
onClick,
isActive,
isOpen,
isExternal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const labelStyles = (theme: Theme) => css`

function SecondaryLink({
label,
onClick = () => {}, // Can't be undefined in order to trigger useClickEvent
onClick,
tracking,
badge,
...props
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export interface UtilityLinkProps
function UtilityLink({
icon: Icon,
label,
onClick = () => {}, // Can't be undefined in order to trigger useClickEvent
onClick,
tracking,
...props
}: UtilityLinkProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('useClickEvent', () => {
// useClickTrigger
Collector.useClickTrigger = jest.fn(() => dispatch);

const onClick = jest.fn();
const onClick = undefined;
const { result } = renderHook(() =>
useClickEvent(onClick, tracking, defaultComponentName),
);
Expand Down
9 changes: 4 additions & 5 deletions packages/circuit-ui/hooks/useClickEvent/useClickEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ export function useClickEvent<Event>(
const { label, component = defaultComponentName, customParameters } =
tracking || {};

/**
* FIXME tracking should also work if onClick is falsy, e.g. if it's a link. This will be a breaking change.
*/
return onClick && label
return label
? (event: Event): void => {
dispatch({ label, component, customParameters });
onClick(event);
if (onClick) {
onClick(event);
}
}
: onClick;
}