Area Card Editor to Ha Form (#11762)

This commit is contained in:
Zack Barett 2022-02-21 13:21:21 -06:00 committed by GitHub
parent 564a725284
commit 27750b8b5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,18 +1,14 @@
import "@polymer/paper-input/paper-input"; import "../../../../components/ha-form/ha-form";
import { CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { assert, assign, boolean, object, optional, string } from "superstruct"; import { assert, assign, boolean, object, optional, string } from "superstruct";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/ha-area-picker"; import type { HomeAssistant } from "../../../../types";
import { HomeAssistant } from "../../../../types"; import type { AreaCardConfig } from "../../cards/types";
import { AreaCardConfig } from "../../cards/types"; import type { LovelaceCardEditor } from "../../types";
import "../../components/hui-theme-select-editor";
import { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct"; import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { EditorTarget } from "../types"; import type { HaFormSchema } from "../../../../components/ha-form/types";
import { configElementStyle } from "./config-elements-style";
import "../../../../components/ha-formfield";
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
const cardConfigStruct = assign( const cardConfigStruct = assign(
baseLovelaceCardConfig, baseLovelaceCardConfig,
@ -38,21 +34,18 @@ export class HuiAreaCardEditor
this._config = config; this._config = config;
} }
get _area(): string { private _schema = memoizeOne((): HaFormSchema[] => [
return this._config!.area || ""; { name: "area", selector: { area: {} } },
} { name: "show_camera", required: false, selector: { boolean: {} } },
{
get _navigation_path(): string { name: "",
return this._config!.navigation_path || ""; type: "grid",
} schema: [
{ name: "navigation_path", required: false, selector: { text: {} } },
get _theme(): string { { name: "theme", required: false, selector: { theme: {} } },
return this._config!.theme || ""; ],
} },
]);
get _show_camera(): boolean {
return this._config!.show_camera || false;
}
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this.hass || !this._config) { if (!this.hass || !this._config) {
@ -60,78 +53,35 @@ export class HuiAreaCardEditor
} }
return html` return html`
<div class="card-config"> <ha-form
<ha-area-picker .hass=${this.hass}
.hass=${this.hass} .data=${this._config}
.value=${this._area} .schema=${this._schema()}
.placeholder=${this._area} .computeLabel=${this._computeLabelCallback}
.configValue=${"area"} @value-changed=${this._valueChanged}
.label=${this.hass.localize( ></ha-form>
"ui.panel.lovelace.editor.card.area.name"
)}
@value-changed=${this._valueChanged}
></ha-area-picker>
<ha-formfield
.label=${this.hass.localize(
"ui.panel.lovelace.editor.card.area.show_camera"
)}
.dir=${computeRTLDirection(this.hass)}
>
<ha-switch
.checked=${this._show_camera}
.configValue=${"show_camera"}
@change=${this._valueChanged}
></ha-switch>
</ha-formfield>
<paper-input
.label=${this.hass!.localize(
"ui.panel.lovelace.editor.action-editor.navigation_path"
)}
.value=${this._navigation_path}
.configValue=${"navigation_path"}
@value-changed=${this._valueChanged}
>
</paper-input>
<hui-theme-select-editor
.hass=${this.hass}
.value=${this._theme}
.configValue=${"theme"}
@value-changed=${this._valueChanged}
></hui-theme-select-editor>
</div>
`; `;
} }
private _valueChanged(ev: CustomEvent): void { private _valueChanged(ev: CustomEvent): void {
if (!this._config || !this.hass) { const config = ev.detail.value;
return; Object.keys(config).forEach((k) => config[k] === "" && delete config[k]);
} fireEvent(this, "config-changed", { config });
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 });
} }
static get styles(): CSSResultGroup { private _computeLabelCallback = (schema: HaFormSchema) => {
return configElementStyle; 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 { declare global {