Allow null selector (#14212)

This commit is contained in:
Paul Bottein
2022-10-27 19:17:42 +02:00
committed by GitHub
parent a56b2e3270
commit 2ab5da6d84
17 changed files with 121 additions and 106 deletions

View File

@@ -9,6 +9,7 @@ import type { HomeAssistant } from "../../types";
import "../ha-checkbox";
import "../ha-chip";
import "../ha-chip-set";
import "../ha-combo-box";
import type { HaComboBox } from "../ha-combo-box";
import "../ha-formfield";
import "../ha-radio";
@@ -36,12 +37,13 @@ export class HaSelectSelector extends LitElement {
private _filter = "";
protected render() {
const options = this.selector.select.options.map((option) =>
typeof option === "object" ? option : { value: option, label: option }
);
const options =
this.selector.select?.options.map((option) =>
typeof option === "object" ? option : { value: option, label: option }
) || [];
if (!this.selector.select.custom_value && this._mode === "list") {
if (!this.selector.select.multiple) {
if (!this.selector.select?.custom_value && this._mode === "list") {
if (!this.selector.select?.multiple) {
return html`
<div>
${this.label}
@@ -82,7 +84,7 @@ export class HaSelectSelector extends LitElement {
`;
}
if (this.selector.select.multiple) {
if (this.selector.select?.multiple) {
const value =
!this.value || this.value === "" ? [] : (this.value as string[]);
@@ -123,7 +125,7 @@ export class HaSelectSelector extends LitElement {
`;
}
if (this.selector.select.custom_value) {
if (this.selector.select?.custom_value) {
if (
this.value !== undefined &&
!options.find((option) => option.value === this.value)
@@ -178,8 +180,8 @@ export class HaSelectSelector extends LitElement {
private get _mode(): "list" | "dropdown" {
return (
this.selector.select.mode ||
(this.selector.select.options.length < 6 ? "list" : "dropdown")
this.selector.select?.mode ||
((this.selector.select?.options?.length || 0) < 6 ? "list" : "dropdown")
);
}
@@ -243,7 +245,7 @@ export class HaSelectSelector extends LitElement {
return;
}
if (!this.selector.select.multiple) {
if (!this.selector.select?.multiple) {
fireEvent(this, "value-changed", {
value: newValue,
});
@@ -271,14 +273,14 @@ export class HaSelectSelector extends LitElement {
this._filter = ev?.detail.value || "";
const filteredItems = this.comboBox.items?.filter((item) => {
if (this.selector.select.multiple && this.value?.includes(item.value)) {
if (this.selector.select?.multiple && this.value?.includes(item.value)) {
return false;
}
const label = item.label || item.value;
return label.toLowerCase().includes(this._filter?.toLowerCase());
});
if (this._filter && this.selector.select.custom_value) {
if (this._filter && this.selector.select?.custom_value) {
filteredItems?.unshift({ label: this._filter, value: this._filter });
}