Add validation and invalid event.

This commit is contained in:
2025-08-16 16:22:03 +02:00
parent 912da5e31f
commit 4a61bca442
2 changed files with 20 additions and 3 deletions

View File

@@ -1,11 +1,11 @@
/**
@license
Copyright (c) 2024 trading_peter
Copyright (c) 2025 trading_peter
This program is available under Apache License Version 2.0
*/
import { FormElement } from '@tp/helpers/form-element.js';
import { LitElement, html, css } from 'lit';
import { LitElement, css } from 'lit';
class TpElement extends FormElement(LitElement) {
static get styles() {
@@ -17,6 +17,23 @@ class TpElement extends FormElement(LitElement) {
`
];
}
static get properties() {
return {
errorMessage: { type: String },
};
}
validate() {
if (!this.required) return true;
const isValid = this.value !== undefined && this.value !== null;
if (!isValid) {
this.dispatchEvent(new CustomEvent('invalid', { detail: this.errorMessage, bubbles: true, composed: true }));
}
return isValid;
}
}
window.customElements.define('tp-form-value', TpElement);