Add support for constant in ha-form (#6324)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Bram Kragten 2020-07-07 09:23:08 +02:00 committed by GitHub
parent 26ae5fd728
commit 0c58c3572a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -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`<span class="label">${this.label}</span>: ${this.schema.value}`;
}
static get styles(): CSSResult {
return css`
:host {
display: block;
}
.label {
font-weight: 500;
}
`;
}
}
declare global {
interface HTMLElementTagNameMap {
"ha-form-constant": HaFormConstant;
}
}

View File

@ -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;