From 46442eddf70eee46f65ae7a5a947e71376f9a6f7 Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Thu, 15 Aug 2024 12:50:47 +0300 Subject: [PATCH] refactor(core): don't bind this in controller options --- .../activedescendant-controller.ts | 6 +- .../controllers/combobox-controller.ts | 64 +++++++++---------- .../controllers/listbox-controller.ts | 36 +++++------ .../test/combobox-controller.spec.ts | 8 +-- elements/pf-select/pf-select.ts | 20 ++---- 5 files changed, 60 insertions(+), 74 deletions(-) diff --git a/core/pfe-core/controllers/activedescendant-controller.ts b/core/pfe-core/controllers/activedescendant-controller.ts index 79f9f84ea4..a5d1e9a47b 100644 --- a/core/pfe-core/controllers/activedescendant-controller.ts +++ b/core/pfe-core/controllers/activedescendant-controller.ts @@ -21,13 +21,13 @@ export interface ActivedescendantControllerOptions< * If you provide this callback, ActivedescendantController will call it on your item with the * active state. You may use this to set active styles. */ - setItemActive?(this: Item, active: boolean): void; + setItemActive?(item: Item, active: boolean): void; /** * Optional callback to retrieve the value from an option element. * By default, retrieves the `value` attribute, or the text content. * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement */ - getItemValue?(this: Item): string; + getItemValue?(item: Item): string; } /** @@ -134,7 +134,7 @@ export class ActivedescendantController< super.atFocusedItemIndex = index; const item = this._items.at(this.atFocusedItemIndex); for (const _item of this.items) { - this.options.setItemActive?.call(_item, _item === item); + this.options.setItemActive?.(_item, _item === item); } const container = this.options.getActiveDescendantContainer(); if (!ActivedescendantController.supportsCrossRootActiveDescendant) { diff --git a/core/pfe-core/controllers/combobox-controller.ts b/core/pfe-core/controllers/combobox-controller.ts index b639249ff9..9c0eb7ac1a 100644 --- a/core/pfe-core/controllers/combobox-controller.ts +++ b/core/pfe-core/controllers/combobox-controller.ts @@ -18,14 +18,6 @@ type AllOptions = type Lang = typeof ComboboxController['langs'][number]; -function getItemValue(this: Item): string { - if ('value' in this && typeof this.value === 'string') { - return this.value; - } else { - return ''; - } -} - function deepClosest(element: Element | null, selector: string) { let closest = element?.closest(selector); let root = element?.getRootNode(); @@ -45,31 +37,39 @@ function deepClosest(element: Element | null, selector: string) { return closest; } -function isItemFiltered(this: Item, value: string): boolean { - return !getItemValue.call(this) +function getItemValue(item: Item): string { + if ('value' in item && typeof item.value === 'string') { + return item.value; + } else { + return ''; + } +} + +function isItemFiltered(item: Item, value: string): boolean { + return !getItemValue(item) .toLowerCase() .startsWith(value.toLowerCase()); } -function setItemHidden(this: HTMLElement, hidden: boolean) { - this.hidden = hidden; +function setItemHidden(item: HTMLElement, hidden: boolean) { + item.hidden = hidden; } -function setComboboxValue(this: HTMLElement, value: string): void { - if (!('value' in this)) { +function setComboboxValue(item: HTMLElement, value: string): void { + if (!('value' in item)) { // eslint-disable-next-line no-console - return console.warn(`Cannot set value on combobox element ${this.localName}`); + return console.warn(`Cannot set value on combobox element ${item.localName}`); } else { - this.value = value; + item.value = value; } } -function getComboboxValue(this: HTMLElement): string { - if ('value' in this && typeof this.value === 'string') { - return this.value; +function getComboboxValue(combobox: HTMLElement): string { + if ('value' in combobox && typeof combobox.value === 'string') { + return combobox.value; } else { // eslint-disable-next-line no-console - return console.warn(`Cannot get value from combobox element ${this.localName}`), ''; + return console.warn(`Cannot get value from combobox element ${combobox.localName}`), ''; } } @@ -118,28 +118,28 @@ export interface ComboboxControllerOptions extends * of the item, as if it implemented the `