Skip to content

Commit

Permalink
Allow to sort options in select selector (#17468)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Aug 3, 2023
1 parent a8debb8 commit b4e2f4b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/components/ha-selector/ha-selector-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "../ha-formfield";
import "../ha-radio";
import "../ha-select";
import "../ha-input-helper-text";
import { caseInsensitiveStringCompare } from "../../common/string/compare";

@customElement("ha-selector-select")
export class HaSelectSelector extends LitElement {
Expand Down Expand Up @@ -51,12 +52,25 @@ export class HaSelectSelector extends LitElement {

if (this.localizeValue && translationKey) {
options.forEach((option) => {
option.label =
this.localizeValue!(`${translationKey}.options.${option.value}`) ||
option.label;
const localizedLabel = this.localizeValue!(
`${translationKey}.options.${option.value}`
);
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?.multiple) {
return html`
Expand Down
1 change: 1 addition & 0 deletions src/data/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export interface SelectSelector {
mode?: "list" | "dropdown";
options: readonly string[] | readonly SelectOption[];
translation_key?: string;
sort?: boolean;
} | null;
}

Expand Down

0 comments on commit b4e2f4b

Please sign in to comment.