From 3c4e13cff161cbc2eb54ddaffedb7ea104e0b0c3 Mon Sep 17 00:00:00 2001 From: Martin Zanoni Date: Mon, 15 Jan 2024 13:26:41 +0100 Subject: [PATCH] fix: issue with keyboard navigation when not using Url-datakey --- .../product-search-overlay-results.ts | 18 ++++----- .../src/search/product-search-overlay.ts | 37 +++++++++++++------ 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/packages/web-components/src/search/components/product-search-overlay-results.ts b/packages/web-components/src/search/components/product-search-overlay-results.ts index 585855dc..53914ddc 100644 --- a/packages/web-components/src/search/components/product-search-overlay-results.ts +++ b/packages/web-components/src/search/components/product-search-overlay-results.ts @@ -6,7 +6,7 @@ import { SearchResult } from '../product-search-overlay'; export class ProductSearchOverlayResults extends LitElement { @property() - setSearchTerm = (term: string) => {}; + setSearchTerm = (term: string) => { }; @property() noResultsMessage: string | null = null; @@ -16,9 +16,9 @@ export class ProductSearchOverlayResults extends LitElement { @property({ type: Number }) selectedIndex = -1; - + @property() - setResultOverlayHovered = (hovered: boolean) => {}; + setResultOverlayHovered = (hovered: boolean) => { }; connectedCallback(): void { super.connectedCallback(); @@ -35,7 +35,7 @@ export class ProductSearchOverlayResults extends LitElement { ` : html`
${this.results.map((result, index) => { - return html` + return html`
${result.searchTermPrediction ? html` @@ -45,15 +45,15 @@ export class ProductSearchOverlayResults extends LitElement {
- ` : + ` : html`
`}
`; - }, - )} + }, + )} `} `; @@ -90,7 +90,7 @@ export class ProductSearchOverlayResults extends LitElement { } .rw-products-container { - display :flex; + display: flex; flex-direction: column; } @@ -102,7 +102,7 @@ export class ProductSearchOverlayResults extends LitElement { } .rw-selected-result[selected] { - background-color: whitesmoke; + background-color: var(--relewise-hover-color, whitesmoke); } .rw-prediction-item-container:hover { diff --git a/packages/web-components/src/search/product-search-overlay.ts b/packages/web-components/src/search/product-search-overlay.ts index 90e2fb8c..86da186b 100644 --- a/packages/web-components/src/search/product-search-overlay.ts +++ b/packages/web-components/src/search/product-search-overlay.ts @@ -12,7 +12,7 @@ export class SearchResult { } export class ProductSearchOverlay extends LitElement { - + @property({ attribute: 'displayed-at-location' }) displayedAtLocation?: string = undefined; @@ -27,8 +27,8 @@ export class ProductSearchOverlay extends LitElement { @state() term: string = ''; - - @state() + + @state() selectedIndex = -1; @state() @@ -41,7 +41,7 @@ export class ProductSearchOverlay extends LitElement { hasCompletedSearchRequest: boolean = false; private debounceTimeoutHandlerId: ReturnType | null = null; - + async connectedCallback() { if (!this.displayedAtLocation) { console.error('No displayedAtLocation defined!'); @@ -98,8 +98,23 @@ export class ProductSearchOverlay extends LitElement { this.setSearchTerm(result.searchTermPrediction.term ?? ''); } - if (result.product && result.product.data && 'Url' in result.product.data) { - window.location.href = result.product.data['Url'].value ?? ''; + if (result.product) { + const selectedProduct = this.shadowRoot! + .querySelector('relewise-product-search-overlay-results') + ?.shadowRoot + ?.querySelector('.rw-selected-result[selected]'); + + if (selectedProduct) { + const productLink = selectedProduct + .querySelector('relewise-product-search-overlay-product') + ?.shadowRoot + ?.querySelector('a') + ?.getAttribute('href'); + + if (productLink) { + window.location.href = productLink; + } + } } } @@ -130,7 +145,7 @@ export class ProductSearchOverlay extends LitElement { .addEntityType('Product') .build()); } - + const response = await searcher.batch(requestBuilder.build()); if (response && response.responses) { @@ -158,7 +173,7 @@ export class ProductSearchOverlay extends LitElement { return html` this.setSearchTerm(term)} + .setSearchTerm=${(term: string) => this.setSearchTerm(term)} .setSearchBarInFocus=${(inFocus: boolean) => this.searchBarInFocus = inFocus} .placeholder=${localization?.searchBar?.placeholder ?? 'Search'} .handleKeyEvent=${(e: KeyboardEvent) => this.handleKeyDown(e)} @@ -166,13 +181,13 @@ export class ProductSearchOverlay extends LitElement { ${(this.searchBarInFocus && this.hasCompletedSearchRequest && this.term) || - this.resultBoxIsHovered ? + this.resultBoxIsHovered ? html` this.setSearchTerm(term)} + .setSearchTerm=${(term: string) => this.setSearchTerm(term)} .noResultsMessage=${localization?.searchResults?.noResults ?? 'No products found'} - .setResultOverlayHovered=${(hovered: boolean) => this.resultBoxIsHovered = hovered }> + .setResultOverlayHovered=${(hovered: boolean) => this.resultBoxIsHovered = hovered}> ` : nothing } `;