import { html, css, CSSResult, property } from '@skhemata/skhemata-base'; import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@riovir/wc-fontawesome'; import { SkhemataFormInput } from './SkhemataFormInput'; export class SkhemataFormTextarea extends SkhemataFormInput { static get styles() { return [ ...super.styles, css` .field { margin-bottom: 1rem; } `, ]; } static get scopedElements() { return { 'fa-icon': FontAwesomeIcon, }; } @property({ type: String }) label = ''; @property({ type: String }) description = ''; @property({ type: Boolean }) horizontal = false; @property({ type: Number }) rows = 4; @property({ type: Number }) minlength = -1; @property({ type: Number }) maxlength = -1; @property({ type: Boolean }) required = false; @property({ type: String }) name = 'name'; @property({ type: String }) placeholder = ''; @property({ type: String }) errorMessage = 'Text is invalid'; @property({ type: Boolean }) submitOnEnter = false; @property({ type: Boolean }) valid = true; @property({ type: String }) helpClass = ''; reset() { this.clearError(); this.setAttribute('value', ''); } validate() { this.helpClass = ''; if ( (this.minlength !== -1 && this.value.length < this.minlength) || (this.maxlength !== -1 && this.value.length > this.maxlength) || (this.required && this.value.length < 1) ) { this.valid = false; this.helpClass = 'is-danger'; this.errorMessage = 'text is invalid'; this.requestUpdate(); } this.dispatchEvent( new CustomEvent('is-valid', { detail: { valid: this.valid }, bubbles: true, composed: true, }) ); } clearError() { this.helpClass = ''; this.errorMessage = ''; this.valid = true; this.requestUpdate(); } handleInput(event: any) { this.clearError(); this.setAttribute('value', event.target.value); } handleKeydown(event: any) { this.clearError(); if (event.keyCode === '13' && this.submitOnEnter) { this.validate(); if (!this.valid) { return; } this.dispatchEvent(new CustomEvent('submit')); event.preventDefault(); } } render() { const field = html` ${this.label && !this.horizontal ? html`${this.label} ${this.required ? html`*` : null}` : null} ${this.description && !this.horizontal ? html`${this.description}` : null} ${!this.valid ? html` ` : null} ${!this.valid ? html`${this.errorMessage}` : ``} `; const horizontalFieldLabel = html` ${this.label ? html`${this.label} ${this.required ? html`*` : null}` : null} ${this.description ? html`${this.description}` : null} `; const horizontalField = html` ${this.label || this.description ? horizontalFieldLabel : null} ${field} `; return this.horizontal ? horizontalField : field; } }
${this.description}
${this.errorMessage}