import { html, property } from '@skhemata/skhemata-base'; import { SkhemataFormInput } from './SkhemataFormInput'; export class SkhemataFormCheckbox extends SkhemataFormInput { @property({ type: Boolean }) checked: boolean = false; constructor() { super(); this.value = false; } reset() { this.clearError(); this.checked = false; this.value = false; } validate() { this.helpClass = ''; if (this.required && this.checked === false) { this.valid = false; this.helpClass = 'is-danger'; this.errorMessage = this.getStr('formErrorRequired'); this.requestUpdate(); } this.dispatchEvent( new CustomEvent('is-valid', { detail: { valid: this.valid }, bubbles: true, composed: true, }) ); } handleClick(event: any) { this.clearError(); this.checked = event.target.checked; this.value = event.target.checked; } updated() { const checkbox = this.shadowRoot.querySelector('[type=checkbox]'); checkbox['checked'] = this.value; } render() { const field = html`
${ this.label && !this.horizontal ? html`` : null } ${ this.description && !this.horizontal ? html`

${this.description}

` : null } ${ !this.valid ? html`

${this.errorMessage}

` : `` }
`; const horizontalFieldLabel = html`
${this.label ? html`` : null} ${this.description ? html`

${this.description}

` : null}
`; const horizontalField = html`
${this.label || this.description ? horizontalFieldLabel : null}
${field}
`; return this.horizontal ? horizontalField : field; } }