Skip to content

Commit

Permalink
Add clear filter button to individual filters (#20343)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Apr 2, 2024
1 parent 1e2c1d1 commit 59b6621
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 30 deletions.
24 changes: 21 additions & 3 deletions src/components/ha-filter-blueprints.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { SelectedDetail } from "@material/mwc-list";
import "@material/mwc-menu/mwc-menu-surface";
import { mdiFilterVariantRemove } from "@mdi/js";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event";
import { Blueprints, fetchBlueprints } from "../data/blueprint";
import { findRelated, RelatedResult } from "../data/search";
import type { HomeAssistant } from "../types";
import { haStyleScrollbar } from "../resources/styles";
import { Blueprints, fetchBlueprints } from "../data/blueprint";
import type { HomeAssistant } from "../types";

@customElement("ha-filter-blueprints")
export class HaFilterBlueprints extends LitElement {
Expand Down Expand Up @@ -35,7 +36,11 @@ export class HaFilterBlueprints extends LitElement {
<div slot="header" class="header">
${this.hass.localize("ui.panel.config.blueprint.caption")}
${this.value?.length
? html`<div class="badge">${this.value?.length}</div>`
? html`<div class="badge">${this.value?.length}</div>
<ha-icon-button
.path=${mdiFilterVariantRemove}
@click=${this._clearFilter}
></ha-icon-button>`
: nothing}
</div>
${this._blueprints && this._shouldRender
Expand Down Expand Up @@ -128,6 +133,15 @@ export class HaFilterBlueprints extends LitElement {
});
}

private _clearFilter(ev) {
ev.preventDefault();
this.value = undefined;
fireEvent(this, "data-table-filter-changed", {
value: undefined,
items: undefined,
});
}

static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
Expand All @@ -147,6 +161,10 @@ export class HaFilterBlueprints extends LitElement {
display: flex;
align-items: center;
}
.header ha-icon-button {
margin-inline-start: auto;
margin-inline-end: 8px;
}
.badge {
display: inline-block;
margin-left: 8px;
Expand Down
20 changes: 19 additions & 1 deletion src/components/ha-filter-categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ActionDetail, SelectedDetail } from "@material/mwc-list";
import {
mdiDelete,
mdiDotsVertical,
mdiFilterVariantRemove,
mdiPencil,
mdiPlus,
mdiTag,
Expand Down Expand Up @@ -68,7 +69,11 @@ export class HaFilterCategories extends SubscribeMixin(LitElement) {
<div slot="header" class="header">
${this.hass.localize("ui.panel.config.category.caption")}
${this.value?.length
? html`<div class="badge">${this.value?.length}</div>`
? html`<div class="badge">${this.value?.length}</div>
<ha-icon-button
.path=${mdiFilterVariantRemove}
@click=${this._clearFilter}
></ha-icon-button>`
: nothing}
</div>
${this._shouldRender
Expand Down Expand Up @@ -254,6 +259,15 @@ export class HaFilterCategories extends SubscribeMixin(LitElement) {
});
}

private _clearFilter(ev) {
ev.preventDefault();
this.value = undefined;
fireEvent(this, "data-table-filter-changed", {
value: undefined,
items: undefined,
});
}

static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
Expand All @@ -274,6 +288,10 @@ export class HaFilterCategories extends SubscribeMixin(LitElement) {
display: flex;
align-items: center;
}
.header ha-icon-button {
margin-inline-start: auto;
margin-inline-end: 8px;
}
.badge {
display: inline-block;
margin-left: 8px;
Expand Down
20 changes: 19 additions & 1 deletion src/components/ha-filter-devices.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mdiFilterVariantRemove } from "@mdi/js";
import {
css,
CSSResultGroup,
Expand Down Expand Up @@ -54,7 +55,11 @@ export class HaFilterDevices extends LitElement {
<div slot="header" class="header">
${this.hass.localize("ui.panel.config.devices.caption")}
${this.value?.length
? html`<div class="badge">${this.value?.length}</div>`
? html`<div class="badge">${this.value?.length}</div>
<ha-icon-button
.path=${mdiFilterVariantRemove}
@click=${this._clearFilter}
></ha-icon-button>`
: nothing}
</div>
${this._shouldRender
Expand Down Expand Up @@ -185,6 +190,15 @@ export class HaFilterDevices extends LitElement {
});
}

private _clearFilter(ev) {
ev.preventDefault();
this.value = undefined;
fireEvent(this, "data-table-filter-changed", {
value: undefined,
items: undefined,
});
}

static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
Expand All @@ -205,6 +219,10 @@ export class HaFilterDevices extends LitElement {
display: flex;
align-items: center;
}
.header ha-icon-button {
margin-inline-start: auto;
margin-inline-end: 8px;
}
.badge {
display: inline-block;
margin-left: 8px;
Expand Down
20 changes: 19 additions & 1 deletion src/components/ha-filter-entities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mdiFilterVariantRemove } from "@mdi/js";
import {
css,
CSSResultGroup,
Expand Down Expand Up @@ -55,7 +56,11 @@ export class HaFilterEntities extends LitElement {
<div slot="header" class="header">
${this.hass.localize("ui.panel.config.entities.caption")}
${this.value?.length
? html`<div class="badge">${this.value?.length}</div>`
? html`<div class="badge">${this.value?.length}</div>
<ha-icon-button
.path=${mdiFilterVariantRemove}
@click=${this._clearFilter}
></ha-icon-button>`
: nothing}
</div>
${this._shouldRender
Expand Down Expand Up @@ -204,6 +209,15 @@ export class HaFilterEntities extends LitElement {
});
}

private _clearFilter(ev) {
ev.preventDefault();
this.value = undefined;
fireEvent(this, "data-table-filter-changed", {
value: undefined,
items: undefined,
});
}

static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
Expand All @@ -223,6 +237,10 @@ export class HaFilterEntities extends LitElement {
display: flex;
align-items: center;
}
.header ha-icon-button {
margin-inline-start: auto;
margin-inline-end: 8px;
}
.badge {
display: inline-block;
margin-left: 8px;
Expand Down
25 changes: 21 additions & 4 deletions src/components/ha-filter-floor-areas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "@material/mwc-menu/mwc-menu-surface";
import { mdiTextureBox } from "@mdi/js";
import { mdiFilterVariantRemove, mdiTextureBox } from "@mdi/js";
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
Expand Down Expand Up @@ -53,9 +53,13 @@ export class HaFilterFloorAreas extends SubscribeMixin(LitElement) {
${this.hass.localize("ui.panel.config.areas.caption")}
${this.value?.areas?.length || this.value?.floors?.length
? html`<div class="badge">
${(this.value?.areas?.length || 0) +
(this.value?.floors?.length || 0)}
</div>`
${(this.value?.areas?.length || 0) +
(this.value?.floors?.length || 0)}
</div>
<ha-icon-button
.path=${mdiFilterVariantRemove}
@click=${this._clearFilter}
></ha-icon-button>`
: nothing}
</div>
${this._shouldRender
Expand Down Expand Up @@ -238,6 +242,15 @@ export class HaFilterFloorAreas extends SubscribeMixin(LitElement) {
});
}

private _clearFilter(ev) {
ev.preventDefault();
this.value = undefined;
fireEvent(this, "data-table-filter-changed", {
value: undefined,
items: undefined,
});
}

static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
Expand All @@ -257,6 +270,10 @@ export class HaFilterFloorAreas extends SubscribeMixin(LitElement) {
display: flex;
align-items: center;
}
.header ha-icon-button {
margin-inline-start: auto;
margin-inline-end: 8px;
}
.badge {
display: inline-block;
margin-left: 8px;
Expand Down
20 changes: 19 additions & 1 deletion src/components/ha-filter-integrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SelectedDetail } from "@material/mwc-list";
import { mdiFilterVariantRemove } from "@mdi/js";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { repeat } from "lit/directives/repeat";
Expand Down Expand Up @@ -38,7 +39,11 @@ export class HaFilterIntegrations extends LitElement {
<div slot="header" class="header">
${this.hass.localize("ui.panel.config.integrations.caption")}
${this.value?.length
? html`<div class="badge">${this.value?.length}</div>`
? html`<div class="badge">${this.value?.length}</div>
<ha-icon-button
.path=${mdiFilterVariantRemove}
@click=${this._clearFilter}
></ha-icon-button>`
: nothing}
</div>
${this._manifests && this._shouldRender
Expand Down Expand Up @@ -142,6 +147,15 @@ export class HaFilterIntegrations extends LitElement {
});
}

private _clearFilter(ev) {
ev.preventDefault();
this.value = undefined;
fireEvent(this, "data-table-filter-changed", {
value: undefined,
items: undefined,
});
}

static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
Expand All @@ -161,6 +175,10 @@ export class HaFilterIntegrations extends LitElement {
display: flex;
align-items: center;
}
.header ha-icon-button {
margin-inline-start: auto;
margin-inline-end: 8px;
}
.badge {
display: inline-block;
margin-left: 8px;
Expand Down
21 changes: 19 additions & 2 deletions src/components/ha-filter-labels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SelectedDetail } from "@material/mwc-list";
import "@material/mwc-menu/mwc-menu-surface";
import { mdiCog } from "@mdi/js";
import { mdiCog, mdiFilterVariantRemove } from "@mdi/js";
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { CSSResultGroup, LitElement, css, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
Expand Down Expand Up @@ -53,7 +53,11 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
<div slot="header" class="header">
${this.hass.localize("ui.panel.config.labels.caption")}
${this.value?.length
? html`<div class="badge">${this.value?.length}</div>`
? html`<div class="badge">${this.value?.length}</div>
<ha-icon-button
.path=${mdiFilterVariantRemove}
@click=${this._clearFilter}
></ha-icon-button>`
: nothing}
</div>
${this._shouldRender
Expand Down Expand Up @@ -150,6 +154,15 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
});
}

private _clearFilter(ev) {
ev.preventDefault();
this.value = undefined;
fireEvent(this, "data-table-filter-changed", {
value: undefined,
items: undefined,
});
}

static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
Expand All @@ -170,6 +183,10 @@ export class HaFilterLabels extends SubscribeMixin(LitElement) {
display: flex;
align-items: center;
}
.header ha-icon-button {
margin-inline-start: auto;
margin-inline-end: 8px;
}
.badge {
display: inline-block;
margin-left: 8px;
Expand Down
22 changes: 20 additions & 2 deletions src/components/ha-filter-states.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { SelectedDetail } from "@material/mwc-list";
import { mdiFilterVariantRemove } from "@mdi/js";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event";
import { haStyleScrollbar } from "../resources/styles";
import type { HomeAssistant } from "../types";
import "./ha-expansion-panel";
import "./ha-check-list-item";
import "./ha-expansion-panel";
import "./ha-icon";

@customElement("ha-filter-states")
Expand Down Expand Up @@ -43,7 +44,11 @@ export class HaFilterStates extends LitElement {
<div slot="header" class="header">
${this.label}
${this.value?.length
? html`<div class="badge">${this.value?.length}</div>`
? html`<div class="badge">${this.value?.length}</div>
<ha-icon-button
.path=${mdiFilterVariantRemove}
@click=${this._clearFilter}
></ha-icon-button>`
: nothing}
</div>
${this._shouldRender
Expand Down Expand Up @@ -118,6 +123,15 @@ export class HaFilterStates extends LitElement {
});
}

private _clearFilter(ev) {
ev.preventDefault();
this.value = undefined;
fireEvent(this, "data-table-filter-changed", {
value: undefined,
items: undefined,
});
}

static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
Expand All @@ -137,6 +151,10 @@ export class HaFilterStates extends LitElement {
display: flex;
align-items: center;
}
.header ha-icon-button {
margin-inline-start: auto;
margin-inline-end: 8px;
}
.badge {
display: inline-block;
margin-left: 8px;
Expand Down
Loading

0 comments on commit 59b6621

Please sign in to comment.