FIx entities picker (#6951)

Fixes #6947 ?
This commit is contained in:
Bram Kragten 2020-09-12 18:18:57 +02:00 committed by GitHub
parent 90e09fc384
commit 7e70ba6ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import { HassEntity } from "home-assistant-js-websocket";
import { import {
css, css,
CSSResult, CSSResult,
customElement,
html, html,
LitElement, LitElement,
property, property,
@ -51,7 +52,8 @@ const rowRenderer = (
root.querySelector("[secondary]")!.textContent = model.item.entity_id; root.querySelector("[secondary]")!.textContent = model.item.entity_id;
}; };
class HaEntityPicker extends LitElement { @customElement("ha-entity-picker")
export class HaEntityPicker extends LitElement {
@property({ type: Boolean }) public autofocus = false; @property({ type: Boolean }) public autofocus = false;
@property({ type: Boolean }) public disabled?: boolean; @property({ type: Boolean }) public disabled?: boolean;
@ -276,8 +278,6 @@ class HaEntityPicker extends LitElement {
} }
} }
customElements.define("ha-entity-picker", HaEntityPicker);
declare global { declare global {
interface HTMLElementTagNameMap { interface HTMLElementTagNameMap {
"ha-entity-picker": HaEntityPicker; "ha-entity-picker": HaEntityPicker;

View File

@ -18,10 +18,10 @@ import Sortable, {
} from "sortablejs/modular/sortable.core.esm"; } from "sortablejs/modular/sortable.core.esm";
import { fireEvent } from "../../../common/dom/fire_event"; import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/entity/ha-entity-picker"; import "../../../components/entity/ha-entity-picker";
import type { HaEntityPicker } from "../../../components/entity/ha-entity-picker";
import "../../../components/ha-icon-button"; import "../../../components/ha-icon-button";
import { sortableStyles } from "../../../resources/ha-sortable-style"; import { sortableStyles } from "../../../resources/ha-sortable-style";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { EditorTarget } from "../editor/types";
import { EntityConfig } from "../entity-rows/types"; import { EntityConfig } from "../entity-rows/types";
@customElement("hui-entity-editor") @customElement("hui-entity-editor")
@ -73,7 +73,7 @@ export class HuiEntityEditor extends LitElement {
.hass=${this.hass} .hass=${this.hass}
.value=${entityConf.entity} .value=${entityConf.entity}
.index=${index} .index=${index}
@change=${this._valueChanged} @value-changed=${this._valueChanged}
allow-custom-entity allow-custom-entity
></ha-entity-picker> ></ha-entity-picker>
</div> </div>
@ -83,7 +83,7 @@ export class HuiEntityEditor extends LitElement {
</div> </div>
<ha-entity-picker <ha-entity-picker
.hass=${this.hass} .hass=${this.hass}
@change=${this._addEntity} @value-changed=${this._addEntity}
></ha-entity-picker> ></ha-entity-picker>
`; `;
} }
@ -136,15 +136,15 @@ export class HuiEntityEditor extends LitElement {
}); });
} }
private async _addEntity(ev: Event): Promise<void> { private async _addEntity(ev: CustomEvent): Promise<void> {
const target = ev.target! as EditorTarget; const value = ev.detail.value;
if (target.value === "") { if (value === "") {
return; return;
} }
const newConfigEntities = this.entities!.concat({ const newConfigEntities = this.entities!.concat({
entity: target.value as string, entity: value as string,
}); });
target.value = ""; (ev.target as HaEntityPicker).value = "";
fireEvent(this, "entities-changed", { entities: newConfigEntities }); fireEvent(this, "entities-changed", { entities: newConfigEntities });
} }
@ -160,16 +160,17 @@ export class HuiEntityEditor extends LitElement {
fireEvent(this, "entities-changed", { entities: newEntities }); fireEvent(this, "entities-changed", { entities: newEntities });
} }
private _valueChanged(ev: Event): void { private _valueChanged(ev: CustomEvent): void {
const target = ev.target! as EditorTarget; const value = ev.detail.value;
const index = (ev.target as any).index;
const newConfigEntities = this.entities!.concat(); const newConfigEntities = this.entities!.concat();
if (target.value === "") { if (value === "") {
newConfigEntities.splice(target.index!, 1); newConfigEntities.splice(index, 1);
} else { } else {
newConfigEntities[target.index!] = { newConfigEntities[index] = {
...newConfigEntities[target.index!], ...newConfigEntities[index],
entity: target.value!, entity: value!,
}; };
} }