Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace checkboxes in list items with check-list-item #11610

Merged
merged 3 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/components/ha-check-list-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { css } from "lit";
import { CheckListItem } from "@material/mwc-list/mwc-check-list-item";
import { customElement } from "lit/decorators";

const styles = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why define above and not use static styles = […] ?

...CheckListItem.styles,
css`
:host {
--mdc-theme-secondary: var(--primary-color);
}
`,
];

@customElement("ha-check-list-item")
export class HaCheckListItem extends CheckListItem {
static get styles() {
return styles;
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-check-list-item": HaCheckListItem;
}
}
14 changes: 11 additions & 3 deletions src/components/ha-checkbox.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Checkbox } from "@material/mwc-checkbox";
import { css } from "lit";
import { customElement } from "lit/decorators";

const styles = [
...Checkbox.styles,
css`
:host {
--mdc-theme-secondary: var(--primary-color);
}
`,
];
@customElement("ha-checkbox")
export class HaCheckbox extends Checkbox {
public firstUpdated() {
super.firstUpdated();
this.style.setProperty("--mdc-theme-secondary", "var(--primary-color)");
static get styles() {
return styles;
}
}

Expand Down
76 changes: 55 additions & 21 deletions src/components/ha-form/ha-form-multi_select.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { mdiMenuDown, mdiMenuUp } from "@mdi/js";
import "@material/mwc-textfield";
import "@material/mwc-formfield";
import "@material/mwc-select/mwc-select";
import "@material/mwc-textfield";
import { mdiMenuDown, mdiMenuUp } from "@mdi/js";
import {
css,
CSSResultGroup,
html,
LitElement,
TemplateResult,
PropertyValues,
TemplateResult,
} from "lit";
import { customElement, property, query, state } from "lit/decorators";
import { fireEvent } from "../../common/dom/fire_event";
import "../ha-button-menu";
import { HaCheckListItem } from "../ha-check-list-item";
import "../ha-checkbox";
import type { HaCheckbox } from "../ha-checkbox";
import "../ha-svg-icon";
import {
HaFormElement,
HaFormMultiSelectData,
HaFormMultiSelectSchema,
} from "./types";
import "../ha-checkbox";
import type { HaCheckbox } from "../ha-checkbox";

function optionValue(item: string | string[]): string {
return Array.isArray(item) ? item[0] : item;
Expand Down Expand Up @@ -57,23 +59,23 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
: Object.entries(this.schema.options);
const data = this.data || [];

const renderedOptions = options.map((item: string | [string, string]) => {
const value = optionValue(item);
return html`
<mwc-formfield .label=${optionLabel(item)}>
<ha-checkbox
.checked=${data.includes(value)}
.value=${value}
.disabled=${this.disabled}
@change=${this._valueChanged}
></ha-checkbox>
</mwc-formfield>
`;
});

// We will just render all checkboxes.
if (options.length < SHOW_ALL_ENTRIES_LIMIT) {
return html`<div>${this.label}${renderedOptions}</div> `;
return html`<div>
${this.label}${options.map((item: string | [string, string]) => {
const value = optionValue(item);
return html`
<mwc-formfield .label=${optionLabel(item)}>
<ha-checkbox
.checked=${data.includes(value)}
.value=${value}
.disabled=${this.disabled}
@change=${this._valueChanged}
></ha-checkbox>
</mwc-formfield>
`;
})}
</div> `;
}

return html`
Expand All @@ -83,6 +85,8 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
corner="BOTTOM_START"
@opened=${this._handleOpen}
@closed=${this._handleClose}
multi
activatable
>
<mwc-textfield
slot="trigger"
Expand All @@ -97,7 +101,20 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
slot="trigger"
.path=${this._opened ? mdiMenuUp : mdiMenuDown}
></ha-svg-icon>
${renderedOptions}
${options.map((item: string | [string, string]) => {
const value = optionValue(item);
const selected = data.includes(value);
return html`<ha-check-list-item
left
.selected=${selected}
.activated=${selected}
@request-selected=${this._selectedChanged}
.value=${value}
.disabled=${this.disabled}
>
${optionLabel(item)}
</ha-check-list-item>`;
})}
</ha-button-menu>
`;
}
Expand Down Expand Up @@ -125,9 +142,23 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
}
}

private _selectedChanged(ev: CustomEvent): void {
ev.stopPropagation();
if (ev.detail.source === "property") {
return;
}
this._handleValueChanged(
(ev.target as HaCheckListItem).value,
ev.detail.selected
);
}

private _valueChanged(ev: CustomEvent): void {
const { value, checked } = ev.target as HaCheckbox;
this._handleValueChanged(value, checked);
}

