Display yaml errors in all ha-yaml-editor (#25819)

This commit is contained in:
karwosts 2025-06-17 23:32:06 -07:00 committed by GitHub
parent 0ddf3aa675
commit e3cdd69835
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import { showToast } from "../util/toast";
import { copyToClipboard } from "../common/util/copy-clipboard"; import { copyToClipboard } from "../common/util/copy-clipboard";
import type { HaCodeEditor } from "./ha-code-editor"; import type { HaCodeEditor } from "./ha-code-editor";
import "./ha-button"; import "./ha-button";
import "./ha-alert";
const isEmpty = (obj: Record<string, unknown>): boolean => { const isEmpty = (obj: Record<string, unknown>): boolean => {
if (typeof obj !== "object" || obj === null) { if (typeof obj !== "object" || obj === null) {
@ -51,8 +52,15 @@ export class HaYamlEditor extends LitElement {
@property({ attribute: "has-extra-actions", type: Boolean }) @property({ attribute: "has-extra-actions", type: Boolean })
public hasExtraActions = false; public hasExtraActions = false;
@property({ attribute: "show-errors", type: Boolean })
public showErrors = true;
@state() private _yaml = ""; @state() private _yaml = "";
@state() private _error = "";
@state() private _showingError = false;
@query("ha-code-editor") _codeEditor?: HaCodeEditor; @query("ha-code-editor") _codeEditor?: HaCodeEditor;
public setValue(value): void { public setValue(value): void {
@ -107,8 +115,12 @@ export class HaYamlEditor extends LitElement {
autocomplete-icons autocomplete-icons
.error=${this.isValid === false} .error=${this.isValid === false}
@value-changed=${this._onChange} @value-changed=${this._onChange}
@blur=${this._onBlur}
dir="ltr" dir="ltr"
></ha-code-editor> ></ha-code-editor>
${this._showingError
? html`<ha-alert alert-type="error">${this._error}</ha-alert>`
: nothing}
${this.copyClipboard || this.hasExtraActions ${this.copyClipboard || this.hasExtraActions
? html` ? html`
<div class="card-actions"> <div class="card-actions">
@ -146,6 +158,10 @@ export class HaYamlEditor extends LitElement {
} else { } else {
parsed = {}; parsed = {};
} }
this._error = errorMsg ?? "";
if (isValid) {
this._showingError = false;
}
this.value = parsed; this.value = parsed;
this.isValid = isValid; this.isValid = isValid;
@ -157,6 +173,12 @@ export class HaYamlEditor extends LitElement {
} as any); } as any);
} }
private _onBlur(): void {
if (this.showErrors && this._error) {
this._showingError = true;
}
}
get yaml() { get yaml() {
return this._yaml; return this._yaml;
} }

View File

@ -501,6 +501,7 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
.defaultValue=${this._preprocessYaml()} .defaultValue=${this._preprocessYaml()}
.readOnly=${this._readOnly} .readOnly=${this._readOnly}
@value-changed=${this._yamlChanged} @value-changed=${this._yamlChanged}
.showErrors=${false}
></ha-yaml-editor>` ></ha-yaml-editor>`
: nothing} : nothing}
</div> </div>

View File

@ -320,6 +320,7 @@ export class HaSceneEditor extends PreventUnsavedMixin(
.hass=${this.hass} .hass=${this.hass}
.defaultValue=${this._config} .defaultValue=${this._config}
@value-changed=${this._yamlChanged} @value-changed=${this._yamlChanged}
.showErrors=${false}
></ha-yaml-editor>`; ></ha-yaml-editor>`;
} }

View File

@ -441,6 +441,7 @@ export class HaScriptEditor extends SubscribeMixin(
.defaultValue=${this._preprocessYaml()} .defaultValue=${this._preprocessYaml()}
.readOnly=${this._readOnly} .readOnly=${this._readOnly}
@value-changed=${this._yamlChanged} @value-changed=${this._yamlChanged}
.showErrors=${false}
></ha-yaml-editor>` ></ha-yaml-editor>`
: nothing} : nothing}
</div> </div>

View File

@ -243,6 +243,7 @@ export abstract class HuiElementEditor<
@blur=${this._onBlurYaml} @blur=${this._onBlurYaml}
@keydown=${this._ignoreKeydown} @keydown=${this._ignoreKeydown}
dir="ltr" dir="ltr"
.showErrors=${false}
></ha-yaml-editor> ></ha-yaml-editor>
</div> </div>
`} `}