Skip to content

Commit

Permalink
fix(atomic): fix clicks for recommendation lists in grid display mode (
Browse files Browse the repository at this point in the history
…#4623)

This PR ensures that clicks on grid display mode result cards open the
result/product for `atomic-recs-list`, `atomic-ipx-recs-list` and
`atomic-commerce-recommendation-list`, as with other types of result
lists.

https://coveord.atlassian.net/browse/KIT-3705
  • Loading branch information
fpbrault authored Nov 4, 2024
1 parent 573b7f3 commit 38970c3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1893,14 +1893,14 @@ export declare interface AtomicRecsList extends Components.AtomicRecsList {}


@ProxyCmp({
inputs: ['classes', 'content', 'density', 'display', 'imageSize', 'result', 'stopPropagation']
inputs: ['classes', 'content', 'density', 'display', 'imageSize', 'linkContent', 'result', 'stopPropagation']
})
@Component({
selector: 'atomic-recs-result',
changeDetection: ChangeDetectionStrategy.OnPush,
template: '<ng-content></ng-content>',
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['classes', 'content', 'density', 'display', 'imageSize', 'result', 'stopPropagation'],
inputs: ['classes', 'content', 'density', 'display', 'imageSize', 'linkContent', 'result', 'stopPropagation'],
})
export class AtomicRecsResult {
protected el: HTMLElement;
Expand Down
10 changes: 10 additions & 0 deletions packages/atomic/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,11 @@ export namespace Components {
* The InteractiveResult item.
*/
"interactiveResult": RecsInteractiveResult;
/**
* The result link to use when the result is clicked in a grid layout.
* @default - An `atomic-result-link` without any customization.
*/
"linkContent": ParentNode;
"loadingFlag"?: string;
/**
* Internal function used by atomic-recs-list in advanced setups, which lets you bypass the standard HTML template system. Particularly useful for Atomic React
Expand Down Expand Up @@ -8646,6 +8651,11 @@ declare namespace LocalJSX {
* The InteractiveResult item.
*/
"interactiveResult": RecsInteractiveResult;
/**
* The result link to use when the result is clicked in a grid layout.
* @default - An `atomic-result-link` without any customization.
*/
"linkContent"?: ParentNode;
"loadingFlag"?: string;
/**
* Internal function used by atomic-recs-list in advanced setups, which lets you bypass the standard HTML template system. Particularly useful for Atomic React
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,7 @@ export class AtomicCommerceRecommendationList
this.imageSize
),
content: this.productTemplateProvider.getTemplateContent(product),
linkContent:
this.display === 'grid'
? this.productTemplateProvider.getLinkTemplateContent(product)
: this.productTemplateProvider.getEmptyLinkTemplateContent(),
linkContent: this.productTemplateProvider.getLinkTemplateContent(product),
store: this.bindings.store,
density: this.density,
display: this.display,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ export class AtomicIPXRecsList implements InitializableComponent<RecsBindings> {
this.imageSize
),
content: this.itemTemplateProvider.getTemplateContent(recommendation),
linkContent:
this.itemTemplateProvider.getLinkTemplateContent(recommendation),
store: this.bindings.store,
density: this.density,
display: this.display,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ export class AtomicRecsList implements InitializableComponent<RecsBindings> {
this.imageSize
),
content: this.itemTemplateProvider.getTemplateContent(recommendation),
linkContent:
this.itemTemplateProvider.getLinkTemplateContent(recommendation),
store: this.bindings.store,
density: this.density,
display: this.display,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Component, h, Prop, Element, Listen, Host} from '@stencil/core';
import {RecsInteractiveResult, RecsResult} from '..';
import {parentNodeToString} from '../../../utils/dom-utils';
import {applyFocusVisiblePolyfill} from '../../../utils/initialization-utils';
import {
InteractiveItemContextEvent,
Expand Down Expand Up @@ -29,6 +30,7 @@ import {AtomicRecsStore} from '../atomic-recs-interface/store';
export class AtomicRecsResult {
private layout!: ItemLayout;
private resultRootRef?: HTMLElement;
private linkContainerRef?: HTMLElement;
private executedRenderingFunctionOnce = false;
@Element() host!: HTMLElement;

Expand All @@ -37,6 +39,13 @@ export class AtomicRecsResult {
*/
@Prop() stopPropagation?: boolean;

/**
* The result link to use when the result is clicked in a grid layout.
*
* @default - An `atomic-result-link` without any customization.
*/
@Prop() linkContent: ParentNode = new DocumentFragment();

/**
* The result item.
*/
Expand Down Expand Up @@ -94,6 +103,18 @@ export class AtomicRecsResult {
*/
@Prop() renderingFunction: ItemRenderingFunction;

@Listen('click')
public handleClick(event: MouseEvent) {
if (this.stopPropagation) {
event.stopPropagation();
}
this.host
.shadowRoot!.querySelector<HTMLAnchorElement>(
'.link-container > atomic-result-link a:not([slot])'
)
?.click();
}

@Listen('atomic/resolveResult')
public resolveResult(event: ItemContextEvent<RecsResult>) {
event.preventDefault();
Expand Down Expand Up @@ -133,11 +154,12 @@ export class AtomicRecsResult {
}

private getContentHTML() {
return Array.from(this.content!.children)
.map((child) => child.outerHTML)
.join('');
return parentNodeToString(this.content!);
}

private getLinkHTML() {
return parentNodeToString(this.linkContent);
}
private get isCustomRenderFunctionMode() {
return this.renderingFunction !== undefined;
}
Expand All @@ -158,6 +180,10 @@ export class AtomicRecsResult {
class="result-root"
ref={(ref) => (this.resultRootRef = ref)}
></div>
<div
class="link-container"
ref={(ref) => (this.linkContainerRef = ref)}
></div>
</Host>
);
}
Expand All @@ -171,6 +197,7 @@ export class AtomicRecsResult {
.join(' ')}`}
innerHTML={this.getContentHTML()}
></div>
<div class="link-container" innerHTML={this.getLinkHTML()}></div>
</Host>
);
}
Expand All @@ -186,7 +213,8 @@ export class AtomicRecsResult {
if (this.shouldExecuteRenderFunction()) {
const customRenderOutputAsString = this.renderingFunction!(
this.result,
this.resultRootRef!
this.resultRootRef!,
this.linkContainerRef!
);

this.resultRootRef!.className += ` ${this.layout
Expand Down
1 change: 0 additions & 1 deletion packages/atomic/src/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@
</atomic-result-template>

<atomic-result-template>
<template slot="link"> </template>
<template>
<style>
.field {
Expand Down

0 comments on commit 38970c3

Please sign in to comment.