private _handleValueChanged(value, checked: boolean): void {
let newValue: string[];

if (checked) {
Expand Down Expand Up @@ -179,6 +210,9 @@ export class HaFormMultiSelect extends LitElement implements HaFormElement {
display: block;
pointer-events: none;
}
mwc-check-list-item {
--mdc-theme-secondary: var(--primary-color);
}
bramkragten marked this conversation as resolved.
Show resolved Hide resolved
ha-svg-icon {
color: var(--input-dropdown-icon-color);
position: absolute;
Expand Down
14 changes: 11 additions & 3 deletions src/components/ha-radio.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Radio } from "@material/mwc-radio";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we go all fancy and import just the base and the styles? like this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼 yes

import { css } from "lit";
import { customElement } from "lit/decorators";

const styles = [
...Radio.styles,
css`
:host {
--mdc-theme-secondary: var(--primary-color);
}
`,
];
@customElement("ha-radio")
export class HaRadio extends Radio {
public firstUpdated() {
super.firstUpdated();
this.style.setProperty("--mdc-theme-secondary", "var(--primary-color)");
static get styles() {
return styles;
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/panels/config/devices/ha-config-devices-dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "@material/mwc-list/mwc-list-item";
import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item";
import { mdiCancel, mdiFilterVariant, mdiPlus } from "@mdi/js";
import "@polymer/paper-tooltip/paper-tooltip";
Expand All @@ -19,6 +18,7 @@ import "../../../components/entity/ha-battery-icon";
import "../../../components/ha-button-menu";
import "../../../components/ha-fab";
import "../../../components/ha-icon-button";
import "../../../components/ha-check-list-item";
import { AreaRegistryEntry } from "../../../data/area_registry";
import { ConfigEntry } from "../../../data/config_entries";
import {
Expand Down Expand Up @@ -435,19 +435,15 @@ export class HaConfigDeviceDashboard extends LitElement {
)}
.path=${mdiFilterVariant}
></ha-icon-button>
<mwc-list-item
<ha-check-list-item
left
@request-selected=${this._showDisabledChanged}
graphic="control"
.selected=${this._showDisabled}
>
<ha-checkbox
slot="graphic"
.checked=${this._showDisabled}
></ha-checkbox>
${this.hass!.localize(
"ui.panel.config.devices.picker.filter.show_disabled"
)}
</mwc-list-item>
</ha-check-list-item>
</ha-button-menu>
</hass-tabs-subpage-data-table>
`;
Expand Down
30 changes: 10 additions & 20 deletions src/panels/config/entities/ha-config-entities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "@material/mwc-list/mwc-list-item";
import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item";
import {
mdiAlertCircle,
Expand Down Expand Up @@ -35,6 +34,7 @@ import type {
import "../../../components/ha-button-menu";
import "../../../components/ha-icon-button";
import "../../../components/ha-svg-icon";
import "../../../components/ha-check-list-item";
import {
AreaRegistryEntry,
subscribeAreaRegistry,
Expand Down Expand Up @@ -586,45 +586,35 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) {
)}
.path=${mdiFilterVariant}
></ha-icon-button>
<mwc-list-item
<ha-check-list-item
@request-selected=${this._showDisabledChanged}
graphic="control"
.selected=${this._showDisabled}
left
>
<ha-checkbox
slot="graphic"
.checked=${this._showDisabled}
></ha-checkbox>
${this.hass!.localize(
"ui.panel.config.entities.picker.filter.show_disabled"
)}
</mwc-list-item>
<mwc-list-item
</ha-check-list-item>
<ha-check-list-item
@request-selected=${this._showRestoredChanged}
graphic="control"
.selected=${this._showUnavailable}
left
>
<ha-checkbox
slot="graphic"
.checked=${this._showUnavailable}
></ha-checkbox>
${this.hass!.localize(
"ui.panel.config.entities.picker.filter.show_unavailable"
)}
</mwc-list-item>
<mwc-list-item
</ha-check-list-item>
<ha-check-list-item
@request-selected=${this._showReadOnlyChanged}
graphic="control"
.selected=${this._showReadOnly}
left
>
<ha-checkbox
slot="graphic"
.checked=${this._showReadOnly}
></ha-checkbox>
${this.hass!.localize(
"ui.panel.config.entities.picker.filter.show_readonly"
)}
</mwc-list-item>
</ha-check-list-item>
</ha-button-menu>`}
${includeZHAFab
? html`<a href="/config/zha/add" slot="fab">
Expand Down
16 changes: 6 additions & 10 deletions src/panels/config/integrations/ha-config-integrations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ActionDetail } from "@material/mwc-list";
import "@material/mwc-list/mwc-list-item";
import { mdiFilterVariant, mdiPlus } from "@mdi/js";
import Fuse from "fuse.js";
import type { UnsubscribeFunc } from "home-assistant-js-websocket";
Expand All @@ -26,6 +25,8 @@ import "../../../components/ha-checkbox";
import "../../../components/ha-fab";
import "../../../components/ha-icon-button";
import "../../../components/ha-svg-icon";
import "../../../components/ha-check-list-item";

import { isComponentLoaded } from "../../../common/config/is_component_loaded";
import { ConfigEntry, getConfigEntries } from "../../../data/config_entries";
import {
Expand Down Expand Up @@ -308,21 +309,16 @@ class HaConfigIntegrations extends SubscribeMixin(LitElement) {
.path=${mdiFilterVariant}
>
</ha-icon-button>
<mwc-list-item graphic="control" .selected=${this._showIgnored}>
<ha-checkbox slot="graphic" .checked=${this._showIgnored}></ha-checkbox>
<ha-check-list-item left .selected=${this._showIgnored}>
${this.hass.localize(
"ui.panel.config.integrations.ignore.show_ignored"
)}
</mwc-list-item>
<mwc-list-item graphic="control" .selected=${this._showDisabled}>
<ha-checkbox
slot="graphic"
.checked=${this._showDisabled}
></ha-checkbox>
</ha-check-list-item>
<ha-check-list-item left .selected=${this._showDisabled}>
${this.hass.localize(
"ui.panel.config.integrations.disable.show_disabled"
)}
</mwc-list-item>
</ha-check-list-item>
</ha-button-menu>`;

return html`
Expand Down