Skip to content

Commit

Permalink
fix(internal): bail out of default click if element has a bindEvent p…
Browse files Browse the repository at this point in the history
…arent
  • Loading branch information
Haroenv committed Mar 14, 2023
1 parent 16c096f commit 35028d0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
20 changes: 18 additions & 2 deletions packages/instantsearch.js/src/components/Hits/Hits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import { cx } from '@algolia/ui-components-shared';
import { h } from 'preact';

import { createInsightsEventHandler } from '../../lib/insights/listener';
import {
createInsightsEventHandler,
findInsightsTarget,
} from '../../lib/insights/listener';
import { warning } from '../../lib/utils';
import Template from '../Template/Template';

Expand Down Expand Up @@ -64,7 +67,20 @@ export default function Hits({
rootTagName="li"
rootProps={{
className: cssClasses.item,
onClick: () => {
onClick: (event: MouseEvent) => {
const targetWithEvent = findInsightsTarget(
event.target as HTMLElement | null,
event.currentTarget as HTMLElement | null,
(element) =>
element.hasAttribute('data-insights-event') ||
(element.hasAttribute('data-insights-method') &&
element.hasAttribute('data-insights-payload'))
);

if (targetWithEvent) {
return;
}

sendEvent('click:internal', hit, 'Hit Clicked');
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import { cx } from '@algolia/ui-components-shared';
import { h } from 'preact';

import { createInsightsEventHandler } from '../../lib/insights/listener';
import {
createInsightsEventHandler,
findInsightsTarget,
} from '../../lib/insights/listener';
import { warning } from '../../lib/utils';
import Template from '../Template/Template';

Expand Down Expand Up @@ -96,7 +99,20 @@ const InfiniteHits = ({
rootTagName="li"
rootProps={{
className: cssClasses.item,
onClick: () => {
onClick: (event: MouseEvent) => {
const targetWithEvent = findInsightsTarget(
event.target as HTMLElement | null,
event.currentTarget as HTMLElement | null,
(element) =>
element.hasAttribute('data-insights-event') ||
(element.hasAttribute('data-insights-method') &&
element.hasAttribute('data-insights-payload'))
);

if (targetWithEvent) {
return;
}

sendEvent('click:internal', hit, 'Hit Clicked');
},
}}
Expand Down
2 changes: 1 addition & 1 deletion packages/instantsearch.js/src/lib/insights/listener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const createInsightsEventHandler =
}
};

function findInsightsTarget(
export function findInsightsTarget(
startElement: HTMLElement | null,
endElement: HTMLElement | null,
validator: (element: HTMLElement) => boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('hits', () => {
);
});

test.only('sends a default `click` event when clicking on a hit', async () => {
test('sends a default `click` event when clicking on a hit', async () => {
const { search } = createInstantSearch();
const { insights, onEvent } = createInsightsMiddlewareWithOnEvent();

Expand Down

0 comments on commit 35028d0

Please sign in to comment.