mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-26 18:56:39 +00:00
Allow to sort options in select selector (#17468)
This commit is contained in:
parent
a8debb8daa
commit
b4e2f4b0f5
@ -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`
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user