1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-20 02:38:53 +00:00

Generic picker: scroll to selected value on open (#28457)

This commit is contained in:
Wendelin
2025-12-09 12:27:56 +01:00
committed by GitHub
parent 2ce1eaf8c6
commit b80481b53e

View File

@@ -133,6 +133,8 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
@state() private _sectionTitle?: string; @state() private _sectionTitle?: string;
@state() private _valuePinned = true;
private _allItems: (PickerComboBoxItem | string)[] = []; private _allItems: (PickerComboBoxItem | string)[] = [];
private _selectedItemIndex = -1; private _selectedItemIndex = -1;
@@ -194,6 +196,15 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
.renderItem=${this._renderItem} .renderItem=${this._renderItem}
style="min-height: 36px;" style="min-height: 36px;"
class=${this._listScrolled ? "scrolled" : ""} class=${this._listScrolled ? "scrolled" : ""}
.layout=${this.value && this._valuePinned
? {
pin: {
index: this._getInitialSelectedIndex(),
block: "center",
},
}
: undefined}
@unpinned=${this._handleUnpinned}
@scroll=${this._onScrollList} @scroll=${this._onScrollList}
@focus=${this._focusList} @focus=${this._focusList}
@visibilityChanged=${this._visibilityChanged} @visibilityChanged=${this._visibilityChanged}
@@ -244,6 +255,11 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
} }
} }
@eventOptions({ passive: true })
private _handleUnpinned() {
this._valuePinned = false;
}
private _getAdditionalItems = (searchString?: string) => private _getAdditionalItems = (searchString?: string) =>
this.getAdditionalItems?.(searchString) || []; this.getAdditionalItems?.(searchString) || [];
@@ -592,6 +608,24 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
private _keyFunction = (item: PickerComboBoxItem | string) => private _keyFunction = (item: PickerComboBoxItem | string) =>
typeof item === "string" ? item : item.id; typeof item === "string" ? item : item.id;
private _getInitialSelectedIndex() {
if (!this._virtualizerElement || !this.value) {
return 0;
}
const index = this._virtualizerElement.items.findIndex(
(item) =>
typeof item !== "string" &&
(item as PickerComboBoxItem).id === this.value
);
if (index === -1) {
return 0;
}
return index;
}
static get styles() { static get styles() {
return [ return [
...super.styles, ...super.styles,