From 976c74b8dad79eaa3cb27e1bd5475bf4229c157f Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 7 Jan 2026 15:42:47 +0100 Subject: [PATCH] Prefill the field with current value when editing a custom text item (#28840) --- .../entity/ha-entity-name-picker.ts | 5 ++ src/components/ha-generic-picker.ts | 87 +++++++++++-------- src/components/ha-picker-combo-box.ts | 6 ++ 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/src/components/entity/ha-entity-name-picker.ts b/src/components/entity/ha-entity-name-picker.ts index 608752aeef..a1f4806f72 100644 --- a/src/components/entity/ha-entity-name-picker.ts +++ b/src/components/entity/ha-entity-name-picker.ts @@ -275,6 +275,11 @@ export class HaEntityNamePicker extends LitElement { this._editIndex = idx; await this.updateComplete; await this._picker?.open(); + const value = this._items[idx]; + // Pre-fill the field value when editing a text item + if (value.type === "text" && value.text) { + this._picker?.setFieldValue(value.text); + } } private get _items(): EntityNameItem[] { diff --git a/src/components/ha-generic-picker.ts b/src/components/ha-generic-picker.ts index 939f934f67..6e2aa99317 100644 --- a/src/components/ha-generic-picker.ts +++ b/src/components/ha-generic-picker.ts @@ -130,6 +130,15 @@ export class HaGenericPicker extends PickerMixin(LitElement) { private _unsubscribeTinyKeys?: () => void; + public setFieldValue(value: string) { + if (this._comboBox) { + this._comboBox.setFieldValue(value); + return; + } + // Store initial value to set when opened + this._initialFieldValue = value; + } + protected render() { // Only show label if it's not a top label and there is a value. const label = this.useTopLabel && this.value ? undefined : this.label; @@ -182,40 +191,42 @@ export class HaGenericPicker extends PickerMixin(LitElement) { `} - ${!this._openedNarrow && (this._pickerWrapperOpen || this._opened) - ? html` - - ${this._renderComboBox()} - - ` - : this._pickerWrapperOpen || this._opened - ? html` - ${this._renderComboBox(true)} - ` - : nothing} + ${this._pickerWrapperOpen || this._opened + ? this._openedNarrow + ? html` + + ${this._renderComboBox(true)} + + ` + : html` + + ${this._renderComboBox()} + + ` + : nothing} ${this._renderHelper()}`; } @@ -283,9 +294,16 @@ export class HaGenericPicker extends PickerMixin(LitElement) { `; } + private _initialFieldValue?: string; + private _dialogOpened = () => { this._opened = true; requestAnimationFrame(() => { + // Set initial field value if needed + if (this._initialFieldValue) { + this._comboBox?.setFieldValue(this._initialFieldValue); + this._initialFieldValue = undefined; + } if (this.hass && isIosApp(this.hass)) { this.hass.auth.external!.fireMessage({ type: "focus_element", @@ -295,6 +313,7 @@ export class HaGenericPicker extends PickerMixin(LitElement) { }); return; } + this._comboBox?.focus(); }); }; diff --git a/src/components/ha-picker-combo-box.ts b/src/components/ha-picker-combo-box.ts index 34f5101f0f..5d4dadae65 100644 --- a/src/components/ha-picker-combo-box.ts +++ b/src/components/ha-picker-combo-box.ts @@ -153,6 +153,12 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) { @state() private _items: PickerComboBoxItem[] = []; + public setFieldValue(value: string) { + if (this._searchFieldElement) { + this._searchFieldElement.value = value; + } + } + protected get scrollableElement(): HTMLElement | null { return this._virtualizerElement as HTMLElement | null; }