@skhemata/skhemata-form
Version:
Skhemata Form Web Component. This web component can be used as base web component when working with forms and inputs.
111 lines (90 loc) • 2.34 kB
text/typescript
import {
css,
CSSResult,
SkhemataBase,
property,
unsafeHTML,
} from '@skhemata/skhemata-base';
import { icon } from '@fortawesome/fontawesome-svg-core';
import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons';
export abstract class SkhemataFormInput extends SkhemataBase {
static get styles() {
return <CSSResult[]>[
...super.styles,
css`
.field {
margin-bottom: 1rem;
},
`,
];
}
private _value: any = null;
faTriangle: any = unsafeHTML(
icon(faExclamationTriangle, {
transform: {
size: 7,
},
}).html[0]
);
get value() {
return this._value;
}
set value(value: any) {
const oldValue = this._value;
this._value = value;
this.dispatchEvent(
new CustomEvent('change', {
detail: {
name: this.name,
value,
},
})
);
this.requestUpdate('value', oldValue);
}
label = '';
horizontal = false;
description = '';
required = false;
name = '';
placeholder = '';
errorMessage = '';
valid = true;
helpClass = '';
translationData = {
eng: {
formErrorMinlength: 'Text must be longer',
formErrorMaxlength: 'Text must be shorter',
formErrorEmail: 'Not a valid email',
formErrorRequired: 'This is required',
},
};
constructor() {
super();
this.addEventListener('validate', this.validate);
this.addEventListener('reset', this.reset);
}
async firstUpdated() {
super.firstUpdated();
}
reset() {
this.clearError();
this.setAttribute('value', '');
}
validate() {
if (this.required && !this.value) {
this.valid = false;
this.requestUpdate();
}
}
clearError() {
this.helpClass = '';
this.valid = true;
this.requestUpdate();
}
}