-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5467 from solidusio/elia/admin/search-panel
[Admin] Extract a `ui/search_panel` component from `orders/cart`
- Loading branch information
Showing
16 changed files
with
351 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 13 additions & 111 deletions
124
admin/app/components/solidus_admin/orders/cart/component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,37 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import { useClickOutside, useDebounce } from "stimulus-use" | ||
|
||
const QUERY_KEY = "q[name_or_variants_including_master_sku_cont]" | ||
import { useDebounce } from "stimulus-use" | ||
|
||
export default class extends Controller { | ||
static targets = ["result", "results", "searchField"] | ||
static values = { | ||
results: String, | ||
productsUrl: String, | ||
loadingText: String, | ||
initialText: String, | ||
emptyText: String, | ||
} | ||
static debounces = [ | ||
{ | ||
name: "requestSubmitForLineItems", | ||
wait: 500, | ||
}, | ||
"search", | ||
] | ||
|
||
get query() { | ||
return this.searchFieldTarget.value | ||
} | ||
|
||
get selectedResult() { | ||
// Keep the index within boundaries | ||
if (this.selectedIndex < 0) this.selectedIndex = 0 | ||
if (this.selectedIndex >= this.resultTargets.length) this.selectedIndex = this.resultTargets.length - 1 | ||
|
||
return this.resultTargets[this.selectedIndex] | ||
} | ||
static values = { productsUrl: String } | ||
static debounces = [{ name: "submitLineItems", wait: 500 }] | ||
|
||
connect() { | ||
useClickOutside(this) | ||
useDebounce(this) | ||
|
||
this.selectedIndex = 0 | ||
this.lineItemsToBeSubmitted = [] | ||
|
||
if (this.query) { | ||
this.showResults() | ||
this.search() | ||
} | ||
} | ||
|
||
selectResult(event) { | ||
switch (event.key) { | ||
case "Enter": | ||
event.preventDefault() | ||
this.selectedResult?.click() | ||
break | ||
case "ArrowUp": | ||
event.preventDefault() | ||
this.selectedIndex -= 1 | ||
this.render() | ||
break | ||
case "ArrowDown": | ||
event.preventDefault() | ||
this.selectedIndex += 1 | ||
this.render() | ||
break | ||
} | ||
} | ||
|
||
clickOutside() { | ||
this.openResults = false | ||
this.render() | ||
} | ||
|
||
async search() { | ||
const query = this.query | ||
|
||
if (query) { | ||
this.resultsValue = this.loadingTextValue | ||
this.render() | ||
|
||
this.resultsValue = (await (await fetch(`${this.productsUrlValue}?${QUERY_KEY}=${query}`)).text()) || this.emptyTextValue | ||
this.render() | ||
} else { | ||
this.resultsValue = this.initialTextValue | ||
this.render() | ||
} | ||
} | ||
|
||
showResults() { | ||
this.openResults = true | ||
this.render() | ||
async search({ detail: { query, controller } }) { | ||
controller.resultsValue = await ( | ||
await fetch(`${this.productsUrlValue}?q[name_or_variants_including_master_sku_cont]=${query}`) | ||
).text() | ||
} | ||
|
||
updateLineItem(event) { | ||
if (!this.lineItemsToBeSubmitted.includes(event.currentTarget)) { | ||
this.lineItemsToBeSubmitted.push(event.currentTarget) | ||
} | ||
|
||
this.requestSubmitForLineItems() | ||
this.submitLineItems() | ||
} | ||
|
||
// This is a workaround to permit using debounce when needing to pass a parameter | ||
requestSubmitForLineItems() { | ||
this.lineItemsToBeSubmitted.forEach((lineItem) => { | ||
lineItem.form.requestSubmit() | ||
}) | ||
submitLineItems() { | ||
this.lineItemsToBeSubmitted.forEach((lineItem) => lineItem.form.requestSubmit()) | ||
this.lineItemsToBeSubmitted = [] | ||
} | ||
|
||
render() { | ||
let resultsHtml = this.resultsValue | ||
|
||
if (this.renderedHtml !== resultsHtml) { | ||
this.renderedHtml = resultsHtml | ||
this.resultsTarget.innerHTML = resultsHtml | ||
} | ||
|
||
if (this.openResults && resultsHtml && this.query) { | ||
if (!this.resultsTarget.parentNode.open) this.selectedIndex = 0 | ||
|
||
for (const result of this.resultTargets) { | ||
if (result === this.selectedResult) { | ||
if (!result.hasAttribute("aria-selected") && result.scrollIntoViewIfNeeded) { | ||
// This is a non-standard method, but it's supported by all major browsers | ||
result.scrollIntoViewIfNeeded() | ||
} | ||
result.setAttribute("aria-selected", true) | ||
} else { | ||
result.removeAttribute("aria-selected") | ||
} | ||
} | ||
this.resultsTarget.parentNode.open = true | ||
} else { | ||
this.resultsTarget.parentNode.open = false | ||
} | ||
selectResult(event) { | ||
const form = event.detail.resultTarget.querySelector("form") | ||
form.submit() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
# Add your component translations here. | ||
# Use the translation in the example in your template with `t(".hello")`. | ||
en: | ||
title: "Cart" | ||
search_placeholder: "Find a variant by name or SKU" | ||
loading: "Loading..." | ||
initial: "Type to search" | ||
empty: "No results" |
Oops, something went wrong.