Allow to sort options in select selector (#17468)

This commit is contained in:
Paul Bottein 2023-08-03 21:49:31 +02:00 committed by GitHub
parent a8debb8daa
commit b4e2f4b0f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -16,6 +16,7 @@ import "../ha-formfield";
import "../ha-radio"; import "../ha-radio";
import "../ha-select"; import "../ha-select";
import "../ha-input-helper-text"; import "../ha-input-helper-text";
import { caseInsensitiveStringCompare } from "../../common/string/compare";
@customElement("ha-selector-select") @customElement("ha-selector-select")
export class HaSelectSelector extends LitElement { export class HaSelectSelector extends LitElement {
@ -51,12 +52,25 @@ export class HaSelectSelector extends LitElement {
if (this.localizeValue && translationKey) { if (this.localizeValue && translationKey) {
options.forEach((option) => { options.forEach((option) => {
option.label = const localizedLabel = this.localizeValue!(
this.localizeValue!(`${translationKey}.options.${option.value}`) || `${translationKey}.options.${option.value}`
option.label; );
if (localizedLabel) {
option.label = localizedLabel;
}
}); });
} }
if (this.selector.select?.sort) {
options.sort((a, b) =>
caseInsensitiveStringCompare(
a.label,
b.label,
this.hass.locale.language
)
);
}
if (!this.selector.select?.custom_value && this._mode === "list") { if (!this.selector.select?.custom_value && this._mode === "list") {
if (!this.selector.select?.multiple) { if (!this.selector.select?.multiple) {
return html` return html`

View File

@ -296,6 +296,7 @@ export interface SelectSelector {
mode?: "list" | "dropdown"; mode?: "list" | "dropdown";
options: readonly string[] | readonly SelectOption[]; options: readonly string[] | readonly SelectOption[];
translation_key?: string; translation_key?: string;
sort?: boolean;
} | null; } | null;
} }