mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 02:06:42 +00:00
Allow disabling an ha-form (#10218)
This commit is contained in:
parent
774f22b7e7
commit
9bf41a37b4
@ -1,6 +1,8 @@
|
|||||||
|
import { Button } from "@material/mwc-button";
|
||||||
import { html, LitElement, css, TemplateResult } from "lit";
|
import { html, LitElement, css, TemplateResult } from "lit";
|
||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import { applyThemesOnElement } from "../../../src/common/dom/apply_themes_on_element";
|
import { applyThemesOnElement } from "../../../src/common/dom/apply_themes_on_element";
|
||||||
|
import { fireEvent } from "../../../src/common/dom/fire_event";
|
||||||
|
|
||||||
@customElement("demo-black-white-row")
|
@customElement("demo-black-white-row")
|
||||||
class DemoBlackWhiteRow extends LitElement {
|
class DemoBlackWhiteRow extends LitElement {
|
||||||
@ -8,6 +10,8 @@ class DemoBlackWhiteRow extends LitElement {
|
|||||||
|
|
||||||
@property() value!: any;
|
@property() value!: any;
|
||||||
|
|
||||||
|
@property() disabled = false;
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
<div class="row">
|
<div class="row">
|
||||||
@ -17,7 +21,12 @@ class DemoBlackWhiteRow extends LitElement {
|
|||||||
<slot name="light"></slot>
|
<slot name="light"></slot>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-actions">
|
<div class="card-actions">
|
||||||
<mwc-button>Submit</mwc-button>
|
<mwc-button
|
||||||
|
.disabled=${this.disabled}
|
||||||
|
@click=${this.handleSubmit}
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</mwc-button>
|
||||||
</div>
|
</div>
|
||||||
</ha-card>
|
</ha-card>
|
||||||
</div>
|
</div>
|
||||||
@ -27,7 +36,12 @@ class DemoBlackWhiteRow extends LitElement {
|
|||||||
<slot name="dark"></slot>
|
<slot name="dark"></slot>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-actions">
|
<div class="card-actions">
|
||||||
<mwc-button>Submit</mwc-button>
|
<mwc-button
|
||||||
|
.disabled=${this.disabled}
|
||||||
|
@click=${this.handleSubmit}
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</mwc-button>
|
||||||
</div>
|
</div>
|
||||||
</ha-card>
|
</ha-card>
|
||||||
<pre>${JSON.stringify(this.value, undefined, 2)}</pre>
|
<pre>${JSON.stringify(this.value, undefined, 2)}</pre>
|
||||||
@ -51,6 +65,13 @@ class DemoBlackWhiteRow extends LitElement {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleSubmit(ev) {
|
||||||
|
const content = (ev.target as Button).closest(".content")!;
|
||||||
|
fireEvent(this, "submitted" as any, {
|
||||||
|
slot: content.classList.contains("light") ? "light" : "dark",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
static styles = css`
|
static styles = css`
|
||||||
.row {
|
.row {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
@ -230,12 +230,26 @@ class DemoHaForm extends LitElement {
|
|||||||
({ schema, data }) => data || computeInitialHaFormData(schema)
|
({ schema, data }) => data || computeInitialHaFormData(schema)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
private disabled = SCHEMAS.map(() => false);
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
${SCHEMAS.map((info, idx) => {
|
${SCHEMAS.map((info, idx) => {
|
||||||
const translations = info.translations || {};
|
const translations = info.translations || {};
|
||||||
return html`
|
return html`
|
||||||
<demo-black-white-row .title=${info.title} .value=${this.data[idx]}>
|
<demo-black-white-row
|
||||||
|
.title=${info.title}
|
||||||
|
.value=${this.data[idx]}
|
||||||
|
.disabled=${this.disabled[idx]}
|
||||||
|
@submitted=${() => {
|
||||||
|
this.disabled[idx] = true;
|
||||||
|
this.requestUpdate();
|
||||||
|
setTimeout(() => {
|
||||||
|
this.disabled[idx] = false;
|
||||||
|
this.requestUpdate();
|
||||||
|
}, 2000);
|
||||||
|
}}
|
||||||
|
>
|
||||||
${["light", "dark"].map(
|
${["light", "dark"].map(
|
||||||
(slot) => html`
|
(slot) => html`
|
||||||
<ha-form
|
<ha-form
|
||||||
@ -243,6 +257,7 @@ class DemoHaForm extends LitElement {
|
|||||||
.data=${this.data[idx]}
|
.data=${this.data[idx]}
|
||||||
.schema=${info.schema}
|
.schema=${info.schema}
|
||||||
.error=${info.error}
|
.error=${info.error}
|
||||||
|
.disabled=${this.disabled[idx]}
|
||||||
.computeError=${(error) => translations[error] || error}
|
.computeError=${(error) => translations[error] || error}
|
||||||
.computeLabel=${(schema) =>
|
.computeLabel=${(schema) =>
|
||||||
translations[schema.name] || schema.name}
|
translations[schema.name] || schema.name}
|
||||||
|
@ -39,6 +39,8 @@ class HaAuthFlow extends litLocalizeLiteMixin(LitElement) {
|
|||||||
|
|
||||||
@state() private _errorMessage?: string;
|
@state() private _errorMessage?: string;
|
||||||
|
|
||||||
|
@state() private _submitting = false;
|
||||||
|
|
||||||
willUpdate(changedProps: PropertyValues) {
|
willUpdate(changedProps: PropertyValues) {
|
||||||
super.willUpdate(changedProps);
|
super.willUpdate(changedProps);
|
||||||
|
|
||||||
@ -135,13 +137,15 @@ class HaAuthFlow extends litLocalizeLiteMixin(LitElement) {
|
|||||||
return html`
|
return html`
|
||||||
${this._renderStep(this._step)}
|
${this._renderStep(this._step)}
|
||||||
<div class="action">
|
<div class="action">
|
||||||
<mwc-button raised @click=${this._handleSubmit}
|
<mwc-button
|
||||||
>${this._step.type === "form"
|
raised
|
||||||
? this.localize("ui.panel.page-authorize.form.next")
|
@click=${this._handleSubmit}
|
||||||
: this.localize(
|
.disabled=${this._submitting}
|
||||||
"ui.panel.page-authorize.form.start_over"
|
|
||||||
)}</mwc-button
|
|
||||||
>
|
>
|
||||||
|
${this._step.type === "form"
|
||||||
|
? this.localize("ui.panel.page-authorize.form.next")
|
||||||
|
: this.localize("ui.panel.page-authorize.form.start_over")}
|
||||||
|
</mwc-button>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
case "error":
|
case "error":
|
||||||
@ -192,6 +196,7 @@ class HaAuthFlow extends litLocalizeLiteMixin(LitElement) {
|
|||||||
.data=${this._stepData}
|
.data=${this._stepData}
|
||||||
.schema=${step.data_schema}
|
.schema=${step.data_schema}
|
||||||
.error=${step.errors}
|
.error=${step.errors}
|
||||||
|
.disabled=${this._submitting}
|
||||||
.computeLabel=${this._computeLabelCallback(step)}
|
.computeLabel=${this._computeLabelCallback(step)}
|
||||||
.computeError=${this._computeErrorCallback(step)}
|
.computeError=${this._computeErrorCallback(step)}
|
||||||
@value-changed=${this._stepDataChanged}
|
@value-changed=${this._stepDataChanged}
|
||||||
@ -317,9 +322,7 @@ class HaAuthFlow extends litLocalizeLiteMixin(LitElement) {
|
|||||||
this._providerChanged(this.authProvider);
|
this._providerChanged(this.authProvider);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._state = "loading";
|
this._submitting = true;
|
||||||
// To avoid a jumping UI.
|
|
||||||
this.style.setProperty("min-height", `${this.offsetHeight}px`);
|
|
||||||
|
|
||||||
const postData = { ...this._stepData, client_id: this.clientId };
|
const postData = { ...this._stepData, client_id: this.clientId };
|
||||||
|
|
||||||
@ -344,16 +347,12 @@ class HaAuthFlow extends litLocalizeLiteMixin(LitElement) {
|
|||||||
this._state = "error";
|
this._state = "error";
|
||||||
this._errorMessage = this._unknownError();
|
this._errorMessage = this._unknownError();
|
||||||
} finally {
|
} finally {
|
||||||
this.style.setProperty("min-height", "");
|
this._submitting = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
return css`
|
return css`
|
||||||
:host {
|
|
||||||
/* So we can set min-height to avoid jumping during loading */
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
.action {
|
.action {
|
||||||
margin: 24px 0 8px;
|
margin: 24px 0 8px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
@ -20,6 +20,8 @@ class HaDurationInput extends LitElement {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public enableMillisecond?: boolean;
|
@property({ type: Boolean }) public enableMillisecond?: boolean;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@query("paper-time-input", true) private _input?: HTMLElement;
|
@query("paper-time-input", true) private _input?: HTMLElement;
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
@ -34,6 +36,7 @@ class HaDurationInput extends LitElement {
|
|||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.required=${this.required}
|
.required=${this.required}
|
||||||
.autoValidate=${this.required}
|
.autoValidate=${this.required}
|
||||||
|
.disabled=${this.disabled}
|
||||||
error-message="Required"
|
error-message="Required"
|
||||||
enable-second
|
enable-second
|
||||||
.enableMillisecond=${this.enableMillisecond}
|
.enableMillisecond=${this.enableMillisecond}
|
||||||
|
@ -18,6 +18,8 @@ export class HaFormBoolean extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
@property() public label!: string;
|
@property() public label!: string;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@query("paper-checkbox", true) private _input?: HTMLElement;
|
@query("paper-checkbox", true) private _input?: HTMLElement;
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
@ -31,6 +33,7 @@ export class HaFormBoolean extends LitElement implements HaFormElement {
|
|||||||
<mwc-formfield .label=${this.label}>
|
<mwc-formfield .label=${this.label}>
|
||||||
<ha-checkbox
|
<ha-checkbox
|
||||||
.checked=${this.data}
|
.checked=${this.data}
|
||||||
|
.disabled=${this.disabled}
|
||||||
@change=${this._valueChanged}
|
@change=${this._valueChanged}
|
||||||
></ha-checkbox>
|
></ha-checkbox>
|
||||||
</mwc-formfield>
|
</mwc-formfield>
|
||||||
|
@ -13,6 +13,8 @@ export class HaFormFloat extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
@property() public label!: string;
|
@property() public label!: string;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@query("mwc-textfield") private _input?: HTMLElement;
|
@query("mwc-textfield") private _input?: HTMLElement;
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
@ -27,6 +29,7 @@ export class HaFormFloat extends LitElement implements HaFormElement {
|
|||||||
inputMode="decimal"
|
inputMode="decimal"
|
||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.value=${this.data !== undefined ? this.data : ""}
|
.value=${this.data !== undefined ? this.data : ""}
|
||||||
|
.disabled=${this.disabled}
|
||||||
.required=${this.schema.required}
|
.required=${this.schema.required}
|
||||||
.autoValidate=${this.schema.required}
|
.autoValidate=${this.schema.required}
|
||||||
.suffix=${this.schema.description?.suffix}
|
.suffix=${this.schema.description?.suffix}
|
||||||
|
@ -23,6 +23,8 @@ export class HaFormInteger extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
@property() public label?: string;
|
@property() public label?: string;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@query("paper-input ha-slider") private _input?: HTMLElement;
|
@query("paper-input ha-slider") private _input?: HTMLElement;
|
||||||
|
|
||||||
private _lastValue?: HaFormIntegerData;
|
private _lastValue?: HaFormIntegerData;
|
||||||
@ -44,6 +46,7 @@ export class HaFormInteger extends LitElement implements HaFormElement {
|
|||||||
<ha-checkbox
|
<ha-checkbox
|
||||||
@change=${this._handleCheckboxChange}
|
@change=${this._handleCheckboxChange}
|
||||||
.checked=${this.data !== undefined}
|
.checked=${this.data !== undefined}
|
||||||
|
.disabled=${this.disabled}
|
||||||
></ha-checkbox>
|
></ha-checkbox>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
@ -52,7 +55,8 @@ export class HaFormInteger extends LitElement implements HaFormElement {
|
|||||||
.value=${this._value}
|
.value=${this._value}
|
||||||
.min=${this.schema.valueMin}
|
.min=${this.schema.valueMin}
|
||||||
.max=${this.schema.valueMax}
|
.max=${this.schema.valueMax}
|
||||||
.disabled=${this.data === undefined && this.schema.optional}
|
.disabled=${this.disabled ||
|
||||||
|
(this.data === undefined && this.schema.optional)}
|
||||||
@change=${this._valueChanged}
|
@change=${this._valueChanged}
|
||||||
></mwc-slider>
|
></mwc-slider>
|
||||||
</div>
|
</div>
|
||||||
@ -66,6 +70,7 @@ export class HaFormInteger extends LitElement implements HaFormElement {
|
|||||||
inputMode="numeric"
|
inputMode="numeric"
|
||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.value=${this.data !== undefined ? this.data : ""}
|
.value=${this.data !== undefined ? this.data : ""}
|
||||||
|
.disabled=${this.disabled}
|
||||||
.required=${this.schema.required}
|
.required=${this.schema.required}
|
||||||
.autoValidate=${this.schema.required}
|
.autoValidate=${this.schema.required}
|
||||||
.suffix=${this.schema.description?.suffix}
|
.suffix=${this.schema.description?.suffix}
|
||||||
|
@ -39,6 +39,8 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
@property() public label!: string;
|
@property() public label!: string;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@state() private _opened = false;
|
@state() private _opened = false;
|
||||||
|
|
||||||
@query("paper-menu-button", true) private _input?: HTMLElement;
|
@query("paper-menu-button", true) private _input?: HTMLElement;
|
||||||
@ -60,6 +62,7 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
|
|||||||
<ha-checkbox
|
<ha-checkbox
|
||||||
.checked=${data.includes(value)}
|
.checked=${data.includes(value)}
|
||||||
.value=${value}
|
.value=${value}
|
||||||
|
.disabled=${this.disabled}
|
||||||
@change=${this._valueChanged}
|
@change=${this._valueChanged}
|
||||||
></ha-checkbox>
|
></ha-checkbox>
|
||||||
</mwc-formfield>
|
</mwc-formfield>
|
||||||
@ -73,6 +76,7 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
return html`
|
return html`
|
||||||
<ha-button-menu
|
<ha-button-menu
|
||||||
|
.disabled=${this.disabled}
|
||||||
fixed
|
fixed
|
||||||
corner="BOTTOM_START"
|
corner="BOTTOM_START"
|
||||||
@opened=${this._handleOpen}
|
@opened=${this._handleOpen}
|
||||||
@ -84,6 +88,7 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
|
|||||||
.value=${data
|
.value=${data
|
||||||
.map((value) => this.schema.options![value] || value)
|
.map((value) => this.schema.options![value] || value)
|
||||||
.join(", ")}
|
.join(", ")}
|
||||||
|
.disabled=${this.disabled}
|
||||||
tabindex="-1"
|
tabindex="-1"
|
||||||
></mwc-textfield>
|
></mwc-textfield>
|
||||||
<ha-svg-icon
|
<ha-svg-icon
|
||||||
|
@ -11,6 +11,8 @@ export class HaFormTimePeriod extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
@property() public label!: string;
|
@property() public label!: string;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@query("ha-time-input", true) private _input?: HTMLElement;
|
@query("ha-time-input", true) private _input?: HTMLElement;
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
@ -25,6 +27,7 @@ export class HaFormTimePeriod extends LitElement implements HaFormElement {
|
|||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.required=${this.schema.required}
|
.required=${this.schema.required}
|
||||||
.data=${this.data}
|
.data=${this.data}
|
||||||
|
.disabled=${this.disabled}
|
||||||
></ha-duration-input>
|
></ha-duration-input>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ export class HaFormSelect extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
@property() public label!: string;
|
@property() public label!: string;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@query("mwc-select", true) private _input?: HTMLElement;
|
@query("mwc-select", true) private _input?: HTMLElement;
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
@ -38,6 +40,7 @@ export class HaFormSelect extends LitElement implements HaFormElement {
|
|||||||
<ha-radio
|
<ha-radio
|
||||||
.checked=${value === this.data}
|
.checked=${value === this.data}
|
||||||
.value=${value}
|
.value=${value}
|
||||||
|
.disabled=${this.disabled}
|
||||||
@change=${this._valueChanged}
|
@change=${this._valueChanged}
|
||||||
></ha-radio>
|
></ha-radio>
|
||||||
</mwc-formfield>
|
</mwc-formfield>
|
||||||
@ -52,6 +55,7 @@ export class HaFormSelect extends LitElement implements HaFormElement {
|
|||||||
fixedMenuPosition
|
fixedMenuPosition
|
||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.value=${this.data}
|
.value=${this.data}
|
||||||
|
.disabled=${this.disabled}
|
||||||
@closed=${stopPropagation}
|
@closed=${stopPropagation}
|
||||||
@selected=${this._valueChanged}
|
@selected=${this._valueChanged}
|
||||||
>
|
>
|
||||||
|
@ -28,6 +28,8 @@ export class HaFormString extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
@property() public label!: string;
|
@property() public label!: string;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@state() private _unmaskedPassword = false;
|
@state() private _unmaskedPassword = false;
|
||||||
|
|
||||||
@query("mwc-textfield") private _input?: HTMLElement;
|
@query("mwc-textfield") private _input?: HTMLElement;
|
||||||
@ -51,6 +53,7 @@ export class HaFormString extends LitElement implements HaFormElement {
|
|||||||
: "password"}
|
: "password"}
|
||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.value=${this.data || ""}
|
.value=${this.data || ""}
|
||||||
|
.disabled=${this.disabled}
|
||||||
.required=${this.schema.required}
|
.required=${this.schema.required}
|
||||||
.autoValidate=${this.schema.required}
|
.autoValidate=${this.schema.required}
|
||||||
.suffix=${isPassword
|
.suffix=${isPassword
|
||||||
|
@ -23,6 +23,8 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
|
|
||||||
@property() public error?: Record<string, string>;
|
@property() public error?: Record<string, string>;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
@property() public computeError?: (schema: HaFormSchema, error) => string;
|
@property() public computeError?: (schema: HaFormSchema, error) => string;
|
||||||
|
|
||||||
@property() public computeLabel?: (schema: HaFormSchema) => string;
|
@property() public computeLabel?: (schema: HaFormSchema) => string;
|
||||||
@ -64,6 +66,7 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
schema: item,
|
schema: item,
|
||||||
data: getValue(this.data, item),
|
data: getValue(this.data, item),
|
||||||
label: this._computeLabel(item),
|
label: this._computeLabel(item),
|
||||||
|
disabled: this.disabled,
|
||||||
})}
|
})}
|
||||||
`;
|
`;
|
||||||
})}
|
})}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user