${this.caption !== ''
? html`
${this.caption}
`
: null}
${this.isInvalid
? html`
${this.invalidText || this.internalValidationMsg}
`
: null}
`;
}
private handleInput(e: any) {
this.value = e.target.value;
this._validate(true, false);
// emit selected value
const event = new CustomEvent('on-input', {
detail: {
value: e.target.value,
origEvent: e,
},
});
this.dispatchEvent(event);
}
override updated(changedProps: any) {
if (
changedProps.has('invalidText') ||
changedProps.has('internalValidationMsg')
) {
//check if any (internal / external )error msg. present then isInvalid is true
this.isInvalid =
this.invalidText !== '' || this.internalValidationMsg !== ''
? true
: false;
}
if (changedProps.has('value')) {
// set form data value
// this.internals.setFormValue(this.value);
this._validate(false, false);
}
if (
changedProps.has('invalidText') &&
changedProps.get('invalidText') !== undefined
) {
this._validate(false, false);
}
}
private _validate(interacted: Boolean, report: Boolean) {
// get validity state from textareaEl, combine customError flag if invalidText is provided
const Validity =
this.invalidText !== ''
? { ...this.textareaEl.validity, customError: true }
: this.textareaEl.validity;
// set validationMessage to invalidText if present, otherwise use textareaEl validationMessage
const ValidationMessage =
this.invalidText !== ''
? this.invalidText
: this.textareaEl.validationMessage;
// set validity on custom element, anchor to textareaEl
this.internals.setValidity(Validity, ValidationMessage, this.textareaEl);
// set internal validation message if value was changed by user input
if (interacted) {
this.internalValidationMsg = this.textareaEl.validationMessage;
}
// focus the form field to show validity
if (report) {
this.internals.reportValidity();
}
}
private _handleFormdata(e: any) {
e.formData.append(this.name, this.value);
}
private _handleInvalid() {
this._validate(true, false);
}
override connectedCallback(): void {
super.connectedCallback();
if (this.internals.form) {
this.internals.form.addEventListener('formdata', (e) =>
this._handleFormdata(e)
);
}
this.addEventListener('invalid', () => {
this._handleInvalid();
});
}
override disconnectedCallback(): void {
if (this.internals.form) {
this.internals.form.removeEventListener('formdata', (e) =>
this._handleFormdata(e)
);
this.removeEventListener('invalid', () => {
this._handleInvalid();
});
}
super.disconnectedCallback();
}
}
declare global {
interface HTMLElementTagNameMap {
'kyn-text-area': TextArea;
}
}