From 6c4e987a2462a943b8f19a95a91419396e0d7d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sun, 21 Nov 2021 18:15:38 +0100 Subject: [PATCH] Make ha-chip-set slot-able (#10647) --- .../{demo-ha-chip.ts => demo-ha-chips.ts} | 31 +++++++++++++-- src/components/ha-chip-set.ts | 39 +++++-------------- src/components/ha-chip.ts | 11 +----- .../ha-device-automation-card.ts | 25 +++++++++--- .../ha-device-automation-dialog.ts | 2 +- 5 files changed, 58 insertions(+), 50 deletions(-) rename gallery/src/demos/{demo-ha-chip.ts => demo-ha-chips.ts} (59%) diff --git a/gallery/src/demos/demo-ha-chip.ts b/gallery/src/demos/demo-ha-chips.ts similarity index 59% rename from gallery/src/demos/demo-ha-chip.ts rename to gallery/src/demos/demo-ha-chips.ts index 8344d4eb64..d1dcc2ae7d 100644 --- a/gallery/src/demos/demo-ha-chip.ts +++ b/gallery/src/demos/demo-ha-chips.ts @@ -3,6 +3,7 @@ import { css, html, LitElement, TemplateResult } from "lit"; import { customElement } from "lit/decorators"; import "../../../src/components/ha-card"; import "../../../src/components/ha-chip"; +import "../../../src/components/ha-chip-set"; import "../../../src/components/ha-svg-icon"; const chips: { @@ -22,8 +23,8 @@ const chips: { }, ]; -@customElement("demo-ha-chip") -export class DemoHaChip extends LitElement { +@customElement("demo-ha-chips") +export class DemoHaChips extends LitElement { protected render(): TemplateResult { return html` @@ -41,6 +42,23 @@ export class DemoHaChip extends LitElement { )} + +
+ + ${chips.map( + (chip) => html` + + ${chip.icon + ? html` + ` + : ""} + ${chip.content} + + ` + )} + +
+
`; } @@ -50,12 +68,19 @@ export class DemoHaChip extends LitElement { max-width: 600px; margin: 24px auto; } + ha-chip { + margin-bottom: 4px; + } + .card-content { + display: flex; + flex-direction: column; + } `; } } declare global { interface HTMLElementTagNameMap { - "demo-ha-chip": DemoHaChip; + "demo-ha-chips": DemoHaChips; } } diff --git a/src/components/ha-chip-set.ts b/src/components/ha-chip-set.ts index 2b2bb6a9e9..2e659c8353 100644 --- a/src/components/ha-chip-set.ts +++ b/src/components/ha-chip-set.ts @@ -8,52 +8,31 @@ import { TemplateResult, unsafeCSS, } from "lit"; -import { customElement, property } from "lit/decorators"; -import { fireEvent } from "../common/dom/fire_event"; -import "./ha-chip"; - -declare global { - // for fire event - interface HASSDomEvents { - "chip-clicked": { index: string }; - } -} +import { customElement } from "lit/decorators"; @customElement("ha-chip-set") export class HaChipSet extends LitElement { - @property() public items = []; - protected render(): TemplateResult { - if (this.items.length === 0) { - return html``; - } return html`
- ${this.items.map( - (item, idx) => - html` - - ${item} - - ` - )} +
`; } - private _handleClick(ev): void { - fireEvent(this, "chip-clicked", { - index: ev.currentTarget.index, - }); - } - static get styles(): CSSResultGroup { return css` ${unsafeCSS(chipStyles)} - ha-chip { + slot::slotted(ha-chip) { margin: 4px; } + slot::slotted(ha-chip:first-of-type) { + margin-left: -4px; + } + slot::slotted(ha-chip:last-of-type) { + margin-right: -4px; + } `; } } diff --git a/src/components/ha-chip.ts b/src/components/ha-chip.ts index bbb934b981..9f58e3c53c 100644 --- a/src/components/ha-chip.ts +++ b/src/components/ha-chip.ts @@ -10,22 +10,13 @@ import { } from "lit"; import { customElement, property } from "lit/decorators"; -declare global { - // for fire event - interface HASSDomEvents { - "chip-clicked": { index: string }; - } -} - @customElement("ha-chip") export class HaChip extends LitElement { - @property() public index = 0; - @property({ type: Boolean }) public hasIcon = false; protected render(): TemplateResult { return html` -
+
${this.hasIcon ? html`
diff --git a/src/panels/config/devices/device-detail/ha-device-automation-card.ts b/src/panels/config/devices/device-detail/ha-device-automation-card.ts index 741c426f3d..47577805c3 100644 --- a/src/panels/config/devices/device-detail/ha-device-automation-card.ts +++ b/src/panels/config/devices/device-detail/ha-device-automation-card.ts @@ -1,6 +1,8 @@ import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { property } from "lit/decorators"; +import { fireEvent } from "../../../../common/dom/fire_event"; import "../../../../components/ha-card"; +import "../../../../components/ha-chip"; import "../../../../components/ha-chip-set"; import { showAutomationEditor } from "../../../../data/automation"; import { @@ -10,6 +12,12 @@ import { import { showScriptEditor } from "../../../../data/script"; import { HomeAssistant } from "../../../../types"; +declare global { + interface HASSDomEvents { + "entry-selected": undefined; + } +} + export abstract class HaDeviceAutomationCard< T extends DeviceAutomation > extends LitElement { @@ -55,29 +63,34 @@ export abstract class HaDeviceAutomationCard< return html`

${this.hass.localize(this.headerKey)}

- - this._localizeDeviceAutomation(this.hass, automation) + + ${this.automations.map( + (automation, idx) => + html` + + ${this._localizeDeviceAutomation(this.hass, automation)} + + ` )} - >
`; } private _handleAutomationClicked(ev: CustomEvent) { - const automation = this.automations[ev.detail.index]; + const automation = this.automations[(ev.currentTarget as any).index]; if (!automation) { return; } if (this.script) { showScriptEditor({ sequence: [automation as DeviceAction] }); + fireEvent(this, "entry-selected"); return; } const data = {}; data[this.type] = [automation]; showAutomationEditor(data); + fireEvent(this, "entry-selected"); } static get styles(): CSSResultGroup { diff --git a/src/panels/config/devices/device-detail/ha-device-automation-dialog.ts b/src/panels/config/devices/device-detail/ha-device-automation-dialog.ts index eb18201a25..b42d2f069b 100644 --- a/src/panels/config/devices/device-detail/ha-device-automation-dialog.ts +++ b/src/panels/config/devices/device-detail/ha-device-automation-dialog.ts @@ -91,7 +91,7 @@ export class DialogDeviceAutomation extends LitElement { }.create` )} > -
+
${this._triggers.length || this._conditions.length || this._actions.length