1
0
mirror of https://github.com/home-assistant/frontend.git synced 2026-02-15 07:25:54 +00:00

Fixes for picker combo box scrolling and selection (#29242)

This commit is contained in:
Aidan Timson
2026-01-29 14:11:30 +00:00
committed by Bram Kragten
parent 518cf87847
commit 1d0251cc28

View File

@@ -523,9 +523,7 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
this._items = this._getItems();
// Reset scroll position when filter changes
if (this.virtualizerElement) {
this.virtualizerElement.scrollToIndex(0);
}
this.virtualizerElement?.element(0)?.scrollIntoView();
}
private _registerKeyboardShortcuts() {
@@ -541,10 +539,36 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
private _focusList() {
if (this._selectedItemIndex === -1) {
this._selectNextItem();
this._initializeSelectedIndex();
}
}
/**
* Initialize keyboard selection to the currently selected value,
* or fall back to the first item when searching (skipping section titles).
*/
private _initializeSelectedIndex(): void {
if (!this.virtualizerElement?.items?.length) {
return;
}
const initialIndex = this._getInitialSelectedIndex();
// Only initialize to first item if searching, otherwise require a selected value
if (initialIndex === 0 && !this._search) {
return;
}
let index = initialIndex;
// Skip section titles (strings)
if (typeof this.virtualizerElement.items[index] === "string") {
index += 1;
}
// Bounds check: ensure index is valid after skipping section title
if (index >= this.virtualizerElement.items.length) {
return;
}
this._selectedItemIndex = index;
this._scrollToSelectedItem();
}
private _selectNextItem = (ev?: KeyboardEvent) => {
ev?.stopPropagation();
ev?.preventDefault();
@@ -563,6 +587,14 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
return;
}
// If no item is selected yet, start from the currently selected value
if (this._selectedItemIndex === -1) {
this._initializeSelectedIndex();
if (this._selectedItemIndex !== -1) {
return;
}
}
const nextIndex =
maxItems === this._selectedItemIndex
? this._selectedItemIndex
@@ -654,7 +686,9 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
?.querySelector(".selected")
?.classList.remove("selected");
this.virtualizerElement?.scrollToIndex(this._selectedItemIndex, "end");
this.virtualizerElement
?.element(this._selectedItemIndex)
?.scrollIntoView({ block: "nearest" });
requestAnimationFrame(() => {
this.virtualizerElement
@@ -690,7 +724,10 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
}
if (this._selectedItemIndex === -1) {
return;
this._initializeSelectedIndex();
if (this._selectedItemIndex === -1) {
return;
}
}
// if filter button is focused