From 0c58c3572a5aca29ba107c1f2d658224e05983ce Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 7 Jul 2020 09:23:08 +0200 Subject: [PATCH] Add support for constant in ha-form (#6324) Co-authored-by: Paulus Schoutsen --- src/components/ha-form/ha-form-constant.ts | 47 ++++++++++++++++++++++ src/components/ha-form/ha-form.ts | 7 ++++ 2 files changed, 54 insertions(+) create mode 100644 src/components/ha-form/ha-form-constant.ts diff --git a/src/components/ha-form/ha-form-constant.ts b/src/components/ha-form/ha-form-constant.ts new file mode 100644 index 0000000000..9ce3a02437 --- /dev/null +++ b/src/components/ha-form/ha-form-constant.ts @@ -0,0 +1,47 @@ +import { + customElement, + html, + LitElement, + property, + TemplateResult, + CSSResult, + css, + PropertyValues, +} from "lit-element"; +import { HaFormElement, HaFormConstantSchema } from "./ha-form"; +import { fireEvent } from "../../common/dom/fire_event"; + +@customElement("ha-form-constant") +export class HaFormConstant extends LitElement implements HaFormElement { + @property({ attribute: false }) public schema!: HaFormConstantSchema; + + @property() public label!: string; + + protected firstUpdated(changedProps: PropertyValues) { + super.firstUpdated(changedProps); + fireEvent(this, "value-changed", { + value: this.schema.value, + }); + } + + protected render(): TemplateResult { + return html`${this.label}: ${this.schema.value}`; + } + + static get styles(): CSSResult { + return css` + :host { + display: block; + } + .label { + font-weight: 500; + } + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "ha-form-constant": HaFormConstant; + } +} diff --git a/src/components/ha-form/ha-form.ts b/src/components/ha-form/ha-form.ts index be6b4eb1da..5f57df1bc1 100644 --- a/src/components/ha-form/ha-form.ts +++ b/src/components/ha-form/ha-form.ts @@ -15,8 +15,10 @@ import "./ha-form-multi_select"; import "./ha-form-positive_time_period_dict"; import "./ha-form-select"; import "./ha-form-string"; +import "./ha-form-constant"; export type HaFormSchema = + | HaFormConstantSchema | HaFormStringSchema | HaFormIntegerSchema | HaFormFloatSchema @@ -33,6 +35,11 @@ export interface HaFormBaseSchema { description?: { suffix?: string; suggested_value?: HaFormData }; } +export interface HaFormConstantSchema extends HaFormBaseSchema { + type: "constant"; + value: string; +} + export interface HaFormIntegerSchema extends HaFormBaseSchema { type: "integer"; default?: HaFormIntegerData;