Skip to content

Commit

Permalink
Add a label filter (#23081)
Browse files Browse the repository at this point in the history
* Label filter

* adjust height

* ci

* Update src/components/ha-filter-labels.ts

---------

Co-authored-by: Petar Petrov <[email protected]>
  • Loading branch information
silamon and MindFreeze authored Dec 2, 2024
1 parent 2c13c5a commit a0f3e4f
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions src/components/ha-filter-labels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "@material/mwc-list/mwc-list";
import type { SelectedDetail } from "@material/mwc-list";
import "@material/mwc-menu/mwc-menu-surface";
import memoizeOne from "memoize-one";
import { mdiCog, mdiFilterVariantRemove } from "@mdi/js";
import type { UnsubscribeFunc } from "home-assistant-js-websocket";
import type { CSSResultGroup } from "lit";
Expand All @@ -21,6 +22,8 @@ import "./ha-icon";
import "./ha-label";
import "./ha-icon-button";
import "./ha-list-item";
import "./search-input-outlined";
import { stringCompare } from "../common/string/compare";

@customElement("ha-filter-labels")
export class HaFilterLabels extends SubscribeMixin(LitElement) {
Expand All @@ -36,6 +39,8 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {

@state() private _shouldRender = false;

@state() private _filter?: string;

protected hassSubscribe(): (UnsubscribeFunc | Promise<UnsubscribeFunc>)[] {
return [
subscribeLabelRegistry(this.hass.connection, (labels) => {
Expand All @@ -44,6 +49,25 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
];
}

private _filteredLabels = memoizeOne(
// `_value` used to recalculate the memoization when the selection changes
(labels: LabelRegistryEntry[], filter: string | undefined, _value) =>
labels
.filter(
(label) =>
!filter ||
label.name.toLowerCase().includes(filter) ||
label.label_id.toLowerCase().includes(filter)
)
.sort((a, b) =>
stringCompare(
a.name || a.label_id,
b.name || b.label_id,
this.hass.locale.language
)
)
);

protected render() {
return html`
<ha-expansion-panel
Expand All @@ -63,14 +87,19 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
: nothing}
</div>
${this._shouldRender
? html`
? html`<search-input-outlined
.hass=${this.hass}
.filter=${this._filter}
@value-changed=${this._handleSearchChange}
>
</search-input-outlined>
<mwc-list
@selected=${this._labelSelected}
class="ha-scrollbar"
multi
>
${repeat(
this._labels,
this._filteredLabels(this._labels, this._filter, this.value),
(label) => label.label_id,
(label) => {
const color = label.color
Expand All @@ -93,8 +122,7 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
</ha-check-list-item>`;
}
)}
</mwc-list>
`
</mwc-list> `
: nothing}
</ha-expansion-panel>
${this.expanded
Expand All @@ -115,7 +143,7 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
setTimeout(() => {
if (!this.expanded) return;
this.renderRoot.querySelector("mwc-list")!.style.height =
`${this.clientHeight - (49 + 48)}px`;
`${this.clientHeight - (49 + 48 + 32)}px`;
}, 300);
}
}
Expand All @@ -132,6 +160,10 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
this.expanded = ev.detail.expanded;
}

private _handleSearchChange(ev: CustomEvent) {
this._filter = ev.detail.value.toLowerCase();
}

private async _labelSelected(ev: CustomEvent<SelectedDetail<Set<number>>>) {
if (!ev.detail.index.size) {
fireEvent(this, "data-table-filter-changed", {
Expand Down Expand Up @@ -218,6 +250,10 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
right: 0;
left: 0;
}
search-input-outlined {
display: block;
padding: 0 8px;
}
`,
];
}
Expand Down

0 comments on commit a0f3e4f

Please sign in to comment.