/* * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { BaseElement, element } from '../BaseElement'; let listHeight = ''; let css = ` `; const initHtmlStyle = (height: string): string => { listHeight = height; return css; }; @element('lit-allocation-select') export class LitAllocationSelect extends BaseElement { private selectAllocationInputEl: HTMLInputElement | null | undefined; private selectAllocationInputContent: HTMLDivElement | undefined; private selectAllocationOptions: any; private processDataList: Array = []; static get observedAttributes() { return ['value', 'disabled', 'placeholder']; } get defaultPlaceholder() { return this.getAttribute('placeholder') || ''; } get placeholder() { return this.getAttribute('placeholder') || this.defaultPlaceholder; } set placeholder(selectAllocationValue) { this.setAttribute('placeholder', selectAllocationValue); } get value() { return this.getAttribute('value') || ''; } set value(selectAllocationValue: string) { this.setAttribute('value', selectAllocationValue); } set processData(value: Array) { this.processDataList = value; this.selectAllocationOptions.innerHTML = ''; value.forEach((item) => { let option = document.createElement('div'); option.className = 'option'; option.innerHTML = item; option.style.padding = '8px 10px'; this.selectAllocationOptions.appendChild(option); this.selectAllocationInputEl?.focus(); }); } get placement(): string { return this.getAttribute('placement') || ''; } set placement(selectAllocationValuePlacement: string) { if (selectAllocationValuePlacement) { this.setAttribute('placement', selectAllocationValuePlacement); } else { this.removeAttribute('placement'); } } get listHeight() { return this.getAttribute('list-height') || '256px'; } set listHeight(value) { this.setAttribute('list-height', value); } attributeChangedCallback(name: any, oldValue: any, newValue: any) { switch (name) { case 'value': this.selectAllocationInputEl!.value = newValue; break; case 'placeholder': this.selectAllocationInputEl!.placeholder = newValue; break; } } initElements(): void { this.selectAllocationInputContent = this.shadowRoot!.querySelector('.multipleSelect') as HTMLDivElement; this.addEventListener('click', () => { if (this.selectAllocationOptions.style.visibility == 'visible') { this.selectAllocationOptions.style.visibility = 'hidden'; this.selectAllocationOptions.style.opacity = '0'; } else { this.showProcessList(); } this.selectAllocationInputContent!.dispatchEvent(new CustomEvent('inputClick', {})); }); this.addEventListener('focusout', (e) => { this.selectAllocationOptions.style.visibility = 'hidden'; this.selectAllocationOptions.style.opacity = '0'; }); this.initData(); } showProcessList() { setTimeout(() => { if (this.processDataList.length > 0) { this.selectAllocationOptions.style.visibility = 'visible'; this.selectAllocationOptions.style.opacity = '1'; } }, 200); } initHtml() { return ` ${initHtmlStyle(this.listHeight)}
`; } connectedCallback() { this.selectAllocationInputEl!.onkeydown = (ev): void => { // @ts-ignore if (ev.key === '0' && ev.target.value.length === 1 && ev.target.value === '0') { ev.preventDefault(); } }; } initData() { this.selectAllocationInputEl = this.shadowRoot!.querySelector('input'); this.selectAllocationOptions = this.shadowRoot!.querySelector('.body') as HTMLDivElement; this.selectAllocationInputEl?.addEventListener('input', () => { let filter = [...this.shadowRoot!.querySelectorAll('.option')].filter((a: HTMLDivElement) => { if (a.textContent!.indexOf(this.selectAllocationInputEl!.value) <= -1) { a.style.display = 'none'; } else { a.style.display = 'block'; } }); this.value = this.selectAllocationInputEl!.value; this.selectAllocationInputContent!.dispatchEvent(new CustomEvent('valuable', {})); }); this.shadowRoot?.querySelectorAll('.option').forEach((a) => { a.addEventListener('mousedown', (e) => { a.dispatchEvent( new CustomEvent('onSelected', { detail: { selected: true, text: a.textContent, }, }) ); }); a.addEventListener('onSelected', (e: any) => { this.selectAllocationInputEl!.value = e.detail.text; this.value = e.detail.text; this.selectAllocationInputContent!.dispatchEvent(new CustomEvent('valuable', {})); }); }); } }