Skip to content

Commit

Permalink
Handle falsy value in ha-yaml-editor (object selector) (#22142)
Browse files Browse the repository at this point in the history
* Handle falsy value in ha-yaml-editor (object selector)

* handle explicit `null`
  • Loading branch information
farmio authored Sep 30, 2024
1 parent 4e96ad5 commit e92be56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/components/ha-service-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,8 @@ export class HaServiceControl extends LitElement {
const value = ev.detail.value;
if (
this._value?.data?.[key] === value ||
(!this._value?.data?.[key] && (value === "" || value === undefined))
((!this._value?.data || !(key in this._value.data)) &&
(value === "" || value === undefined))
) {
return;
}
Expand Down
19 changes: 9 additions & 10 deletions src/components/ha-yaml-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { HaCodeEditor } from "./ha-code-editor";
import "./ha-button";

const isEmpty = (obj: Record<string, unknown>): boolean => {
if (typeof obj !== "object") {
if (typeof obj !== "object" || obj === null) {
return false;
}
for (const key in obj) {
Expand Down Expand Up @@ -59,14 +59,13 @@ export class HaYamlEditor extends LitElement {

public setValue(value): void {
try {
this._yaml =
value && !isEmpty(value)
? dump(value, {
schema: this.yamlSchema,
quotingType: '"',
noRefs: true,
})
: "";
this._yaml = !isEmpty(value)
? dump(value, {
schema: this.yamlSchema,
quotingType: '"',
noRefs: true,
})
: "";
} catch (err: any) {
// eslint-disable-next-line no-console
console.error(err, value);
Expand All @@ -75,7 +74,7 @@ export class HaYamlEditor extends LitElement {
}

protected firstUpdated(): void {
if (this.defaultValue) {
if (this.defaultValue !== undefined) {
this.setValue(this.defaultValue);
}
}
Expand Down

0 comments on commit e92be56

Please sign in to comment.