diff --git a/src/components/entity/ha-entity-attribute-picker.ts b/src/components/entity/ha-entity-attribute-picker.ts index 51b17fd53d..291d30da5e 100644 --- a/src/components/entity/ha-entity-attribute-picker.ts +++ b/src/components/entity/ha-entity-attribute-picker.ts @@ -1,54 +1,14 @@ -import { mdiCheck, mdiClose, mdiMenuDown, mdiMenuUp } from "@mdi/js"; -import "@polymer/paper-input/paper-input"; -import "@polymer/paper-item/paper-item"; -import "@vaadin/combo-box/theme/material/vaadin-combo-box-light"; import { HassEntity } from "home-assistant-js-websocket"; -import { - css, - CSSResultGroup, - html, - LitElement, - PropertyValues, - TemplateResult, -} from "lit"; -import { ComboBoxLitRenderer, comboBoxRenderer } from "lit-vaadin-helpers"; +import { html, LitElement, PropertyValues, TemplateResult } from "lit"; import { customElement, property, query } from "lit/decorators"; -import { fireEvent } from "../../common/dom/fire_event"; import { formatAttributeName } from "../../data/entity_attributes"; import { PolymerChangedEvent } from "../../polymer-types"; import { HomeAssistant } from "../../types"; -import "../ha-icon-button"; -import "../ha-svg-icon"; -import "./state-badge"; +import "../ha-combo-box"; +import type { HaComboBox } from "../ha-combo-box"; export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean; -// eslint-disable-next-line lit/prefer-static-styles -const rowRenderer: ComboBoxLitRenderer = (item) => html` - - ${formatAttributeName(item)}`; - @customElement("ha-entity-attribute-picker") class HaEntityAttributePicker extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @@ -68,7 +28,7 @@ class HaEntityAttributePicker extends LitElement { @property({ type: Boolean }) private _opened = false; - @query("vaadin-combo-box-light", true) private _comboBox!: HTMLElement; + @query("ha-combo-box", true) private _comboBox!: HaComboBox; protected shouldUpdate(changedProps: PropertyValues) { return !(!changedProps.has("_opened") && this._opened); @@ -78,7 +38,10 @@ class HaEntityAttributePicker extends LitElement { if (changedProps.has("_opened") && this._opened) { const state = this.entityId ? this.hass.states[this.entityId] : undefined; (this._comboBox as any).items = state - ? Object.keys(state.attributes) + ? Object.keys(state.attributes).map((key) => ({ + value: key, + label: formatAttributeName(key), + })) : []; } } @@ -89,100 +52,31 @@ class HaEntityAttributePicker extends LitElement { } return html` - - -
- ${this.value - ? html` - - ` - : ""} - - -
-
-
+ `; } - private _clearValue(ev: Event) { - ev.stopPropagation(); - this._setValue(""); - } - - private get _value() { - return this.value; - } - private _openedChanged(ev: PolymerChangedEvent) { this._opened = ev.detail.value; } private _valueChanged(ev: PolymerChangedEvent) { - const newValue = ev.detail.value; - if (newValue !== this._value) { - this._setValue(newValue); - } - } - - private _setValue(value: string) { - this.value = value; - setTimeout(() => { - fireEvent(this, "value-changed", { value }); - fireEvent(this, "change"); - }, 0); - } - - static get styles(): CSSResultGroup { - return css` - .suffix { - display: flex; - } - ha-icon-button { - --mdc-icon-button-size: 24px; - padding: 0px 2px; - color: var(--secondary-text-color); - } - [hidden] { - display: none; - } - `; + this.value = ev.detail.value; } } diff --git a/src/components/ha-combo-box.ts b/src/components/ha-combo-box.ts index c4717b4d33..6a44d33a6f 100644 --- a/src/components/ha-combo-box.ts +++ b/src/components/ha-combo-box.ts @@ -52,9 +52,6 @@ registerStyles( ` ); -const defaultRowRenderer: ComboBoxLitRenderer = (item) => - html`${item}`; - @customElement("ha-combo-box") export class HaComboBox extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @@ -112,7 +109,7 @@ export class HaComboBox extends LitElement { .filteredItems=${this.filteredItems} .allowCustomValue=${this.allowCustomValue} .disabled=${this.disabled} - ${comboBoxRenderer(this.renderer || defaultRowRenderer)} + ${comboBoxRenderer(this.renderer || this._defaultRowRenderer)} @opened-changed=${this._openedChanged} @filter-changed=${this._filterChanged} @value-changed=${this._valueChanged} @@ -147,6 +144,13 @@ export class HaComboBox extends LitElement { `; } + private _defaultRowRenderer: ComboBoxLitRenderer< + string | Record + > = (item) => + html` + ${this.itemLabelPath ? item[this.itemLabelPath] : item} + `; + private _clearValue(ev: Event) { ev.stopPropagation(); fireEvent(this, "value-changed", { value: undefined }); diff --git a/src/panels/config/automation/condition/ha-automation-condition-row.ts b/src/panels/config/automation/condition/ha-automation-condition-row.ts index 3e792abd18..b36118b680 100644 --- a/src/panels/config/automation/condition/ha-automation-condition-row.ts +++ b/src/panels/config/automation/condition/ha-automation-condition-row.ts @@ -24,7 +24,7 @@ export const handleChangeEvent = ( ev: CustomEvent ) => { ev.stopPropagation(); - const name = (ev.target as any)?.name; + const name = (ev.currentTarget as any)?.name; if (!name) { return; } diff --git a/src/panels/config/automation/trigger/ha-automation-trigger-row.ts b/src/panels/config/automation/trigger/ha-automation-trigger-row.ts index 15dbd9779a..f62a33fd7a 100644 --- a/src/panels/config/automation/trigger/ha-automation-trigger-row.ts +++ b/src/panels/config/automation/trigger/ha-automation-trigger-row.ts @@ -57,7 +57,7 @@ export interface TriggerElement extends LitElement { export const handleChangeEvent = (element: TriggerElement, ev: CustomEvent) => { ev.stopPropagation(); - const name = (ev.target as any)?.name; + const name = (ev.currentTarget as any)?.name; if (!name) { return; } diff --git a/src/panels/lovelace/editor/config-elements/hui-entity-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-entity-card-editor.ts index 845743d300..df9cb0e627 100644 --- a/src/panels/lovelace/editor/config-elements/hui-entity-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-entity-card-editor.ts @@ -175,7 +175,7 @@ export class HuiEntityCardEditor if (!this._config || !this.hass) { return; } - const target = ev.target! as EditorTarget; + const target = ev.currentTarget! as EditorTarget; if ( this[`_${target.configValue}`] === target.value || diff --git a/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts index 106010c441..cb1a53bede 100644 --- a/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts @@ -199,7 +199,8 @@ export class HuiWeatherForecastCardEditor if (!this._config || !this.hass) { return; } - const target = ev.target! as EditorTarget; + const target = ev.currentTarget! as EditorTarget; + if (this[`_${target.configValue}`] === target.value) { return; } diff --git a/src/panels/lovelace/editor/types.ts b/src/panels/lovelace/editor/types.ts index 73f2b3fe65..821c2dabcb 100644 --- a/src/panels/lovelace/editor/types.ts +++ b/src/panels/lovelace/editor/types.ts @@ -38,12 +38,12 @@ export interface ConfigError { message: string; } -export interface EntitiesEditorEvent { - detail?: { +export interface EntitiesEditorEvent extends CustomEvent { + detail: { entities?: EntityConfig[]; item?: any; }; - target?: EventTarget; + target: EventTarget | null; } export interface EditorTarget extends EventTarget {