Skip to content

Commit

Permalink
Merge pull request #989 from ae-utbm/counter
Browse files Browse the repository at this point in the history
Make code matching rank first in counter click
  • Loading branch information
klmp200 authored Jan 5, 2025
2 parents 8e339c3 + bb24516 commit 429df81
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { registerComponent } from "#core:utils/web-components";
import type { RecursivePartial, TomSettings } from "tom-select/dist/types/types";

const productParsingRegex = /^(\d+x)?(.*)/i;
const codeParsingRegex = / \((\w+)\)$/;

function parseProduct(query: string): [number, string] {
const parsed = productParsingRegex.exec(query);
Expand All @@ -21,6 +22,18 @@ export class CounterProductSelect extends AutoCompleteSelectBase {

protected attachBehaviors(): void {
this.allowMultipleProducts();
this.parseCodes();
}

private parseCodes(): void {
// We guess the code from the product name so we can prioritize search on it
// If no code is found, we just ignore it and everything still is fine
for (const option of Object.values(this.widget.options)) {
const match = codeParsingRegex.exec(option.text);
if (match !== null) {
option.code = match[1];
}
}
}

private allowMultipleProducts(): void {
Expand Down Expand Up @@ -60,6 +73,10 @@ export class CounterProductSelect extends AutoCompleteSelectBase {
}
protected tomSelectSettings(): RecursivePartial<TomSettings> {
/* We disable the dropdown on focus because we're going to always autofocus the widget */
return { ...super.tomSelectSettings(), openOnFocus: false };
return {
...super.tomSelectSettings(),
openOnFocus: false,
searchField: ["code", "text"],
};
}
}

0 comments on commit 429df81

Please sign in to comment.