mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-17 14:26:35 +00:00
Grid Card to HA Form (#11798)
This commit is contained in:
parent
0113cc3cf6
commit
9ae1f01ad6
@ -12,8 +12,8 @@ import {
|
|||||||
string,
|
string,
|
||||||
} from "superstruct";
|
} from "superstruct";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
|
import type { HaFormSchema } from "../../../../components/ha-form/types";
|
||||||
import { GridCardConfig } from "../../cards/types";
|
import type { GridCardConfig } from "../../cards/types";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { HuiStackCardEditor } from "./hui-stack-card-editor";
|
import { HuiStackCardEditor } from "./hui-stack-card-editor";
|
||||||
|
|
||||||
@ -27,6 +27,17 @@ const cardConfigStruct = assign(
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const SCHEMA: HaFormSchema[] = [
|
||||||
|
{
|
||||||
|
type: "grid",
|
||||||
|
name: "",
|
||||||
|
schema: [
|
||||||
|
{ name: "columns", selector: { number: { min: 1, mode: "box" } } },
|
||||||
|
{ name: "square", selector: { boolean: {} } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
@customElement("hui-grid-card-editor")
|
@customElement("hui-grid-card-editor")
|
||||||
export class HuiGridCardEditor extends HuiStackCardEditor {
|
export class HuiGridCardEditor extends HuiStackCardEditor {
|
||||||
public setConfig(config: Readonly<GridCardConfig>): void {
|
public setConfig(config: Readonly<GridCardConfig>): void {
|
||||||
@ -34,80 +45,31 @@ export class HuiGridCardEditor extends HuiStackCardEditor {
|
|||||||
this._config = config;
|
this._config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
get _columns(): number {
|
|
||||||
return this._config!.columns || 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
get _square(): boolean {
|
|
||||||
return this._config!.square ?? true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!this.hass || !this._config) {
|
if (!this.hass || !this._config) {
|
||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const data = { square: true, columns: 3, ...this._config };
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div class="card-config">
|
<ha-form
|
||||||
<div class="side-by-side">
|
.hass=${this.hass}
|
||||||
<paper-input
|
.data=${data}
|
||||||
.label="${this.hass.localize(
|
.schema=${SCHEMA}
|
||||||
"ui.panel.lovelace.editor.card.grid.columns"
|
.computeLabel=${this._computeLabelCallback}
|
||||||
)} (${this.hass.localize(
|
@value-changed=${this._valueChanged}
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
></ha-form>
|
||||||
)})"
|
|
||||||
type="number"
|
|
||||||
.value=${this._columns}
|
|
||||||
.configValue=${"columns"}
|
|
||||||
@value-changed=${this._handleColumnsChanged}
|
|
||||||
></paper-input>
|
|
||||||
<ha-formfield
|
|
||||||
.label=${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.grid.square"
|
|
||||||
)}
|
|
||||||
.dir=${computeRTLDirection(this.hass)}
|
|
||||||
>
|
|
||||||
<ha-switch
|
|
||||||
.checked=${this._square}
|
|
||||||
.configValue=${"square"}
|
|
||||||
@change=${this._handleSquareChanged}
|
|
||||||
></ha-switch>
|
|
||||||
</ha-formfield>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
${super.render()}
|
${super.render()}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _handleColumnsChanged(ev): void {
|
private _valueChanged(ev: CustomEvent): void {
|
||||||
if (!this._config) {
|
fireEvent(this, "config-changed", { config: ev.detail.value });
|
||||||
return;
|
|
||||||
}
|
|
||||||
const value = Number(ev.target.value);
|
|
||||||
if (this._columns === value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!ev.target.value) {
|
|
||||||
this._config = { ...this._config };
|
|
||||||
delete this._config.columns;
|
|
||||||
} else {
|
|
||||||
this._config = {
|
|
||||||
...this._config,
|
|
||||||
columns: value,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
fireEvent(this, "config-changed", { config: this._config });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _handleSquareChanged(ev): void {
|
private _computeLabelCallback = (schema: HaFormSchema) =>
|
||||||
if (!this._config || this._square === ev.target.checked) {
|
this.hass!.localize(`ui.panel.lovelace.editor.card.grid.${schema.name}`);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fireEvent(this, "config-changed", {
|
|
||||||
config: { ...this._config, square: ev.target.checked },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user