-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add area filter selector for default dashboard (#18779)
Co-authored-by: Bram Kragten <[email protected]>
- Loading branch information
1 parent
9b20e1c
commit 7727f34
Showing
11 changed files
with
444 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { mdiChevronRight, mdiSofa } from "@mdi/js"; | ||
import { CSSResultGroup, LitElement, TemplateResult, css, html } from "lit"; | ||
import { customElement, property } from "lit/decorators"; | ||
import { fireEvent } from "../common/dom/fire_event"; | ||
import { showAreaFilterDialog } from "../dialogs/area-filter/show-area-filter-dialog"; | ||
import { HomeAssistant } from "../types"; | ||
import "./ha-svg-icon"; | ||
import "./ha-textfield"; | ||
|
||
export type AreaFilterValue = { | ||
hidden?: string[]; | ||
order?: string[]; | ||
}; | ||
|
||
@customElement("ha-area-filter") | ||
export class HaAreaPicker extends LitElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@property() public label?: string; | ||
|
||
@property({ attribute: false }) public value?: AreaFilterValue; | ||
|
||
@property() public helper?: string; | ||
|
||
@property({ type: Boolean }) public disabled = false; | ||
|
||
@property({ type: Boolean }) public required = false; | ||
|
||
protected render(): TemplateResult { | ||
const allAreasCount = Object.keys(this.hass.areas).length; | ||
const hiddenAreasCount = this.value?.hidden?.length ?? 0; | ||
|
||
const description = | ||
hiddenAreasCount === 0 | ||
? this.hass.localize("ui.components.area-filter.all_areas") | ||
: allAreasCount === hiddenAreasCount | ||
? this.hass.localize("ui.components.area-filter.no_areas") | ||
: this.hass.localize("ui.components.area-filter.area_count", { | ||
count: allAreasCount - hiddenAreasCount, | ||
}); | ||
|
||
return html` | ||
<ha-list-item | ||
tabindex="0" | ||
role="button" | ||
hasMeta | ||
twoline | ||
graphic="icon" | ||
@click=${this._edit} | ||
@keydown=${this._edit} | ||
.disabled=${this.disabled} | ||
> | ||
<ha-svg-icon slot="graphic" .path=${mdiSofa}></ha-svg-icon> | ||
<span>${this.label}</span> | ||
<span slot="secondary">${description}</span> | ||
<ha-svg-icon | ||
slot="meta" | ||
.label=${this.hass.localize("ui.common.edit")} | ||
.path=${mdiChevronRight} | ||
></ha-svg-icon> | ||
</ha-list-item> | ||
`; | ||
} | ||
|
||
private async _edit(ev) { | ||
if (ev.defaultPrevented) { | ||
return; | ||
} | ||
if (ev.type === "keydown" && ev.key !== "Enter" && ev.key !== " ") { | ||
return; | ||
} | ||
ev.preventDefault(); | ||
ev.stopPropagation(); | ||
const value = await showAreaFilterDialog(this, { | ||
title: this.label, | ||
initialValue: this.value, | ||
}); | ||
if (!value) return; | ||
fireEvent(this, "value-changed", { value }); | ||
} | ||
|
||
static get styles(): CSSResultGroup { | ||
return css` | ||
ha-list-item { | ||
--mdc-list-side-padding-left: 8px; | ||
--mdc-list-side-padding-right: 8px; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"ha-area-filter": HaAreaPicker; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { LitElement, html } from "lit"; | ||
import { customElement, property } from "lit/decorators"; | ||
import type { AreaFilterSelector } from "../../data/selector"; | ||
import { HomeAssistant } from "../../types"; | ||
import "../ha-area-filter"; | ||
|
||
@customElement("ha-selector-area_filter") | ||
export class HaAreaFilterSelector extends LitElement { | ||
@property() public hass!: HomeAssistant; | ||
|
||
@property() public selector!: AreaFilterSelector; | ||
|
||
@property() public value?: any; | ||
|
||
@property() public label?: string; | ||
|
||
@property() public helper?: string; | ||
|
||
@property({ type: Boolean }) public disabled = false; | ||
|
||
@property({ type: Boolean }) public required = true; | ||
|
||
protected render() { | ||
return html` | ||
<ha-area-filter | ||
.hass=${this.hass} | ||
.value=${this.value} | ||
.label=${this.label} | ||
.helper=${this.helper} | ||
.disabled=${this.disabled} | ||
.required=${this.required} | ||
></ha-area-filter> | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"ha-selector-area_filter": HaAreaFilterSelector; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.