this._handleInput(e)}
/>
${this.isInvalid
? html` `
: null}
${this.value !== ''
? html`
`
: null}
${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);
this._emitValue(e);
}
private _handleClear() {
this.value = '';
this.inputEl.value = '';
this._validate(true, false);
this._emitValue();
}
private _emitValue(e?: any) {
const Detail: any = {
value: this.value,
};
if (e) {
Detail.origEvent = e;
}
const event = new CustomEvent('on-input', {
detail: Detail,
});
this.dispatchEvent(event);
}
private _validate(interacted: Boolean, report: Boolean) {
// get validity state from inputEl, combine customError flag if invalidText is provided
const Validity =
this.invalidText !== ''
? { ...this.inputEl.validity, customError: true }
: this.inputEl.validity;
// set validationMessage to invalidText if present, otherwise use inputEl validationMessage
const ValidationMessage =
this.invalidText !== ''
? this.invalidText
: this.inputEl.validationMessage;
// set validity on custom element, anchor to inputEl
this.internals.setValidity(Validity, ValidationMessage, this.inputEl);
// set internal validation message if value was changed by user input
if (interacted) {
this.internalValidationMsg = this.inputEl.validationMessage;
}
// focus the form field to show validity
if (report) {
this.internals.reportValidity();
}
}
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')) {
this.inputEl.value = this.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);
}
}
override firstUpdated() {
this.determineIfSlotted();
}
private determineIfSlotted() {
this.iconSlotted = this.iconSlot.length ? true : false;
}
private _handleFormdata(e: any) {
e.formData.append(this.name, this.value);
}
private _handleInvalid() {
this._validate(true, false);
}
override connectedCallback(): void {
super.connectedCallback();
console.log('kyn-text-input is connected to the document.');
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-input': TextInput;
}
}