mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-14 12:56:37 +00:00
Add toggle to show_hide optional fields in add-on config (#8430)
This commit is contained in:
parent
4e7f68a86c
commit
97f9df2f2d
@ -15,11 +15,15 @@ import {
|
|||||||
query,
|
query,
|
||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
import { fireEvent } from "../../../../src/common/dom/fire_event";
|
import { fireEvent } from "../../../../src/common/dom/fire_event";
|
||||||
import "../../../../src/components/buttons/ha-progress-button";
|
import "../../../../src/components/buttons/ha-progress-button";
|
||||||
import "../../../../src/components/ha-button-menu";
|
import "../../../../src/components/ha-button-menu";
|
||||||
import "../../../../src/components/ha-card";
|
import "../../../../src/components/ha-card";
|
||||||
import "../../../../src/components/ha-form/ha-form";
|
import "../../../../src/components/ha-form/ha-form";
|
||||||
|
import type { HaFormSchema } from "../../../../src/components/ha-form/ha-form";
|
||||||
|
import "../../../../src/components/ha-formfield";
|
||||||
|
import "../../../../src/components/ha-switch";
|
||||||
import "../../../../src/components/ha-yaml-editor";
|
import "../../../../src/components/ha-yaml-editor";
|
||||||
import type { HaYamlEditor } from "../../../../src/components/ha-yaml-editor";
|
import type { HaYamlEditor } from "../../../../src/components/ha-yaml-editor";
|
||||||
import {
|
import {
|
||||||
@ -48,6 +52,8 @@ class HassioAddonConfig extends LitElement {
|
|||||||
|
|
||||||
@internalProperty() private _canShowSchema = false;
|
@internalProperty() private _canShowSchema = false;
|
||||||
|
|
||||||
|
@internalProperty() private _showOptional = false;
|
||||||
|
|
||||||
@internalProperty() private _error?: string;
|
@internalProperty() private _error?: string;
|
||||||
|
|
||||||
@internalProperty() private _options?: Record<string, unknown>;
|
@internalProperty() private _options?: Record<string, unknown>;
|
||||||
@ -56,7 +62,21 @@ class HassioAddonConfig extends LitElement {
|
|||||||
|
|
||||||
@query("ha-yaml-editor") private _editor?: HaYamlEditor;
|
@query("ha-yaml-editor") private _editor?: HaYamlEditor;
|
||||||
|
|
||||||
|
private _filteredShchema = memoizeOne(
|
||||||
|
(options: Record<string, unknown>, schema: HaFormSchema[]) => {
|
||||||
|
return schema.filter((entry) => entry.name in options || entry.required);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
|
const showForm =
|
||||||
|
!this._yamlMode && this._canShowSchema && this.addon.schema;
|
||||||
|
const hasHiddenOptions =
|
||||||
|
showForm &&
|
||||||
|
JSON.stringify(this.addon.schema) !==
|
||||||
|
JSON.stringify(
|
||||||
|
this._filteredShchema(this.addon.options, this.addon.schema!)
|
||||||
|
);
|
||||||
return html`
|
return html`
|
||||||
<h1>${this.addon.name}</h1>
|
<h1>${this.addon.name}</h1>
|
||||||
<ha-card>
|
<ha-card>
|
||||||
@ -78,11 +98,16 @@ class HassioAddonConfig extends LitElement {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
${!this._yamlMode && this._canShowSchema && this.addon.schema
|
${showForm
|
||||||
? html`<ha-form
|
? html`<ha-form
|
||||||
.data=${this._options!}
|
.data=${this._options!}
|
||||||
@value-changed=${this._configChanged}
|
@value-changed=${this._configChanged}
|
||||||
.schema=${this.addon.schema}
|
.schema=${this._showOptional
|
||||||
|
? this.addon.schema!
|
||||||
|
: this._filteredShchema(
|
||||||
|
this.addon.options,
|
||||||
|
this.addon.schema!
|
||||||
|
)}
|
||||||
></ha-form>`
|
></ha-form>`
|
||||||
: html` <ha-yaml-editor
|
: html` <ha-yaml-editor
|
||||||
@value-changed=${this._configChanged}
|
@value-changed=${this._configChanged}
|
||||||
@ -94,6 +119,18 @@ class HassioAddonConfig extends LitElement {
|
|||||||
? ""
|
? ""
|
||||||
: html` <div class="errors">Invalid YAML</div> `}
|
: html` <div class="errors">Invalid YAML</div> `}
|
||||||
</div>
|
</div>
|
||||||
|
${hasHiddenOptions
|
||||||
|
? html`<ha-formfield
|
||||||
|
class="show-additional"
|
||||||
|
label="Show unused optional configuration options"
|
||||||
|
>
|
||||||
|
<ha-switch
|
||||||
|
@change=${this._toggleOptional}
|
||||||
|
.checked=${this._showOptional}
|
||||||
|
>
|
||||||
|
</ha-switch>
|
||||||
|
</ha-formfield>`
|
||||||
|
: ""}
|
||||||
<div class="card-actions right">
|
<div class="card-actions right">
|
||||||
<ha-progress-button
|
<ha-progress-button
|
||||||
@click=${this._saveTapped}
|
@click=${this._saveTapped}
|
||||||
@ -146,6 +183,10 @@ class HassioAddonConfig extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _toggleOptional() {
|
||||||
|
this._showOptional = !this._showOptional;
|
||||||
|
}
|
||||||
|
|
||||||
private _configChanged(ev): void {
|
private _configChanged(ev): void {
|
||||||
if (this.addon.schema && this._canShowSchema && !this._yamlMode) {
|
if (this.addon.schema && this._canShowSchema && !this._yamlMode) {
|
||||||
this._valid = true;
|
this._valid = true;
|
||||||
@ -275,6 +316,10 @@ class HassioAddonConfig extends LitElement {
|
|||||||
.card-actions.right {
|
.card-actions.right {
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.show-additional {
|
||||||
|
padding: 16px;
|
||||||
|
}
|
||||||
`,
|
`,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user