Compare commits

...

1 Commits

Author SHA1 Message Date
Paul Bottein
36f5e50ccc Display label inside field for entity picker and area picker 2025-11-03 17:07:22 +01:00
4 changed files with 51 additions and 14 deletions

View File

@@ -357,7 +357,11 @@ export class HaAreaPicker extends LitElement {
protected render(): TemplateResult { protected render(): TemplateResult {
const placeholder = const placeholder =
this.placeholder ?? this.hass.localize("ui.components.area-picker.area"); this.placeholder ??
this.hass.localize("ui.components.area-picker.placeholder");
const notFoundLabel = this.hass.localize(
"ui.components.area-picker.no_match"
);
const valueRenderer = this._computeValueRenderer(this.hass.areas); const valueRenderer = this._computeValueRenderer(this.hass.areas);
@@ -367,9 +371,7 @@ export class HaAreaPicker extends LitElement {
.autofocus=${this.autofocus} .autofocus=${this.autofocus}
.label=${this.label} .label=${this.label}
.helper=${this.helper} .helper=${this.helper}
.notFoundLabel=${this.hass.localize( .notFoundLabel=${notFoundLabel}
"ui.components.area-picker.no_match"
)}
.placeholder=${placeholder} .placeholder=${placeholder}
.value=${this.value} .value=${this.value}
.getItems=${this._getItems} .getItems=${this._getItems}

View File

@@ -3,7 +3,6 @@ import type { RenderItemFunction } from "@lit-labs/virtualizer/virtualize";
import { mdiPlaylistPlus } from "@mdi/js"; import { mdiPlaylistPlus } from "@mdi/js";
import { css, html, LitElement, nothing, type CSSResultGroup } from "lit"; import { css, html, LitElement, nothing, type CSSResultGroup } from "lit";
import { customElement, property, query, state } from "lit/decorators"; import { customElement, property, query, state } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { tinykeys } from "tinykeys"; import { tinykeys } from "tinykeys";
import { fireEvent } from "../common/dom/fire_event"; import { fireEvent } from "../common/dom/fire_event";
import type { HomeAssistant } from "../types"; import type { HomeAssistant } from "../types";
@@ -107,9 +106,6 @@ export class HaGenericPicker extends LitElement {
protected render() { protected render() {
return html` return html`
${this.label
? html`<label ?disabled=${this.disabled}>${this.label}</label>`
: nothing}
<div class="container"> <div class="container">
<div id="picker"> <div id="picker">
<slot name="field"> <slot name="field">
@@ -130,7 +126,7 @@ export class HaGenericPicker extends LitElement {
type="button" type="button"
class=${this._opened ? "opened" : ""} class=${this._opened ? "opened" : ""}
compact compact
aria-label=${ifDefined(this.label)} .label=${this.label}
@click=${this.open} @click=${this.open}
@clear=${this._clear} @clear=${this._clear}
.placeholder=${this.placeholder} .placeholder=${this.placeholder}

View File

@@ -8,6 +8,7 @@ import {
type TemplateResult, type TemplateResult,
} from "lit"; } from "lit";
import { customElement, property, query } from "lit/decorators"; import { customElement, property, query } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { fireEvent } from "../common/dom/fire_event"; import { fireEvent } from "../common/dom/fire_event";
import "./ha-combo-box-item"; import "./ha-combo-box-item";
import type { HaComboBoxItem } from "./ha-combo-box-item"; import type { HaComboBoxItem } from "./ha-combo-box-item";
@@ -33,6 +34,8 @@ export class HaPickerField extends LitElement {
@property() public placeholder?: string; @property() public placeholder?: string;
@property() public label?: string;
@property({ attribute: "hide-clear-icon", type: Boolean }) @property({ attribute: "hide-clear-icon", type: Boolean })
public hideClearIcon = false; public hideClearIcon = false;
@@ -51,15 +54,35 @@ export class HaPickerField extends LitElement {
!!this.value && !this.required && !this.disabled && !this.hideClearIcon; !!this.value && !this.required && !this.disabled && !this.hideClearIcon;
return html` return html`
<ha-combo-box-item .disabled=${this.disabled} type="button" compact> <ha-combo-box-item
.disabled=${this.disabled}
type="button"
compact
aria-label=${ifDefined(this.label)}
>
${this.value ${this.value
? this.valueRenderer ? this.valueRenderer
? this.valueRenderer(this.value) ? this.valueRenderer(this.value)
: html`<slot name="headline">${this.value}</slot>` : html`<span slot="headline">${this.value}</span>`
: html` : html`
${this.label
? html`
<span
slot="headline"
class="label ${this.placeholder
? "with-placeholder"
: ""}"
>${this.label}</span
>
`
: nothing}
${this.placeholder
? html`
<span slot="headline" class="placeholder"> <span slot="headline" class="placeholder">
${this.placeholder} ${this.placeholder}
</span> </span>
`
: nothing}
`} `}
${showClearIcon ${showClearIcon
? html` ? html`
@@ -152,9 +175,24 @@ export class HaPickerField extends LitElement {
width: 32px; width: 32px;
} }
.label {
padding: 0 8px;
color: var(--secondary-text-color);
line-height: var(--ha-line-height-normal);
font-size: var(--ha-font-size-m);
white-space: nowrap;
}
.label.with-placeholder {
line-height: var(--ha-line-height-condensed);
font-size: var(--ha-font-size-xs);
}
.placeholder { .placeholder {
color: var(--secondary-text-color); color: var(--secondary-text-color);
padding: 0 8px; padding: 0 8px;
line-height: var(--ha-line-height-normal);
font-size: var(--ha-font-size-m);
} }
`, `,
]; ];

View File

@@ -824,6 +824,7 @@
"add_new": "Add new area…", "add_new": "Add new area…",
"no_areas": "You don't have any areas", "no_areas": "You don't have any areas",
"no_match": "No matching areas found", "no_match": "No matching areas found",
"placeholder": "Select an area",
"unassigned_areas": "Unassigned areas", "unassigned_areas": "Unassigned areas",
"failed_create_area": "Failed to create area." "failed_create_area": "Failed to create area."
}, },