import { html, css, CSSResult, property } from '@skhemata/skhemata-base'; import { faAngleDown, faSearch } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@riovir/wc-fontawesome'; import { SkhemataFormInput } from './SkhemataFormInput'; export class SkhemataFormAutocomplete extends SkhemataFormInput { static get styles() { return [ ...super.styles, css` .field { margin-bottom: 1rem; } option { text-align: left; padding: 0.5rem 0rem 0.5rem 1rem; white-space: nowrap; } option:hover { background-color: whitesmoke; color: rgb(10, 10, 10); cursor: pointer; } option.is-active { background-color: rgb(50, 115, 220); color: rgb(255, 255, 255); } .results.dropdown-content { max-height: 15em; overflow-y: auto; } .dropdown, .dropdown-trigger, .dropdown button { width: 100%; } .dropdown button span { margin-right: auto; } .selected-dropdown { color: rgb(54, 54, 54); } .dropdown .button { color: #d0d0d0; } #dropdown-menu { width: 100%; padding-top: 0px; } #dropdown-menu .dropdown-content { padding: 0; } #dropdown-menu .dropdown-content .dropdown-item { padding: 5px; } `, ]; } static get scopedElements() { return { 'fa-icon': FontAwesomeIcon, }; } @property({ type: String }) description = ''; @property({ type: String }) selected = ''; @property({ type: String }) mapValue = ''; @property({ type: String }) mapLabel = ''; @property({ type: String }) search = ''; @property({ type: Array }) results: any = []; @property({ type: String }) label = ''; @property({ type: Boolean }) menuOpen = false; @property({ type: Boolean }) required = false; @property({ type: String }) name = 'name'; @property({ type: String }) placeholder = 'Select One'; @property({ type: String }) errorMessage = 'Select one'; @property({ type: Boolean }) submitOnSelect = false; @property({ type: Boolean }) valid = true; @property({ type: String }) helpClass = ''; private _active: number = 0; @property({ type: Boolean, reflect: true }) get active(): number { return this._active; } set active(value: number) { const oldValue = this._active; this._active = value; this.requestUpdate('active', oldValue); this.shadowRoot ?.querySelector('.dropdown-item.is-active') ?.scrollIntoView({ block: 'center', behavior: 'smooth' }); } reset() { this.clearError(); this.value = ''; this.selected = ''; this.results = []; } validate() { this.helpClass = ''; if (this.required && this.value.length < 1) { this.valid = false; this.helpClass = 'is-danger'; this.requestUpdate(); } this.dispatchEvent( new CustomEvent('is-valid', { detail: { valid: this.valid }, bubbles: true, composed: true, }) ); } clearError() { this.helpClass = ''; this.valid = true; this.requestUpdate(); } handleKeydown(event: any) { if (event.target.value.length < 2) { this.reset(); } else { switch (event.keyCode) { case 13: this.value = this.results[this.active].value; this.selected = this.results[this.active].label; this.menuOpen = false; break; case 40: this.active = this.active < this.results.length - 1 ? this.active + 1 : this.active; break; case 38: this.active = this.active > 0 ? this.active - 1 : this.active; break; default: this.active = 0; } } } handleInput(event: any) { this.clearError(); this.setAttribute('search', event.target.value); if (this.search.length > 2) { this.getResults(); } } handleSelectValue(selected: string, value: any) { this.clearError(); this.selected = selected; this.value = value; this.menuOpen = false; } toggleMenu() { this.menuOpen = !this.menuOpen; this.requestUpdate(); } handleFocusOut() { this.toggleMenu(); this.results = []; if (!this.selected) { this.search = ''; } } getResults() { if (this.api?.url) { fetch(`${this.api.url}/locale/city/${this.search}`) .then(data => data.json()) .then(results => { const mapped = this.mapLabel && this.mapValue ? results.map((result: any) => ({ value: result[this.mapValue], label: result[this.mapLabel], })) : results; this.results = mapped; this.requestUpdate(); }); } } async firstUpdated() { // document.addEventListener('click', e=>this.clickOffDropdown(e)); } 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; } }