1
mirror of https://github.com/home-assistant/frontend synced 2024-09-28 00:43:28 +02:00

Tweak icon picker a bit (#10319)

This commit is contained in:
Bram Kragten 2021-10-20 21:03:18 +02:00 committed by GitHub
parent f062e13921
commit c3975e48d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,17 @@
import { mdiCheck, mdiMenuDown, mdiMenuUp } from "@mdi/js";
import "@polymer/paper-input/paper-input"; import "@polymer/paper-input/paper-input";
import { mdiCheck } from "@mdi/js"; import "@polymer/paper-item/paper-icon-item";
import "@polymer/paper-item/paper-item-body";
import "@vaadin/vaadin-combo-box/theme/material/vaadin-combo-box-light";
import { css, html, LitElement, TemplateResult } from "lit"; import { css, html, LitElement, TemplateResult } from "lit";
import { ComboBoxLitRenderer, comboBoxRenderer } from "lit-vaadin-helpers"; import { ComboBoxLitRenderer, comboBoxRenderer } from "lit-vaadin-helpers";
import { customElement, property, query } from "lit/decorators"; import { customElement, property, query, state } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event"; import { fireEvent } from "../common/dom/fire_event";
import { PolymerChangedEvent } from "../polymer-types"; import { PolymerChangedEvent } from "../polymer-types";
import "./ha-icon"; import "./ha-icon";
import iconList from "../../build/mdi/iconList.json"; import "./ha-icon-button";
const mdiIconList = iconList.map((icon) => `mdi:${icon}`); let mdiIconList: string[] = [];
// eslint-disable-next-line lit/prefer-static-styles // eslint-disable-next-line lit/prefer-static-styles
const rowRenderer: ComboBoxLitRenderer<string> = (item) => html`<style> const rowRenderer: ComboBoxLitRenderer<string> = (item) => html`<style>
@ -35,7 +38,7 @@ const rowRenderer: ComboBoxLitRenderer<string> = (item) => html`<style>
<ha-svg-icon .path=${mdiCheck}></ha-svg-icon> <ha-svg-icon .path=${mdiCheck}></ha-svg-icon>
<paper-icon-item> <paper-icon-item>
<ha-icon .icon=${item} slot="item-icon"></ha-icon> <ha-icon .icon=${item} slot="item-icon"></ha-icon>
<paper-item-body> ${item} </paper-item-body> <paper-item-body>${item}</paper-item-body>
</paper-icon-item>`; </paper-icon-item>`;
@customElement("ha-icon-picker") @customElement("ha-icon-picker")
@ -52,6 +55,8 @@ export class HaIconPicker extends LitElement {
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
@state() private _opened = false;
@query("vaadin-combo-box-light", true) private comboBox!: HTMLElement; @query("vaadin-combo-box-light", true) private comboBox!: HTMLElement;
protected render(): TemplateResult { protected render(): TemplateResult {
@ -60,9 +65,10 @@ export class HaIconPicker extends LitElement {
item-value-path="icon" item-value-path="icon"
item-label-path="icon" item-label-path="icon"
.value=${this._value} .value=${this._value}
.allowCustomValue=${true} allow-custom-value
.filteredItems=${[]} .filteredItems=${mdiIconList}
${comboBoxRenderer(rowRenderer)} ${comboBoxRenderer(rowRenderer)}
@opened-changed=${this._openedChanged}
@value-changed=${this._valueChanged} @value-changed=${this._valueChanged}
@filter-changed=${this._filterChanged} @filter-changed=${this._filterChanged}
> >
@ -78,20 +84,34 @@ export class HaIconPicker extends LitElement {
> >
${this._value || this.placeholder ${this._value || this.placeholder
? html` ? html`
<ha-icon .icon=${this._value || this.placeholder} slot="suffix"> <ha-icon .icon=${this._value || this.placeholder} slot="prefix">
</ha-icon> </ha-icon>
` `
: this.fallbackPath : this.fallbackPath
? html`<ha-svg-icon ? html`<ha-svg-icon
.path=${this.fallbackPath} .path=${this.fallbackPath}
slot="suffix" slot="prefix"
></ha-svg-icon>` ></ha-svg-icon>`
: ""} : ""}
<ha-icon-button
.path=${this._opened ? mdiMenuUp : mdiMenuDown}
slot="suffix"
class="toggle-button"
></ha-icon-button>
</paper-input> </paper-input>
</vaadin-combo-box-light> </vaadin-combo-box-light>
`; `;
} }
private async _openedChanged(ev: PolymerChangedEvent<boolean>) {
this._opened = ev.detail.value;
if (this._opened && !mdiIconList.length) {
const iconList = await import("../../build/mdi/iconList.json");
mdiIconList = iconList.default.map((icon) => `mdi:${icon}`);
(this.comboBox as any).filteredItems = mdiIconList;
}
}
private _valueChanged(ev: PolymerChangedEvent<string>) { private _valueChanged(ev: PolymerChangedEvent<string>) {
this._setValue(ev.detail.value); this._setValue(ev.detail.value);
} }
@ -114,7 +134,7 @@ export class HaIconPicker extends LitElement {
const characterCount = filterString.length; const characterCount = filterString.length;
if (characterCount >= 2) { if (characterCount >= 2) {
const filteredItems = mdiIconList.filter((icon) => const filteredItems = mdiIconList.filter((icon) =>
icon.toLowerCase().includes(filterString) icon.includes(filterString)
); );
if (filteredItems.length > 0) { if (filteredItems.length > 0) {
(this.comboBox as any).filteredItems = filteredItems; (this.comboBox as any).filteredItems = filteredItems;
@ -122,7 +142,7 @@ export class HaIconPicker extends LitElement {
(this.comboBox as any).filteredItems = [filterString]; (this.comboBox as any).filteredItems = [filterString];
} }
} else { } else {
(this.comboBox as any).filteredItems = []; (this.comboBox as any).filteredItems = mdiIconList;
} }
} }
@ -137,6 +157,20 @@ export class HaIconPicker extends LitElement {
position: relative; position: relative;
bottom: 2px; bottom: 2px;
} }
*[slot="prefix"] {
margin-right: 8px;
}
paper-input > ha-icon-button {
--mdc-icon-button-size: 24px;
padding: 2px;
color: var(--secondary-text-color);
}
`; `;
} }
} }
declare global {
interface HTMLElementTagNameMap {
"ha-icon-picker": HaIconPicker;
}
}