Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import { classMap } from 'lit-html/directives/class-map'; import { unsafeHTML } from 'lit-html/directives/unsafe-html'; import { html, TemplateResult, property } from 'lit-element'; import { RootElement, htmlElement } from '../../core'; @htmlElement('select-input') export class SelectInput extends RootElement { @property({ type: String, reflect: true }) value: string | undefined | null; @property({ type: Array, reflect: true }) options = []; @property({ type: String, attribute: 'option-template' }) optionTemplate: string | null | undefined; @property({ type: String, attribute: 'value-template' }) valueTemplate: string | null | undefined; @property({ type: String, attribute: 'value-field' }) valueField: string | undefined; @property({ type: String, attribute: 'text-field' }) textField: string | undefined; @property({ type: String, attribute: 'disabled-field' }) disabledField: string | undefined; @property({ type: String }) placeholder = ''; @property({ type: Boolean }) required = false; private valid = false; private invalid = false; private showOptions = false; private selectedOptionIndex = -1; public constructor() { super(); } public connectedCallback(): void { document.addEventListener('click', evt => { if (this.isOutsideEvent(evt)) { this.showOptions = false; this.requestUpdate(); } }); super.connectedCallback(); } protected firstUpdated(): void { this.selectedOptionIndex = this.options.findIndex((option: any) => { if (typeof option !== 'object') { return option === this.value; } else if (this.valueField) { return option[this.valueField] === this.value; } return false; }); const eleInput = this.querySelector('.form-control'); if (eleInput) { eleInput.addEventListener('click', () => { this.showOptions = !this.showOptions; this.requestUpdate(); }); } const eleOption = this.querySelectorAll('.select-option'); Array.from(eleOption).forEach((element: Element) => { element.addEventListener('click', () => { if (element.classList.contains('disabled')) { return; } const val = element.getAttribute('value'); const idx = parseInt(element.getAttribute('data-index') || '-1'); this.value = val; this.showOptions = false; this.selectedOptionIndex = idx; if (this.required) { this.invalid = !this.value; this.valid = !!this.value; } this.requestUpdate(); }); }); } protected render(): TemplateResult { const eleInputClass = { 'form-control': true, 'is-valid': this.valid, 'is-invalid': this.invalid }; const eleOptionsClass = { 'select-options': true, active: this.showOptions }; const valueView = this.selectedOptionIndex > -1 ? this.getHtmlFromTemplate(this.valueTemplate || this.optionTemplate, this.selectedOptionIndex) : this.value ? html` ${this.value} ` : html` <span class="placeholder">${this.placeholder}</span> `; return html` <div class="${classMap(eleInputClass)}" tabindex="0"> ${valueView} </div> <div class="${classMap(eleOptionsClass)}"> ${this.options.map((_option: any, index: number) => this.getHtmlFromTemplate(this.optionTemplate, index))} </div> `; } private getHtmlFromTemplate(template: string | null | undefined, index: number): TemplateResult { const option = this.options[index]; if (!option) { return html``; } const optionClass: any = { 'select-option': true, disabled: this.disabledField && option[this.disabledField], }; let text = this.formatTemplate(template, option); let value = ''; if (typeof option !== 'object') { value = option; if (!text) { text = option; } } else { if (this.valueField) { value = option[this.valueField]; } else { throw new Error(`No value-field attribute found for ${this.tagName} element.`); } if (!text) { if (this.textField) { text = option[this.textField]; } else { throw new Error(`No text-field attribute found for ${this.tagName} element.`); } } } optionClass['active'] = value === this.value; return html` <div class="${classMap(optionClass)}" value="${value}" data-index="${index}">${unsafeHTML(text)}</div> `; } } |