Skip to content

Commit

Permalink
fix(core): ensure compatibility in combobox controller
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Aug 15, 2024
1 parent 7917a9c commit c2e5a2f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 13 additions & 5 deletions core/pfe-core/controllers/combobox-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ export interface ComboboxControllerOptions<Item extends HTMLElement> extends
* Callback which the host must implement to change the expanded state to true.
* Return or resolve false to prevent the change.
*/
requestShowListbox(): boolean | Promise<boolean>;
requestShowListbox(): void | boolean | Promise<boolean> | Promise<void>;
/**
* Callback which the host must implement to change the expanded to false.
* Return or resolve false to prevent the default.
*/
requestHideListbox(): boolean | Promise<boolean>;
requestHideListbox(): void | boolean | Promise<boolean> | Promise<void>;
/**
* Returns the listbox container element
*/
Expand Down Expand Up @@ -325,7 +325,10 @@ export class ComboboxController<
this.#lb = ListboxController.of(host, {
isItem: this.options.isItem,
getItemsContainer: this.options.getListboxElement,
getControlsElements: () => [this.#button, this.#input].filter(x => !!x),
getControlsElements: () => [
this.options.getToggleButton(),
this.options.getComboboxInput(),
].filter(x => !!x),
getATFocusedItem: () => this.items[this.#fc?.atFocusedItemIndex ?? -1] ?? null,
isItemDisabled: this.options.isItemDisabled,
setItemSelected: this.options.setItemSelected,
Expand Down Expand Up @@ -444,13 +447,18 @@ export class ComboboxController<
this.#fc = ActivedescendantController.of(this.host, {
getItems, getItemsContainer, getOrientation,
getActiveDescendantContainer: () => this.#input,
getControlsElements: () => [this.#button, this.#input].filter(x => !!x),
getControlsElements: () => [
this.options.getToggleButton(),
this.options.getComboboxInput(),
].filter(x => !!x),
setItemActive: this.options.setItemActive,
});
} else {
this.#fc = RovingTabindexController.of(this.host, {
getItems, getItemsContainer, getOrientation,
getControlsElements: () => [this.#button].filter(x => !!x),
getControlsElements: () => [
this.options.getToggleButton(),
].filter(x => !!x),
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions core/pfe-core/controllers/listbox-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export class ListboxController<Item extends HTMLElement> implements ReactiveCont

async hostConnected(): Promise<void> {
await this.host.updateComplete;
this.hostUpdate();
this.hostUpdated();
}

Expand All @@ -266,7 +267,7 @@ export class ListboxController<Item extends HTMLElement> implements ReactiveCont
}
}

hostUpdated(): void {
hostUpdate(): void {
const last = this.#controlsElements;
this.#controlsElements = this.#options.getControlsElements?.() ?? [];
if (!arraysAreEquivalent(last, this.#controlsElements)) {
Expand All @@ -276,9 +277,12 @@ export class ListboxController<Item extends HTMLElement> implements ReactiveCont
el.addEventListener('keyup', this.#onKeyup);
}
}
}

hostUpdated(): void {
if (!this.#listening) {
this.container?.addEventListener('click', this.#onClick);
this.container?.addEventListener('keydown', this.#onKeydown, { capture: true });
this.container?.addEventListener('keydown', this.#onKeydown);
this.container?.addEventListener('keyup', this.#onKeyup);
this.#listening = true;
}
Expand Down

0 comments on commit c2e5a2f

Please sign in to comment.