Skip to content

Commit

Permalink
Add title to stack editor UI (#21328)
Browse files Browse the repository at this point in the history
Add title to stack editor UI
  • Loading branch information
karwosts authored Jul 8, 2024
1 parent bef53ae commit e7a749e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
34 changes: 9 additions & 25 deletions src/panels/lovelace/editor/config-elements/hui-grid-card-editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { html, nothing } from "lit";
import { customElement } from "lit/decorators";
import {
any,
Expand All @@ -11,8 +10,10 @@ import {
optional,
string,
} from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type {
HaFormSchema,
SchemaUnion,
} from "../../../../components/ha-form/types";
import type { GridCardConfig } from "../../cards/types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { HuiStackCardEditor } from "./hui-stack-card-editor";
Expand Down Expand Up @@ -49,35 +50,18 @@ const SCHEMA = [

@customElement("hui-grid-card-editor")
export class HuiGridCardEditor extends HuiStackCardEditor {
protected _schema: readonly HaFormSchema[] = SCHEMA;

public setConfig(config: Readonly<GridCardConfig>): void {
assert(config, cardConfigStruct);
this._config = config;
}

protected render() {
if (!this.hass || !this._config) {
return nothing;
}

const data = { square: true, ...this._config };

return html`
<ha-form
.hass=${this.hass}
.data=${data}
.schema=${SCHEMA}
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
></ha-form>
${super.render()}
`;
}

private _valueChanged(ev: CustomEvent): void {
fireEvent(this, "config-changed", { config: ev.detail.value });
protected formData(): object {
return { square: true, ...this._config };
}

private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) =>
protected _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) =>
this.hass!.localize(`ui.panel.lovelace.editor.card.grid.${schema.name}`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import {
optional,
string,
} from "superstruct";
import type {
HaFormSchema,
SchemaUnion,
} from "../../../../components/ha-form/types";
import { storage } from "../../../../common/decorators/storage";
import { HASSDomEvent, fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/ha-icon-button";
Expand All @@ -46,6 +50,13 @@ const cardConfigStruct = assign(
})
);

const SCHEMA = [
{
name: "title",
selector: { text: {} },
},
] as const;

@customElement("hui-stack-card-editor")
export class HuiStackCardEditor
extends LitElement
Expand All @@ -71,6 +82,8 @@ export class HuiStackCardEditor

@state() protected _guiModeAvailable? = true;

protected _schema: readonly HaFormSchema[] = SCHEMA;

@query("hui-card-element-editor")
protected _cardEditorEl?: HuiCardElementEditor;

Expand All @@ -83,6 +96,10 @@ export class HuiStackCardEditor
this._cardEditorEl?.focusYamlEditor();
}

protected formData(): object {
return this._config!;
}

protected render() {
if (!this.hass || !this._config) {
return nothing;
Expand All @@ -93,6 +110,13 @@ export class HuiStackCardEditor
const isGuiMode = !this._cardEditorEl || this._GUImode;

return html`
<ha-form
.hass=${this.hass}
.data=${this.formData()}
.schema=${this._schema}
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
></ha-form>
<div class="card-config">
<div class="toolbar">
<paper-tabs
Expand Down Expand Up @@ -285,6 +309,15 @@ export class HuiStackCardEditor
}
}

protected _valueChanged(ev: CustomEvent): void {
fireEvent(this, "config-changed", { config: ev.detail.value });
}

protected _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) =>
this.hass!.localize(
`ui.panel.lovelace.editor.card.${this._config!.type}.${schema.name}`
);

static get styles(): CSSResultGroup {
return [
configElementStyle,
Expand Down
2 changes: 2 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5852,6 +5852,7 @@
},
"horizontal-stack": {
"name": "Horizontal stack",
"title": "[%key:ui::panel::lovelace::editor::card::grid::title%]",
"description": "The Horizontal stack card allows you to stack together multiple cards, so they always sit next to each other in the space of one column."
},
"humidifier": {
Expand Down Expand Up @@ -5988,6 +5989,7 @@
},
"vertical-stack": {
"name": "Vertical stack",
"title": "[%key:ui::panel::lovelace::editor::card::grid::title%]",
"description": "The Vertical stack card allows you to group multiple cards so they always sit in the same column."
},
"weather-forecast": {
Expand Down

0 comments on commit e7a749e

Please sign in to comment.