Don't update entity picker items while open (#5588)

* Don't update entity picker items while open

* Update items in updated, when we open the dropdown
This commit is contained in:
Bram Kragten 2020-04-24 10:36:38 +02:00 committed by GitHub
parent 2503fabe1d
commit 355f40d740
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 36 deletions

View File

@ -12,6 +12,7 @@ import {
property, property,
PropertyValues, PropertyValues,
TemplateResult, TemplateResult,
query,
} from "lit-element"; } from "lit-element";
import memoizeOne from "memoize-one"; import memoizeOne from "memoize-one";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
@ -91,12 +92,13 @@ class HaEntityPicker extends LitElement {
@property() public entityFilter?: HaEntityPickerEntityFilterFunc; @property() public entityFilter?: HaEntityPickerEntityFilterFunc;
@property({ type: Boolean }) private _opened?: boolean; @property({ type: Boolean }) private _opened = false;
@property() private _hass?: HomeAssistant; @query("vaadin-combo-box-light") private _comboBox!: HTMLElement;
private _getStates = memoizeOne( private _getStates = memoizeOne(
( (
_opened: boolean,
hass: this["hass"], hass: this["hass"],
includeDomains: this["includeDomains"], includeDomains: this["includeDomains"],
excludeDomains: this["excludeDomains"], excludeDomains: this["excludeDomains"],
@ -147,27 +149,28 @@ class HaEntityPicker extends LitElement {
); );
protected updated(changedProps: PropertyValues) { protected updated(changedProps: PropertyValues) {
super.updated(changedProps); if (changedProps.has("_opened") && this._opened) {
const states = this._getStates(
if (changedProps.has("hass") && !this._opened) { this._opened,
this._hass = this.hass; this.hass,
this.includeDomains,
this.excludeDomains,
this.entityFilter,
this.includeDeviceClasses
);
(this._comboBox as any).items = states;
} }
} }
protected render(): TemplateResult { protected render(): TemplateResult {
const states = this._getStates( if (!this.hass) {
this._hass, return html``;
this.includeDomains, }
this.excludeDomains,
this.entityFilter,
this.includeDeviceClasses
);
return html` return html`
<vaadin-combo-box-light <vaadin-combo-box-light
item-value-path="entity_id" item-value-path="entity_id"
item-label-path="entity_id" item-label-path="entity_id"
.items=${states}
.value=${this._value} .value=${this._value}
.allowCustomValue=${this.allowCustomEntity} .allowCustomValue=${this.allowCustomEntity}
.renderer=${rowRenderer} .renderer=${rowRenderer}
@ -176,8 +179,8 @@ class HaEntityPicker extends LitElement {
> >
<paper-input <paper-input
.autofocus=${this.autofocus} .autofocus=${this.autofocus}
.label=${this.label === undefined && this._hass .label=${this.label === undefined
? this._hass.localize("ui.components.entity.entity-picker.entity") ? this.hass.localize("ui.components.entity.entity-picker.entity")
: this.label} : this.label}
.value=${this._value} .value=${this._value}
.disabled=${this.disabled} .disabled=${this.disabled}
@ -190,7 +193,7 @@ class HaEntityPicker extends LitElement {
${this.value ${this.value
? html` ? html`
<paper-icon-button <paper-icon-button
aria-label=${this.hass!.localize( aria-label=${this.hass.localize(
"ui.components.entity.entity-picker.clear" "ui.components.entity.entity-picker.clear"
)} )}
slot="suffix" slot="suffix"
@ -203,20 +206,17 @@ class HaEntityPicker extends LitElement {
</paper-icon-button> </paper-icon-button>
` `
: ""} : ""}
${states.length > 0
? html` <paper-icon-button
<paper-icon-button aria-label=${this.hass.localize(
aria-label=${this.hass!.localize( "ui.components.entity.entity-picker.show_entities"
"ui.components.entity.entity-picker.show_entities" )}
)} slot="suffix"
slot="suffix" class="toggle-button"
class="toggle-button" .icon=${this._opened ? "hass:menu-up" : "hass:menu-down"}
.icon=${this._opened ? "hass:menu-up" : "hass:menu-down"} >
> Toggle
Toggle </paper-icon-button>
</paper-icon-button>
`
: ""}
</paper-input> </paper-input>
</vaadin-combo-box-light> </vaadin-combo-box-light>
`; `;

View File

@ -24,7 +24,7 @@ export class HaSceneAction extends LitElement implements ActionElement {
.hass=${this.hass} .hass=${this.hass}
.value=${scene} .value=${scene}
@value-changed=${this._entityPicked} @value-changed=${this._entityPicked}
.includeDomains=${["scene"]} include-domains="['scene']"
allow-custom-entity allow-custom-entity
></ha-entity-picker> ></ha-entity-picker>
`; `;

View File

@ -34,6 +34,8 @@ export class HaServiceAction extends LitElement implements ActionElement {
return { service: "", data: {} }; return { service: "", data: {} };
} }
private _domain = memoizeOne((service: string) => [computeDomain(service)]);
private _getServiceData = memoizeOne((service: string) => { private _getServiceData = memoizeOne((service: string) => {
if (!service) { if (!service) {
return []; return [];
@ -85,7 +87,7 @@ export class HaServiceAction extends LitElement implements ActionElement {
.value=${entity_id} .value=${entity_id}
.label=${entity.description} .label=${entity.description}
@value-changed=${this._entityPicked} @value-changed=${this._entityPicked}
.includeDomains=${[computeDomain(service)]} .includeDomains=${this._domain(service)}
allow-custom-entity allow-custom-entity
></ha-entity-picker> ></ha-entity-picker>
` `

View File

@ -46,7 +46,7 @@ export class HaZoneCondition extends LitElement {
@value-changed=${this._zonePicked} @value-changed=${this._zonePicked}
.hass=${this.hass} .hass=${this.hass}
allow-custom-entity allow-custom-entity
.includeDomains=${["zone"]} include-domains="['zone']"
></ha-entity-picker> ></ha-entity-picker>
<label id="eventlabel"> <label id="eventlabel">
${this.hass.localize( ${this.hass.localize(

View File

@ -42,7 +42,7 @@ export default class HaGeolocationTrigger extends LitElement {
@value-changed=${this._zonePicked} @value-changed=${this._zonePicked}
.hass=${this.hass} .hass=${this.hass}
allow-custom-entity allow-custom-entity
.includeDomains=${["zone"]} include-domains="['zone']"
></ha-entity-picker> ></ha-entity-picker>
<label id="eventlabel"> <label id="eventlabel">
${this.hass.localize( ${this.hass.localize(

View File

@ -49,7 +49,7 @@ export class HaZoneTrigger extends LitElement {
@value-changed=${this._zonePicked} @value-changed=${this._zonePicked}
.hass=${this.hass} .hass=${this.hass}
allow-custom-entity allow-custom-entity
.includeDomains=${["zone"]} include-domains="['zone']"
></ha-entity-picker> ></ha-entity-picker>
<label id="eventlabel"> <label id="eventlabel">
${this.hass.localize( ${this.hass.localize(