Skip to content

Commit

Permalink
Sync selected icon with selected value in new select component (home-…
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Aug 16, 2023
1 parent 2c17d2f commit bd52643
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 64 deletions.
27 changes: 24 additions & 3 deletions src/components/ha-control-select-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { classMap } from "lit/directives/class-map";
import { ifDefined } from "lit/directives/if-defined";
import { debounce } from "../common/util/debounce";
import { nextRender } from "../common/util/render-status";
import "./ha-icon";
import type { HaIcon } from "./ha-icon";
import "./ha-svg-icon";
import type { HaSvgIcon } from "./ha-svg-icon";

@customElement("ha-control-select-menu")
export class HaControlSelectMenu extends SelectBase {
Expand Down Expand Up @@ -66,9 +70,7 @@ export class HaControlSelectMenu extends SelectBase {
@touchend=${this.handleRippleDeactivate}
@touchcancel=${this.handleRippleDeactivate}
>
<div class="icon">
<slot name="icon"></slot>
</div>
${this.renderIcon()}
<div class="content">
<p id="label" class="label">${this.label}</p>
${this.selectedText
Expand All @@ -84,6 +86,25 @@ export class HaControlSelectMenu extends SelectBase {
`;
}

private renderIcon() {
const index = this.mdcFoundation?.getSelectedIndex();
const items = this.menuElement?.items ?? [];
const item = index != null ? items[index] : undefined;
const icon =
item?.querySelector("[slot='graphic']") ??
(null as HaSvgIcon | HaIcon | null);

return html`
<div class="icon">
${icon && "path" in icon
? html`<ha-svg-icon .path=${icon.path}></ha-svg-icon>`
: icon && "icon" in icon
? html`<ha-icon .path=${icon.icon}></ha-icon>`
: html`<slot name="icon"></slot>`}
</div>
`;
}

protected onFocus() {
this.handleRippleFocus();
super.onFocus();
Expand Down
25 changes: 4 additions & 21 deletions src/dialogs/more-info/controls/more-info-climate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import "../../../components/ha-switch";
import {
ClimateEntity,
ClimateEntityFeature,
HvacMode,
compareClimateHvacModes,
computeFanModeIcon,
computeHvacModeIcon,
Expand Down Expand Up @@ -176,11 +175,7 @@ class MoreInfoClimate extends LitElement {
@selected=${this._handleOperationModeChanged}
@closed=${stopPropagation}
>
<ha-svg-icon
slot="icon"
.path=${computeHvacModeIcon(stateObj.state as HvacMode) ??
mdiThermostat}
></ha-svg-icon>
<ha-svg-icon slot="icon" .path=${mdiThermostat}></ha-svg-icon>
${stateObj.attributes.hvac_modes
.concat()
.sort(compareClimateHvacModes)
Expand Down Expand Up @@ -212,9 +207,7 @@ class MoreInfoClimate extends LitElement {
>
<ha-svg-icon
slot="icon"
.path=${stateObj.attributes.preset_mode
? computePresetModeIcon(stateObj.attributes.preset_mode)
: mdiTuneVariant}
.path=${mdiTuneVariant}
></ha-svg-icon>
${stateObj.attributes.preset_modes!.map(
(mode) => html`
Expand Down Expand Up @@ -248,12 +241,7 @@ class MoreInfoClimate extends LitElement {
@selected=${this._handleFanModeChanged}
@closed=${stopPropagation}
>
<ha-svg-icon
slot="icon"
.path=${stateObj.attributes.fan_mode
? computeFanModeIcon(stateObj.attributes.fan_mode)
: mdiFan}
></ha-svg-icon>
<ha-svg-icon slot="icon" .path=${mdiFan}></ha-svg-icon>
${stateObj.attributes.fan_modes!.map(
(mode) => html`
<ha-list-item .value=${mode} graphic="icon">
Expand Down Expand Up @@ -286,12 +274,7 @@ class MoreInfoClimate extends LitElement {
@selected=${this._handleSwingmodeChanged}
@closed=${stopPropagation}
>
<ha-svg-icon
slot="icon"
.path=${stateObj.attributes.swing_mode
? computeSwingModeIcon(stateObj.attributes.swing_mode)
: haOscillating}
></ha-svg-icon>
<ha-svg-icon slot="icon" .path=${haOscillating}></ha-svg-icon>
${stateObj.attributes.swing_modes!.map(
(mode) => html`
<ha-list-item .value=${mode} graphic="icon">
Expand Down
11 changes: 2 additions & 9 deletions src/dialogs/more-info/controls/more-info-fan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,7 @@ class MoreInfoFan extends LitElement {
@selected=${this._handleDirection}
@closed=${stopPropagation}
>
<ha-svg-icon
slot="icon"
.path=${this.stateObj.attributes.direction === "reverse"
? mdiRotateLeft
: mdiRotateRight}
></ha-svg-icon>
<ha-svg-icon slot="icon" .path=${mdiRotateLeft}></ha-svg-icon>
<ha-list-item value="forward" graphic="icon">
<ha-svg-icon
slot="graphic"
Expand Down Expand Up @@ -286,9 +281,7 @@ class MoreInfoFan extends LitElement {
>
<ha-svg-icon
slot="icon"
.path=${this.stateObj.attributes.oscillating
? haOscillating
: haOscillatingOff}
.path=${haOscillatingOff}
></ha-svg-icon>
<ha-list-item value="on" graphic="icon">
<ha-svg-icon
Expand Down
35 changes: 14 additions & 21 deletions src/dialogs/more-info/controls/more-info-humidifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,11 @@ class MoreInfoHumidifier extends LitElement {
fixedMenuPosition
naturalMenuWidth
@selected=${this._handleModeChanged}
@action=${this._handleModeChanged}
@closed=${stopPropagation}
>
<ha-svg-icon
slot="icon"
.path=${stateObj.attributes.mode
? computeHumidiferModeIcon(stateObj.attributes.mode)
: mdiTuneVariant}
.path=${mdiTuneVariant}
></ha-svg-icon>
${stateObj.attributes.available_modes!.map(
(mode) => html`
Expand Down Expand Up @@ -196,6 +193,19 @@ class MoreInfoHumidifier extends LitElement {
);
}

private _handleModeChanged(ev) {
const newVal = ev.target.value || null;
this._mode = newVal;
this._callServiceHelper(
this.stateObj!.attributes.mode,
newVal,
"set_mode",
{
mode: newVal,
}
);
}

private async _callServiceHelper(
oldVal: unknown,
newVal: unknown,
Expand Down Expand Up @@ -234,23 +244,6 @@ class MoreInfoHumidifier extends LitElement {
}
}

private _handleModeChanged(ev) {
ev.stopPropagation();
ev.preventDefault();

const index = ev.detail.index;
const newVal = this.stateObj!.attributes.available_modes![index];
const oldVal = this._mode;

if (!newVal || oldVal === newVal) return;

this._mode = newVal;
this.hass.callService("humidifier", "set_mode", {
entity_id: this.stateObj!.entity_id,
mode: newVal,
});
}

static get styles(): CSSResultGroup {
return [
moreInfoControlStyle,
Expand Down
12 changes: 2 additions & 10 deletions src/dialogs/more-info/controls/more-info-water_heater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { formatNumber } from "../../../common/number/format_number";
import "../../../components/ha-control-select-menu";
import "../../../components/ha-list-item";
import {
OperationMode,
WaterHeaterEntity,
WaterHeaterEntityFeature,
compareWaterHeaterOperationMode,
Expand Down Expand Up @@ -86,9 +85,7 @@ class MoreInfoWaterHeater extends LitElement {
>
<ha-svg-icon
slot="icon"
.path=${computeOperationModeIcon(
stateObj.state as OperationMode
) ?? mdiWaterBoiler}
.path=${mdiWaterBoiler}
></ha-svg-icon>
${stateObj.attributes.operation_list
.concat()
Expand Down Expand Up @@ -121,12 +118,7 @@ class MoreInfoWaterHeater extends LitElement {
@selected=${this._handleAwayModeChanged}
@closed=${stopPropagation}
>
<ha-svg-icon
slot="icon"
.path=${stateObj.attributes.away_mode === "on"
? mdiAccountArrowRight
: mdiAccount}
></ha-svg-icon>
<ha-svg-icon slot="icon" .path=${mdiAccount}></ha-svg-icon>
<ha-list-item value="on" graphic="icon">
<ha-svg-icon
slot="graphic"
Expand Down

0 comments on commit bd52643

Please sign in to comment.