Convert entity-attribute picker to ha-combo-box (#11587)

This commit is contained in:
Bram Kragten 2022-02-07 17:22:08 +01:00 committed by GitHub
parent 22df03427f
commit 869fa91ae5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 138 deletions

View File

@ -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 { HassEntity } from "home-assistant-js-websocket";
import { import { html, LitElement, PropertyValues, TemplateResult } from "lit";
css,
CSSResultGroup,
html,
LitElement,
PropertyValues,
TemplateResult,
} from "lit";
import { ComboBoxLitRenderer, comboBoxRenderer } from "lit-vaadin-helpers";
import { customElement, property, query } from "lit/decorators"; import { customElement, property, query } from "lit/decorators";
import { fireEvent } from "../../common/dom/fire_event";
import { formatAttributeName } from "../../data/entity_attributes"; import { formatAttributeName } from "../../data/entity_attributes";
import { PolymerChangedEvent } from "../../polymer-types"; import { PolymerChangedEvent } from "../../polymer-types";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import "../ha-icon-button"; import "../ha-combo-box";
import "../ha-svg-icon"; import type { HaComboBox } from "../ha-combo-box";
import "./state-badge";
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean; export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
// eslint-disable-next-line lit/prefer-static-styles
const rowRenderer: ComboBoxLitRenderer<string> = (item) => html`<style>
paper-item {
padding: 0;
margin: -10px;
margin-left: 0;
}
#content {
display: flex;
align-items: center;
}
ha-svg-icon {
padding-left: 2px;
margin-right: -2px;
color: var(--secondary-text-color);
}
:host(:not([selected])) ha-svg-icon {
display: none;
}
:host([selected]) paper-item {
margin-left: 10px;
}
</style>
<ha-svg-icon .path=${mdiCheck}></ha-svg-icon>
<paper-item>${formatAttributeName(item)}</paper-item>`;
@customElement("ha-entity-attribute-picker") @customElement("ha-entity-attribute-picker")
class HaEntityAttributePicker extends LitElement { class HaEntityAttributePicker extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@ -68,7 +28,7 @@ class HaEntityAttributePicker extends LitElement {
@property({ type: Boolean }) private _opened = false; @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) { protected shouldUpdate(changedProps: PropertyValues) {
return !(!changedProps.has("_opened") && this._opened); return !(!changedProps.has("_opened") && this._opened);
@ -78,7 +38,10 @@ class HaEntityAttributePicker extends LitElement {
if (changedProps.has("_opened") && this._opened) { if (changedProps.has("_opened") && this._opened) {
const state = this.entityId ? this.hass.states[this.entityId] : undefined; const state = this.entityId ? this.hass.states[this.entityId] : undefined;
(this._comboBox as any).items = state (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` return html`
<vaadin-combo-box-light <ha-combo-box
.value=${this._value} .hass=${this.hass}
.allowCustomValue=${this.allowCustomValue} .value=${this.value || ""}
attr-for-value="bind-value"
${comboBoxRenderer(rowRenderer)}
@opened-changed=${this._openedChanged}
@value-changed=${this._valueChanged}
>
<paper-input
.autofocus=${this.autofocus} .autofocus=${this.autofocus}
.label=${this.label ?? .label=${this.label ??
this.hass.localize( this.hass.localize(
"ui.components.entity.entity-attribute-picker.attribute" "ui.components.entity.entity-attribute-picker.attribute"
)} )}
.value=${this._value ? formatAttributeName(this._value) : ""}
.disabled=${this.disabled || !this.entityId} .disabled=${this.disabled || !this.entityId}
class="input" .allowCustomValue=${this.allowCustomValue}
autocapitalize="none" item-value-path="value"
autocomplete="off" item-label-path="label"
autocorrect="off" @opened-changed=${this._openedChanged}
spellcheck="false" @value-changed=${this._valueChanged}
> >
<div class="suffix" slot="suffix"> </ha-combo-box>
${this.value
? html`
<ha-icon-button
.label=${this.hass.localize(
"ui.components.entity.entity-picker.clear"
)}
.path=${mdiClose}
class="clear-button"
tabindex="-1"
@click=${this._clearValue}
no-ripple
></ha-icon-button>
`
: ""}
<ha-icon-button
.label=${this.hass.localize(
"ui.components.entity.entity-attribute-picker.show_attributes"
)}
.path=${this._opened ? mdiMenuUp : mdiMenuDown}
class="toggle-button"
tabindex="-1"
></ha-icon-button>
</div>
</paper-input>
</vaadin-combo-box-light>
`; `;
} }
private _clearValue(ev: Event) {
ev.stopPropagation();
this._setValue("");
}
private get _value() {
return this.value;
}
private _openedChanged(ev: PolymerChangedEvent<boolean>) { private _openedChanged(ev: PolymerChangedEvent<boolean>) {
this._opened = ev.detail.value; this._opened = ev.detail.value;
} }
private _valueChanged(ev: PolymerChangedEvent<string>) { private _valueChanged(ev: PolymerChangedEvent<string>) {
const newValue = ev.detail.value; this.value = 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;
}
`;
} }
} }

View File

@ -52,9 +52,6 @@ registerStyles(
` `
); );
const defaultRowRenderer: ComboBoxLitRenderer<string> = (item) =>
html`<mwc-list-item>${item}</mwc-list-item>`;
@customElement("ha-combo-box") @customElement("ha-combo-box")
export class HaComboBox extends LitElement { export class HaComboBox extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@ -112,7 +109,7 @@ export class HaComboBox extends LitElement {
.filteredItems=${this.filteredItems} .filteredItems=${this.filteredItems}
.allowCustomValue=${this.allowCustomValue} .allowCustomValue=${this.allowCustomValue}
.disabled=${this.disabled} .disabled=${this.disabled}
${comboBoxRenderer(this.renderer || defaultRowRenderer)} ${comboBoxRenderer(this.renderer || this._defaultRowRenderer)}
@opened-changed=${this._openedChanged} @opened-changed=${this._openedChanged}
@filter-changed=${this._filterChanged} @filter-changed=${this._filterChanged}
@value-changed=${this._valueChanged} @value-changed=${this._valueChanged}
@ -147,6 +144,13 @@ export class HaComboBox extends LitElement {
`; `;
} }
private _defaultRowRenderer: ComboBoxLitRenderer<
string | Record<string, any>
> = (item) =>
html`<mwc-list-item>
${this.itemLabelPath ? item[this.itemLabelPath] : item}
</mwc-list-item>`;
private _clearValue(ev: Event) { private _clearValue(ev: Event) {
ev.stopPropagation(); ev.stopPropagation();
fireEvent(this, "value-changed", { value: undefined }); fireEvent(this, "value-changed", { value: undefined });

View File

@ -24,7 +24,7 @@ export const handleChangeEvent = (
ev: CustomEvent ev: CustomEvent
) => { ) => {
ev.stopPropagation(); ev.stopPropagation();
const name = (ev.target as any)?.name; const name = (ev.currentTarget as any)?.name;
if (!name) { if (!name) {
return; return;
} }

View File

@ -57,7 +57,7 @@ export interface TriggerElement extends LitElement {
export const handleChangeEvent = (element: TriggerElement, ev: CustomEvent) => { export const handleChangeEvent = (element: TriggerElement, ev: CustomEvent) => {
ev.stopPropagation(); ev.stopPropagation();
const name = (ev.target as any)?.name; const name = (ev.currentTarget as any)?.name;
if (!name) { if (!name) {
return; return;
} }

View File

@ -175,7 +175,7 @@ export class HuiEntityCardEditor
if (!this._config || !this.hass) { if (!this._config || !this.hass) {
return; return;
} }
const target = ev.target! as EditorTarget; const target = ev.currentTarget! as EditorTarget;
if ( if (
this[`_${target.configValue}`] === target.value || this[`_${target.configValue}`] === target.value ||

View File

@ -199,7 +199,8 @@ export class HuiWeatherForecastCardEditor
if (!this._config || !this.hass) { if (!this._config || !this.hass) {
return; return;
} }
const target = ev.target! as EditorTarget; const target = ev.currentTarget! as EditorTarget;
if (this[`_${target.configValue}`] === target.value) { if (this[`_${target.configValue}`] === target.value) {
return; return;
} }

View File

@ -38,12 +38,12 @@ export interface ConfigError {
message: string; message: string;
} }
export interface EntitiesEditorEvent { export interface EntitiesEditorEvent extends CustomEvent {
detail?: { detail: {
entities?: EntityConfig[]; entities?: EntityConfig[];
item?: any; item?: any;
}; };
target?: EventTarget; target: EventTarget | null;
} }
export interface EditorTarget extends EventTarget { export interface EditorTarget extends EventTarget {