mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 03:36:44 +00:00
Sort labels by name (#20316)
This commit is contained in:
parent
21263a1ffb
commit
48ee3a34eb
@ -5,20 +5,22 @@ import { LabelRegistryEntry } from "../../data/label_registry";
|
|||||||
import { computeCssColor } from "../../common/color/compute-color";
|
import { computeCssColor } from "../../common/color/compute-color";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import "../ha-label";
|
import "../ha-label";
|
||||||
|
import { stringCompare } from "../../common/string/compare";
|
||||||
|
|
||||||
@customElement("ha-data-table-labels")
|
@customElement("ha-data-table-labels")
|
||||||
class HaDataTableLabels extends LitElement {
|
class HaDataTableLabels extends LitElement {
|
||||||
@property({ attribute: false }) public labels!: LabelRegistryEntry[];
|
@property({ attribute: false }) public labels!: LabelRegistryEntry[];
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
|
const labels = this.labels.sort((a, b) => stringCompare(a.name, b.name));
|
||||||
return html`
|
return html`
|
||||||
<ha-chip-set>
|
<ha-chip-set>
|
||||||
${repeat(
|
${repeat(
|
||||||
this.labels.slice(0, 2),
|
labels.slice(0, 2),
|
||||||
(label) => label.label_id,
|
(label) => label.label_id,
|
||||||
(label) => this._renderLabel(label, true)
|
(label) => this._renderLabel(label, true)
|
||||||
)}
|
)}
|
||||||
${this.labels.length > 2
|
${labels.length > 2
|
||||||
? html`<ha-button-menu
|
? html`<ha-button-menu
|
||||||
absolute
|
absolute
|
||||||
role="button"
|
role="button"
|
||||||
@ -27,10 +29,10 @@ class HaDataTableLabels extends LitElement {
|
|||||||
@closed=${this._handleIconOverflowMenuClosed}
|
@closed=${this._handleIconOverflowMenuClosed}
|
||||||
>
|
>
|
||||||
<ha-label slot="trigger" class="plus" dense>
|
<ha-label slot="trigger" class="plus" dense>
|
||||||
+${this.labels.length - 2}
|
+${labels.length - 2}
|
||||||
</ha-label>
|
</ha-label>
|
||||||
${repeat(
|
${repeat(
|
||||||
this.labels.slice(2),
|
labels.slice(2),
|
||||||
(label) => label.label_id,
|
(label) => label.label_id,
|
||||||
(label) => html`
|
(label) => html`
|
||||||
<ha-list-item @click=${this._labelClicked} .item=${label}>
|
<ha-list-item @click=${this._labelClicked} .item=${label}>
|
||||||
|
@ -17,6 +17,7 @@ import "./chips/ha-input-chip";
|
|||||||
import type { HaDevicePickerDeviceFilterFunc } from "./device/ha-device-picker";
|
import type { HaDevicePickerDeviceFilterFunc } from "./device/ha-device-picker";
|
||||||
import "./ha-label-picker";
|
import "./ha-label-picker";
|
||||||
import type { HaLabelPicker } from "./ha-label-picker";
|
import type { HaLabelPicker } from "./ha-label-picker";
|
||||||
|
import { stringCompare } from "../common/string/compare";
|
||||||
|
|
||||||
@customElement("ha-labels-picker")
|
@customElement("ha-labels-picker")
|
||||||
export class HaLabelsPicker extends SubscribeMixin(LitElement) {
|
export class HaLabelsPicker extends SubscribeMixin(LitElement) {
|
||||||
@ -75,7 +76,7 @@ export class HaLabelsPicker extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public required = false;
|
@property({ type: Boolean }) public required = false;
|
||||||
|
|
||||||
@state() private _labels?: LabelRegistryEntry[];
|
@state() private _labels?: { [id: string]: LabelRegistryEntry };
|
||||||
|
|
||||||
@query("ha-label-picker", true) public labelPicker!: HaLabelPicker;
|
@query("ha-label-picker", true) public labelPicker!: HaLabelPicker;
|
||||||
|
|
||||||
@ -92,22 +93,28 @@ export class HaLabelsPicker extends SubscribeMixin(LitElement) {
|
|||||||
protected hassSubscribe(): (UnsubscribeFunc | Promise<UnsubscribeFunc>)[] {
|
protected hassSubscribe(): (UnsubscribeFunc | Promise<UnsubscribeFunc>)[] {
|
||||||
return [
|
return [
|
||||||
subscribeLabelRegistry(this.hass.connection, (labels) => {
|
subscribeLabelRegistry(this.hass.connection, (labels) => {
|
||||||
this._labels = labels;
|
const lookUp = {};
|
||||||
|
labels.forEach((label) => {
|
||||||
|
lookUp[label.label_id] = label;
|
||||||
|
});
|
||||||
|
this._labels = lookUp;
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
|
const labels = this.value
|
||||||
|
?.map((id) => this._labels?.[id])
|
||||||
|
.sort((a, b) =>
|
||||||
|
stringCompare(a?.name || "", b?.name || "", this.hass.locale.language)
|
||||||
|
);
|
||||||
return html`
|
return html`
|
||||||
${this.value?.length
|
${labels?.length
|
||||||
? html`<ha-chip-set>
|
? html`<ha-chip-set>
|
||||||
${repeat(
|
${repeat(
|
||||||
this.value,
|
labels,
|
||||||
(item) => item,
|
(label) => label?.label_id,
|
||||||
(item, idx) => {
|
(label, idx) => {
|
||||||
const label = this._labels?.find(
|
|
||||||
(lbl) => lbl.label_id === item
|
|
||||||
);
|
|
||||||
const color = label?.color
|
const color = label?.color
|
||||||
? computeCssColor(label.color)
|
? computeCssColor(label.color)
|
||||||
: undefined;
|
: undefined;
|
||||||
@ -168,9 +175,6 @@ export class HaLabelsPicker extends SubscribeMixin(LitElement) {
|
|||||||
label.label_id,
|
label.label_id,
|
||||||
values
|
values
|
||||||
);
|
);
|
||||||
this._labels = this._labels!.map((lbl) =>
|
|
||||||
lbl.label_id === updated.label_id ? updated : lbl
|
|
||||||
);
|
|
||||||
return updated;
|
return updated;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user