mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 11:46:42 +00:00
UI editor for shopping-list
card (#2227)
* Add UI editor for `shopping-list` card * Cleanup * Export config * Remove `id`
This commit is contained in:
parent
8679f10f10
commit
88f0ebf75d
@ -9,7 +9,7 @@ import "../../../components/ha-icon";
|
|||||||
|
|
||||||
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
|
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { LovelaceCard } from "../types";
|
import { LovelaceCard, LovelaceCardEditor } from "../types";
|
||||||
import { LovelaceCardConfig } from "../../../data/lovelace";
|
import { LovelaceCardConfig } from "../../../data/lovelace";
|
||||||
import {
|
import {
|
||||||
fetchItems,
|
fetchItems,
|
||||||
@ -19,12 +19,20 @@ import {
|
|||||||
addItem,
|
addItem,
|
||||||
} from "../../../data/shopping-list";
|
} from "../../../data/shopping-list";
|
||||||
|
|
||||||
interface Config extends LovelaceCardConfig {
|
export interface Config extends LovelaceCardConfig {
|
||||||
title?: string;
|
title?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement)
|
class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement)
|
||||||
implements LovelaceCard {
|
implements LovelaceCard {
|
||||||
|
public static async getConfigElement(): Promise<LovelaceCardEditor> {
|
||||||
|
await import("../editor/config-elements/hui-shopping-list-editor");
|
||||||
|
return document.createElement("hui-shopping-list-card-editor");
|
||||||
|
}
|
||||||
|
public static getStubConfig(): object {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
public hass?: HomeAssistant;
|
public hass?: HomeAssistant;
|
||||||
private _config?: Config;
|
private _config?: Config;
|
||||||
private _uncheckedItems?: ShoppingListItem[];
|
private _uncheckedItems?: ShoppingListItem[];
|
||||||
@ -33,6 +41,7 @@ class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
|
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
|
hass: {},
|
||||||
_config: {},
|
_config: {},
|
||||||
_uncheckedItems: {},
|
_uncheckedItems: {},
|
||||||
_checkedItems: {},
|
_checkedItems: {},
|
||||||
|
@ -0,0 +1,79 @@
|
|||||||
|
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
|
||||||
|
import { TemplateResult } from "lit-html";
|
||||||
|
import "@polymer/paper-input/paper-input";
|
||||||
|
|
||||||
|
import { struct } from "../../common/structs/struct";
|
||||||
|
import { EntitiesEditorEvent, EditorTarget } from "../types";
|
||||||
|
import { hassLocalizeLitMixin } from "../../../../mixins/lit-localize-mixin";
|
||||||
|
import { HomeAssistant } from "../../../../types";
|
||||||
|
import { LovelaceCardEditor } from "../../types";
|
||||||
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
|
import { Config } from "../../cards/hui-shopping-list-card";
|
||||||
|
|
||||||
|
const cardConfigStruct = struct({
|
||||||
|
type: "string",
|
||||||
|
title: "string?",
|
||||||
|
});
|
||||||
|
|
||||||
|
export class HuiShoppingListEditor extends hassLocalizeLitMixin(LitElement)
|
||||||
|
implements LovelaceCardEditor {
|
||||||
|
public hass?: HomeAssistant;
|
||||||
|
private _config?: Config;
|
||||||
|
|
||||||
|
public setConfig(config: Config): void {
|
||||||
|
config = cardConfigStruct(config);
|
||||||
|
|
||||||
|
this._config = { type: "shopping-list", ...config };
|
||||||
|
}
|
||||||
|
|
||||||
|
static get properties(): PropertyDeclarations {
|
||||||
|
return { hass: {}, _config: {} };
|
||||||
|
}
|
||||||
|
|
||||||
|
get _title(): string {
|
||||||
|
return this._config!.title || "";
|
||||||
|
}
|
||||||
|
|
||||||
|
protected render(): TemplateResult {
|
||||||
|
if (!this.hass) {
|
||||||
|
return html``;
|
||||||
|
}
|
||||||
|
|
||||||
|
return html`
|
||||||
|
<div class="card-config">
|
||||||
|
<paper-input
|
||||||
|
label="Title"
|
||||||
|
value="${this._title}"
|
||||||
|
.configValue="${"title"}"
|
||||||
|
@value-changed="${this._valueChanged}"
|
||||||
|
></paper-input>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _valueChanged(ev: EntitiesEditorEvent): void {
|
||||||
|
if (!this._config || !this.hass) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const target = ev.target! as EditorTarget;
|
||||||
|
|
||||||
|
if (this[`_${target.configValue}`] === target.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (target.configValue) {
|
||||||
|
this._config = {
|
||||||
|
...this._config,
|
||||||
|
[target.configValue!]: target.value,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
fireEvent(this, "config-changed", { config: this._config });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"hui-shopping-list-card-editor": HuiShoppingListEditor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define("hui-shopping-list-card-editor", HuiShoppingListEditor);
|
Loading…
x
Reference in New Issue
Block a user