From 27750b8b5d01a0b0270f4cf3e4e012445ab635c0 Mon Sep 17 00:00:00 2001 From: Zack Barett Date: Mon, 21 Feb 2022 13:21:21 -0600 Subject: [PATCH] Area Card Editor to Ha Form (#11762) --- .../config-elements/hui-area-card-editor.ts | 134 ++++++------------ 1 file changed, 42 insertions(+), 92 deletions(-) diff --git a/src/panels/lovelace/editor/config-elements/hui-area-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-area-card-editor.ts index c2e9bb7564..547b3af4f5 100644 --- a/src/panels/lovelace/editor/config-elements/hui-area-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-area-card-editor.ts @@ -1,18 +1,14 @@ -import "@polymer/paper-input/paper-input"; -import { CSSResultGroup, html, LitElement, TemplateResult } from "lit"; +import "../../../../components/ha-form/ha-form"; +import { html, LitElement, TemplateResult } from "lit"; import { customElement, property, state } from "lit/decorators"; import { assert, assign, boolean, object, optional, string } from "superstruct"; +import memoizeOne from "memoize-one"; import { fireEvent } from "../../../../common/dom/fire_event"; -import "../../../../components/ha-area-picker"; -import { HomeAssistant } from "../../../../types"; -import { AreaCardConfig } from "../../cards/types"; -import "../../components/hui-theme-select-editor"; -import { LovelaceCardEditor } from "../../types"; +import type { HomeAssistant } from "../../../../types"; +import type { AreaCardConfig } from "../../cards/types"; +import type { LovelaceCardEditor } from "../../types"; import { baseLovelaceCardConfig } from "../structs/base-card-struct"; -import { EditorTarget } from "../types"; -import { configElementStyle } from "./config-elements-style"; -import "../../../../components/ha-formfield"; -import { computeRTLDirection } from "../../../../common/util/compute_rtl"; +import type { HaFormSchema } from "../../../../components/ha-form/types"; const cardConfigStruct = assign( baseLovelaceCardConfig, @@ -38,21 +34,18 @@ export class HuiAreaCardEditor this._config = config; } - get _area(): string { - return this._config!.area || ""; - } - - get _navigation_path(): string { - return this._config!.navigation_path || ""; - } - - get _theme(): string { - return this._config!.theme || ""; - } - - get _show_camera(): boolean { - return this._config!.show_camera || false; - } + private _schema = memoizeOne((): HaFormSchema[] => [ + { name: "area", selector: { area: {} } }, + { name: "show_camera", required: false, selector: { boolean: {} } }, + { + name: "", + type: "grid", + schema: [ + { name: "navigation_path", required: false, selector: { text: {} } }, + { name: "theme", required: false, selector: { theme: {} } }, + ], + }, + ]); protected render(): TemplateResult { if (!this.hass || !this._config) { @@ -60,78 +53,35 @@ export class HuiAreaCardEditor } return html` -
- - - - - - - -
+ `; } private _valueChanged(ev: CustomEvent): void { - if (!this._config || !this.hass) { - return; - } - const target = ev.target! as EditorTarget; - const value = - target.checked !== undefined ? target.checked : ev.detail.value; - - if (this[`_${target.configValue}`] === value) { - return; - } - - let newConfig; - if (target.configValue) { - if (!value) { - newConfig = { ...this._config }; - delete newConfig[target.configValue!]; - } else { - newConfig = { - ...this._config, - [target.configValue!]: value, - }; - } - } - fireEvent(this, "config-changed", { config: newConfig }); + const config = ev.detail.value; + Object.keys(config).forEach((k) => config[k] === "" && delete config[k]); + fireEvent(this, "config-changed", { config }); } - static get styles(): CSSResultGroup { - return configElementStyle; - } + private _computeLabelCallback = (schema: HaFormSchema) => { + switch (schema.name) { + case "area": + return this.hass!.localize("ui.panel.lovelace.editor.card.area.name"); + case "navigation_path": + return this.hass!.localize( + "ui.panel.lovelace.editor.action-editor.navigation_path" + ); + } + return this.hass!.localize( + `ui.panel.lovelace.editor.card.area.${schema.name}` + ); + }; } declare global {