UI editor for shopping-list card (#2227)

* Add UI editor for `shopping-list` card

* Cleanup

* Export config

* Remove `id`
This commit is contained in:
Ian Richardson 2018-12-11 14:23:10 -06:00 committed by Paulus Schoutsen
parent 8679f10f10
commit 88f0ebf75d
2 changed files with 90 additions and 2 deletions

View File

@ -9,7 +9,7 @@ import "../../../components/ha-icon";
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
import { HomeAssistant } from "../../../types";
import { LovelaceCard } from "../types";
import { LovelaceCard, LovelaceCardEditor } from "../types";
import { LovelaceCardConfig } from "../../../data/lovelace";
import {
fetchItems,
@ -19,12 +19,20 @@ import {
addItem,
} from "../../../data/shopping-list";
interface Config extends LovelaceCardConfig {
export interface Config extends LovelaceCardConfig {
title?: string;
}
class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement)
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;
private _config?: Config;
private _uncheckedItems?: ShoppingListItem[];
@ -33,6 +41,7 @@ class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement)
static get properties() {
return {
hass: {},
_config: {},
_uncheckedItems: {},
_checkedItems: {},

View File

@ -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);