1fb726d48Sopenharmony_ci/* 2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb726d48Sopenharmony_ci * You may obtain a copy of the License at 6fb726d48Sopenharmony_ci * 7fb726d48Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb726d48Sopenharmony_ci * 9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and 13fb726d48Sopenharmony_ci * limitations under the License. 14fb726d48Sopenharmony_ci */ 15fb726d48Sopenharmony_ci 16fb726d48Sopenharmony_ciimport { BaseElement, element } from '../BaseElement'; 17fb726d48Sopenharmony_ciimport { selectHtmlStr } from './LitSelectHtml'; 18fb726d48Sopenharmony_ciimport { LitSelectOption } from './LitSelectOption'; 19fb726d48Sopenharmony_ciimport { SpSystemTrace } from '../../trace/component/SpSystemTrace'; 20fb726d48Sopenharmony_ci 21fb726d48Sopenharmony_ci@element('lit-select') 22fb726d48Sopenharmony_ciexport class LitSelect extends BaseElement { 23fb726d48Sopenharmony_ci private focused: unknown; 24fb726d48Sopenharmony_ci private selectInputEl: unknown; 25fb726d48Sopenharmony_ci private selectSearchInputEl: HTMLInputElement | null | undefined; 26fb726d48Sopenharmony_ci private selectVInputEl: HTMLInputElement | null | undefined; 27fb726d48Sopenharmony_ci private selectOptions: HTMLDivElement | null | undefined; 28fb726d48Sopenharmony_ci private selectItem: string = ''; 29fb726d48Sopenharmony_ci private selectClearEl: unknown; 30fb726d48Sopenharmony_ci private selectIconEl: unknown; 31fb726d48Sopenharmony_ci private bodyEl: unknown; 32fb726d48Sopenharmony_ci private selectSearchEl: unknown; 33fb726d48Sopenharmony_ci private selectMultipleRootEl: unknown; 34fb726d48Sopenharmony_ci private currentSelectedValue: string = ''; 35fb726d48Sopenharmony_ci 36fb726d48Sopenharmony_ci static get observedAttributes(): string[] { 37fb726d48Sopenharmony_ci return [ 38fb726d48Sopenharmony_ci 'value', 39fb726d48Sopenharmony_ci 'default-value', 40fb726d48Sopenharmony_ci 'placeholder', 41fb726d48Sopenharmony_ci 'disabled', 42fb726d48Sopenharmony_ci 'loading', 43fb726d48Sopenharmony_ci 'allow-clear', 44fb726d48Sopenharmony_ci 'show-search', 45fb726d48Sopenharmony_ci 'list-height', 46fb726d48Sopenharmony_ci 'border', 47fb726d48Sopenharmony_ci 'mode', 48fb726d48Sopenharmony_ci 'showSearchInput', 49fb726d48Sopenharmony_ci 'tabSelect' 50fb726d48Sopenharmony_ci ]; 51fb726d48Sopenharmony_ci } 52fb726d48Sopenharmony_ci 53fb726d48Sopenharmony_ci get value(): string { 54fb726d48Sopenharmony_ci return this.getAttribute('value') || this.defaultValue; 55fb726d48Sopenharmony_ci } 56fb726d48Sopenharmony_ci 57fb726d48Sopenharmony_ci set value(selectValue) { 58fb726d48Sopenharmony_ci this.setAttribute('value', selectValue); 59fb726d48Sopenharmony_ci } 60fb726d48Sopenharmony_ci 61fb726d48Sopenharmony_ci get rounded(): boolean { 62fb726d48Sopenharmony_ci return this.hasAttribute('rounded'); 63fb726d48Sopenharmony_ci } 64fb726d48Sopenharmony_ci 65fb726d48Sopenharmony_ci set rounded(selectRounded: boolean) { 66fb726d48Sopenharmony_ci if (selectRounded) { 67fb726d48Sopenharmony_ci this.setAttribute('rounded', ''); 68fb726d48Sopenharmony_ci } else { 69fb726d48Sopenharmony_ci this.removeAttribute('rounded'); 70fb726d48Sopenharmony_ci } 71fb726d48Sopenharmony_ci } 72fb726d48Sopenharmony_ci 73fb726d48Sopenharmony_ci get placement(): string { 74fb726d48Sopenharmony_ci return this.getAttribute('placement') || ''; 75fb726d48Sopenharmony_ci } 76fb726d48Sopenharmony_ci 77fb726d48Sopenharmony_ci set placement(selectPlacement: string) { 78fb726d48Sopenharmony_ci if (selectPlacement) { 79fb726d48Sopenharmony_ci this.setAttribute('placement', selectPlacement); 80fb726d48Sopenharmony_ci } else { 81fb726d48Sopenharmony_ci this.removeAttribute('placement'); 82fb726d48Sopenharmony_ci } 83fb726d48Sopenharmony_ci } 84fb726d48Sopenharmony_ci 85fb726d48Sopenharmony_ci get border(): string { 86fb726d48Sopenharmony_ci return this.getAttribute('border') || 'true'; 87fb726d48Sopenharmony_ci } 88fb726d48Sopenharmony_ci 89fb726d48Sopenharmony_ci set border(selectBorder) { 90fb726d48Sopenharmony_ci if (selectBorder) { 91fb726d48Sopenharmony_ci this.setAttribute('border', 'true'); 92fb726d48Sopenharmony_ci } else { 93fb726d48Sopenharmony_ci this.setAttribute('border', 'false'); 94fb726d48Sopenharmony_ci } 95fb726d48Sopenharmony_ci } 96fb726d48Sopenharmony_ci 97fb726d48Sopenharmony_ci get listHeight(): string { 98fb726d48Sopenharmony_ci return this.getAttribute('list-height') || '256px'; 99fb726d48Sopenharmony_ci } 100fb726d48Sopenharmony_ci 101fb726d48Sopenharmony_ci set listHeight(selectListHeight) { 102fb726d48Sopenharmony_ci this.setAttribute('list-height', selectListHeight); 103fb726d48Sopenharmony_ci } 104fb726d48Sopenharmony_ci 105fb726d48Sopenharmony_ci get defaultPlaceholder(): string { 106fb726d48Sopenharmony_ci return this.getAttribute('placeholder') || '请选择'; 107fb726d48Sopenharmony_ci } 108fb726d48Sopenharmony_ci 109fb726d48Sopenharmony_ci set canInsert(can: boolean) { 110fb726d48Sopenharmony_ci if (can) { 111fb726d48Sopenharmony_ci this.setAttribute('canInsert', ''); 112fb726d48Sopenharmony_ci } else { 113fb726d48Sopenharmony_ci this.removeAttribute('canInsert'); 114fb726d48Sopenharmony_ci } 115fb726d48Sopenharmony_ci } 116fb726d48Sopenharmony_ci 117fb726d48Sopenharmony_ci get canInsert(): boolean { 118fb726d48Sopenharmony_ci return this.hasAttribute('canInsert'); 119fb726d48Sopenharmony_ci } 120fb726d48Sopenharmony_ci get showSearch(): boolean { 121fb726d48Sopenharmony_ci return this.hasAttribute('show-search'); 122fb726d48Sopenharmony_ci } 123fb726d48Sopenharmony_ci 124fb726d48Sopenharmony_ci get defaultValue(): string { 125fb726d48Sopenharmony_ci return this.getAttribute('default-value') || ''; 126fb726d48Sopenharmony_ci } 127fb726d48Sopenharmony_ci 128fb726d48Sopenharmony_ci set defaultValue(selectDefaultValue) { 129fb726d48Sopenharmony_ci this.setAttribute('default-value', selectDefaultValue); 130fb726d48Sopenharmony_ci } 131fb726d48Sopenharmony_ci 132fb726d48Sopenharmony_ci get placeholder(): string { 133fb726d48Sopenharmony_ci return this.getAttribute('placeholder') || this.defaultPlaceholder; 134fb726d48Sopenharmony_ci } 135fb726d48Sopenharmony_ci 136fb726d48Sopenharmony_ci set placeholder(selectPlaceHolder) { 137fb726d48Sopenharmony_ci this.setAttribute('placeholder', selectPlaceHolder); 138fb726d48Sopenharmony_ci } 139fb726d48Sopenharmony_ci 140fb726d48Sopenharmony_ci get loading(): boolean { 141fb726d48Sopenharmony_ci return this.hasAttribute('loading'); 142fb726d48Sopenharmony_ci } 143fb726d48Sopenharmony_ci 144fb726d48Sopenharmony_ci set loading(selectLoading) { 145fb726d48Sopenharmony_ci if (selectLoading) { 146fb726d48Sopenharmony_ci this.setAttribute('loading', ''); 147fb726d48Sopenharmony_ci } else { 148fb726d48Sopenharmony_ci this.removeAttribute('loading'); 149fb726d48Sopenharmony_ci } 150fb726d48Sopenharmony_ci } 151fb726d48Sopenharmony_ci 152fb726d48Sopenharmony_ci get showSearchInput(): boolean { 153fb726d48Sopenharmony_ci return this.hasAttribute('showSearchInput'); 154fb726d48Sopenharmony_ci } 155fb726d48Sopenharmony_ci 156fb726d48Sopenharmony_ci set showSearchInput(isHide: boolean) { 157fb726d48Sopenharmony_ci if (isHide) { 158fb726d48Sopenharmony_ci this.setAttribute('showSearchInput', ''); 159fb726d48Sopenharmony_ci } else { 160fb726d48Sopenharmony_ci this.removeAttribute('showSearchInput'); 161fb726d48Sopenharmony_ci } 162fb726d48Sopenharmony_ci } 163fb726d48Sopenharmony_ci 164fb726d48Sopenharmony_ci set showItem(item: string) { 165fb726d48Sopenharmony_ci this.selectItem = item; 166fb726d48Sopenharmony_ci } 167fb726d48Sopenharmony_ci 168fb726d48Sopenharmony_ci set dataSource(selectDataSource: unknown) { 169fb726d48Sopenharmony_ci this.innerHTML = '<slot></slot><slot name="footer"></slot>'; // @ts-ignore 170fb726d48Sopenharmony_ci if (selectDataSource.length > 0) { 171fb726d48Sopenharmony_ci // @ts-ignore 172fb726d48Sopenharmony_ci this.bodyEl!.style.display = 'flex'; 173fb726d48Sopenharmony_ci this.querySelectorAll('lit-select-option').forEach((a) => { 174fb726d48Sopenharmony_ci this.removeChild(a); 175fb726d48Sopenharmony_ci }); 176fb726d48Sopenharmony_ci let valuesSet = new Set(); 177fb726d48Sopenharmony_ci let flag = true; // 假设所有 value 都是唯一的 178fb726d48Sopenharmony_ci // @ts-ignore 179fb726d48Sopenharmony_ci selectDataSource.forEach(item => { 180fb726d48Sopenharmony_ci if (valuesSet.has(item.value)) { 181fb726d48Sopenharmony_ci flag = false; // 如果value有重复,就设置flag为false 182fb726d48Sopenharmony_ci return; 183fb726d48Sopenharmony_ci } 184fb726d48Sopenharmony_ci valuesSet.add(item.value); 185fb726d48Sopenharmony_ci }); 186fb726d48Sopenharmony_ci // @ts-ignore 187fb726d48Sopenharmony_ci selectDataSource.forEach((dateSourceBean: unknown) => { 188fb726d48Sopenharmony_ci if (dateSourceBean) { 189fb726d48Sopenharmony_ci let selectOption = document.createElement('lit-select-option'); 190fb726d48Sopenharmony_ci let optionData = { 191fb726d48Sopenharmony_ci // @ts-ignore 192fb726d48Sopenharmony_ci value: dateSourceBean.value ? dateSourceBean.value : dateSourceBean.name || dateSourceBean, // @ts-ignore 193fb726d48Sopenharmony_ci name: dateSourceBean.name ? dateSourceBean.name : dateSourceBean, 194fb726d48Sopenharmony_ci }; 195fb726d48Sopenharmony_ci if (!flag) { // 如果数组的value值不是唯一的,就用name做为value值,避免多个选项被选中 196fb726d48Sopenharmony_ci optionData = { 197fb726d48Sopenharmony_ci // @ts-ignore 198fb726d48Sopenharmony_ci value: dateSourceBean.name ? dateSourceBean.name : dateSourceBean, // @ts-ignore 199fb726d48Sopenharmony_ci name: dateSourceBean.name ? dateSourceBean.name : dateSourceBean, 200fb726d48Sopenharmony_ci }; 201fb726d48Sopenharmony_ci } 202fb726d48Sopenharmony_ci selectOption.textContent = optionData.name; 203fb726d48Sopenharmony_ci selectOption.setAttribute('value', optionData.value); 204fb726d48Sopenharmony_ci if (this.currentSelectedValue === optionData.value) { 205fb726d48Sopenharmony_ci selectOption.setAttribute('selected', ''); 206fb726d48Sopenharmony_ci } 207fb726d48Sopenharmony_ci // @ts-ignore 208fb726d48Sopenharmony_ci this.selectInputEl!.value = ''; 209fb726d48Sopenharmony_ci this.append(selectOption); 210fb726d48Sopenharmony_ci } 211fb726d48Sopenharmony_ci }); 212fb726d48Sopenharmony_ci this.initOptions(); 213fb726d48Sopenharmony_ci } else { 214fb726d48Sopenharmony_ci // @ts-ignore 215fb726d48Sopenharmony_ci this.bodyEl!.style.display = 'none'; 216fb726d48Sopenharmony_ci } 217fb726d48Sopenharmony_ci } 218fb726d48Sopenharmony_ci 219fb726d48Sopenharmony_ci initElements(): void { 220fb726d48Sopenharmony_ci this.selectVInputEl = this.shadowRoot!.querySelector('#select-input') as HTMLInputElement; 221fb726d48Sopenharmony_ci this.selectVInputEl?.addEventListener('keyup', (e) => { 222fb726d48Sopenharmony_ci if (e.code === 'Enter' || e.code === 'NumpadEnter') {// @ts-ignore 223fb726d48Sopenharmony_ci this.selectVInputEl.blur();// @ts-ignore 224fb726d48Sopenharmony_ci this.selectInputEl.value = this.selectVInputEl.value; 225fb726d48Sopenharmony_ci } 226fb726d48Sopenharmony_ci }); 227fb726d48Sopenharmony_ci if (this.showSearchInput) { 228fb726d48Sopenharmony_ci this.shadowRoot!.querySelector<HTMLDivElement>('.body-select')!.style.display = 'block'; 229fb726d48Sopenharmony_ci this.selectSearchInputEl = this.shadowRoot!.querySelector('#search-input') as HTMLInputElement; 230fb726d48Sopenharmony_ci this.selectSearchInputEl?.addEventListener('keyup', (evt) => { 231fb726d48Sopenharmony_ci let options = []; 232fb726d48Sopenharmony_ci options = [...this.querySelectorAll<LitSelectOption>('lit-select-option')]; 233fb726d48Sopenharmony_ci options.filter((a: LitSelectOption) => { 234fb726d48Sopenharmony_ci if (a.textContent!.indexOf(this.selectSearchInputEl!.value) <= -1) { 235fb726d48Sopenharmony_ci a.style.display = 'none'; 236fb726d48Sopenharmony_ci } else { 237fb726d48Sopenharmony_ci a.style.display = 'flex'; 238fb726d48Sopenharmony_ci } 239fb726d48Sopenharmony_ci }); 240fb726d48Sopenharmony_ci evt.preventDefault(); 241fb726d48Sopenharmony_ci evt.stopPropagation(); 242fb726d48Sopenharmony_ci }); 243fb726d48Sopenharmony_ci } 244fb726d48Sopenharmony_ci } 245fb726d48Sopenharmony_ci 246fb726d48Sopenharmony_ci initHtml(): string { 247fb726d48Sopenharmony_ci return ` 248fb726d48Sopenharmony_ci ${selectHtmlStr(this.listHeight)} 249fb726d48Sopenharmony_ci <div class="root noSelect" tabindex="0" hidefocus="true"> 250fb726d48Sopenharmony_ci <div class="multipleRoot"> 251fb726d48Sopenharmony_ci <input id="select-input" placeholder="${this.placeholder}" autocomplete="off" ${this.showSearch || this.canInsert ? '' : 'readonly'} tabindex="0"> 252fb726d48Sopenharmony_ci </div> 253fb726d48Sopenharmony_ci <lit-loading class="loading" size="12"></lit-loading> 254fb726d48Sopenharmony_ci <lit-icon class="icon" name='down' color="#c3c3c3"></lit-icon> 255fb726d48Sopenharmony_ci <lit-icon class="clear" name='close-circle-fill'></lit-icon> 256fb726d48Sopenharmony_ci <lit-icon class="search" name='search'></lit-icon> 257fb726d48Sopenharmony_ci </div> 258fb726d48Sopenharmony_ci <div class="body"> 259fb726d48Sopenharmony_ci <div class="body-select" style="display: none;"> 260fb726d48Sopenharmony_ci <input id="search-input" placeholder="Search"> 261fb726d48Sopenharmony_ci </div> 262fb726d48Sopenharmony_ci <div class="body-opt"> 263fb726d48Sopenharmony_ci <slot></slot> 264fb726d48Sopenharmony_ci <slot name="footer"></slot> 265fb726d48Sopenharmony_ci </div> 266fb726d48Sopenharmony_ci </div> 267fb726d48Sopenharmony_ci `; 268fb726d48Sopenharmony_ci } 269fb726d48Sopenharmony_ci 270fb726d48Sopenharmony_ci isMultiple(): boolean { 271fb726d48Sopenharmony_ci return this.hasAttribute('mode') && this.getAttribute('mode') === 'multiple'; 272fb726d48Sopenharmony_ci } 273fb726d48Sopenharmony_ci 274fb726d48Sopenharmony_ci newTag(value: unknown, text: unknown): HTMLDivElement { 275fb726d48Sopenharmony_ci let tag: unknown = document.createElement('div'); 276fb726d48Sopenharmony_ci let icon: unknown = document.createElement('lit-icon'); // @ts-ignore 277fb726d48Sopenharmony_ci icon.classList.add('tag-close'); // @ts-ignore 278fb726d48Sopenharmony_ci icon.name = 'close'; 279fb726d48Sopenharmony_ci let span = document.createElement('span'); // @ts-ignore 280fb726d48Sopenharmony_ci tag.classList.add('tag'); // @ts-ignore 281fb726d48Sopenharmony_ci span.dataset.value = value; // @ts-ignore 282fb726d48Sopenharmony_ci span.textContent = text; // @ts-ignore 283fb726d48Sopenharmony_ci tag.append(span); // @ts-ignore 284fb726d48Sopenharmony_ci tag.append(icon); // @ts-ignore 285fb726d48Sopenharmony_ci icon.onclick = (ev: unknown): void => { 286fb726d48Sopenharmony_ci // @ts-ignore 287fb726d48Sopenharmony_ci tag.parentElement.removeChild(tag); 288fb726d48Sopenharmony_ci this.querySelector(`lit-select-option[value=${value}]`)!.removeAttribute('selected'); 289fb726d48Sopenharmony_ci if (this.shadowRoot!.querySelectorAll('.tag').length === 0) { 290fb726d48Sopenharmony_ci // @ts-ignore 291fb726d48Sopenharmony_ci this.selectInputEl.style.width = 'auto'; // @ts-ignore 292fb726d48Sopenharmony_ci this.selectInputEl.placeholder = this.defaultPlaceholder; 293fb726d48Sopenharmony_ci } // @ts-ignore 294fb726d48Sopenharmony_ci ev.stopPropagation(); 295fb726d48Sopenharmony_ci }; // @ts-ignore 296fb726d48Sopenharmony_ci tag.value = value; // @ts-ignore 297fb726d48Sopenharmony_ci tag.dataset.value = value; // @ts-ignore 298fb726d48Sopenharmony_ci tag.text = text; // @ts-ignore 299fb726d48Sopenharmony_ci tag.dataset.text = text; // @ts-ignore 300fb726d48Sopenharmony_ci return tag; 301fb726d48Sopenharmony_ci } 302fb726d48Sopenharmony_ci 303fb726d48Sopenharmony_ci connectedCallback(): void { 304fb726d48Sopenharmony_ci this.tabIndex = 0; 305fb726d48Sopenharmony_ci this.focused = false; 306fb726d48Sopenharmony_ci this.bodyEl = this.shadowRoot!.querySelector('.body'); 307fb726d48Sopenharmony_ci this.selectInputEl = this.shadowRoot!.querySelector('input'); 308fb726d48Sopenharmony_ci this.selectClearEl = this.shadowRoot!.querySelector('.clear'); 309fb726d48Sopenharmony_ci this.selectIconEl = this.shadowRoot!.querySelector('.icon'); 310fb726d48Sopenharmony_ci this.selectSearchEl = this.shadowRoot!.querySelector('.search'); 311fb726d48Sopenharmony_ci this.selectMultipleRootEl = this.shadowRoot!.querySelector('.multipleRoot'); 312fb726d48Sopenharmony_ci this.selectOptions = this.shadowRoot!.querySelector('.body-opt') as HTMLDivElement; 313fb726d48Sopenharmony_ci this.setEventClick(); 314fb726d48Sopenharmony_ci this.setEvent(); // @ts-ignore 315fb726d48Sopenharmony_ci this.selectInputEl.onblur = (ev: unknown): void => { 316fb726d48Sopenharmony_ci if (this.hasAttribute('disabled')) { 317fb726d48Sopenharmony_ci return; 318fb726d48Sopenharmony_ci } 319fb726d48Sopenharmony_ci if (this.isMultiple()) { 320fb726d48Sopenharmony_ci if (this.hasAttribute('show-search')) { 321fb726d48Sopenharmony_ci // @ts-ignore 322fb726d48Sopenharmony_ci this.selectSearchEl.style.display = 'none'; // @ts-ignore 323fb726d48Sopenharmony_ci this.selectIconEl.style.display = 'flex'; 324fb726d48Sopenharmony_ci } 325fb726d48Sopenharmony_ci } else { 326fb726d48Sopenharmony_ci // @ts-ignore 327fb726d48Sopenharmony_ci if (this.selectInputEl.placeholder !== this.defaultPlaceholder) { 328fb726d48Sopenharmony_ci // @ts-ignore 329fb726d48Sopenharmony_ci this.selectInputEl.value = this.selectInputEl.placeholder; // @ts-ignore 330fb726d48Sopenharmony_ci this.selectInputEl.placeholder = this.defaultPlaceholder; 331fb726d48Sopenharmony_ci } 332fb726d48Sopenharmony_ci if (this.hasAttribute('show-search')) { 333fb726d48Sopenharmony_ci // @ts-ignore 334fb726d48Sopenharmony_ci this.selectSearchEl.style.display = 'none'; // @ts-ignore 335fb726d48Sopenharmony_ci this.selectIconEl.style.display = 'flex'; 336fb726d48Sopenharmony_ci } 337fb726d48Sopenharmony_ci } 338fb726d48Sopenharmony_ci }; 339fb726d48Sopenharmony_ci this.setOninput(); 340fb726d48Sopenharmony_ci this.setOnkeydown(); 341fb726d48Sopenharmony_ci } 342fb726d48Sopenharmony_ci 343fb726d48Sopenharmony_ci setOninput(): void { 344fb726d48Sopenharmony_ci // @ts-ignore 345fb726d48Sopenharmony_ci this.selectInputEl.oninput = (ev: unknown): void => { 346fb726d48Sopenharmony_ci let els: Element[] = [...this.querySelectorAll('lit-select-option')]; 347fb726d48Sopenharmony_ci if (this.hasAttribute('show-search')) { 348fb726d48Sopenharmony_ci // @ts-ignore 349fb726d48Sopenharmony_ci if (!ev.target.value) { 350fb726d48Sopenharmony_ci // @ts-ignore 351fb726d48Sopenharmony_ci els.forEach((a: unknown) => (a.style.display = 'flex')); 352fb726d48Sopenharmony_ci } else { 353fb726d48Sopenharmony_ci this.setSelectItem(els, ev); 354fb726d48Sopenharmony_ci } 355fb726d48Sopenharmony_ci } else { 356fb726d48Sopenharmony_ci // @ts-ignore 357fb726d48Sopenharmony_ci this.value = ev.target.value; 358fb726d48Sopenharmony_ci } 359fb726d48Sopenharmony_ci }; 360fb726d48Sopenharmony_ci } 361fb726d48Sopenharmony_ci 362fb726d48Sopenharmony_ci setSelectItem(els: Element[], ev: unknown): void { 363fb726d48Sopenharmony_ci els.forEach((a: unknown) => { 364fb726d48Sopenharmony_ci // @ts-ignore 365fb726d48Sopenharmony_ci let value = a.getAttribute('value'); 366fb726d48Sopenharmony_ci if ( 367fb726d48Sopenharmony_ci // @ts-ignore 368fb726d48Sopenharmony_ci value.toLowerCase().indexOf(ev.target.value.toLowerCase()) !== -1 || // @ts-ignore 369fb726d48Sopenharmony_ci a.textContent.toLowerCase().indexOf(ev.target.value.toLowerCase()) !== -1 370fb726d48Sopenharmony_ci ) { 371fb726d48Sopenharmony_ci // @ts-ignore 372fb726d48Sopenharmony_ci a.style.display = 'flex'; 373fb726d48Sopenharmony_ci } else { 374fb726d48Sopenharmony_ci // @ts-ignore 375fb726d48Sopenharmony_ci a.style.display = 'none'; 376fb726d48Sopenharmony_ci } 377fb726d48Sopenharmony_ci }); 378fb726d48Sopenharmony_ci } 379fb726d48Sopenharmony_ci 380fb726d48Sopenharmony_ci setEventClick(): void { 381fb726d48Sopenharmony_ci // @ts-ignore 382fb726d48Sopenharmony_ci this.selectClearEl.onclick = (ev: unknown): void => { 383fb726d48Sopenharmony_ci if (this.isMultiple()) { 384fb726d48Sopenharmony_ci let delNodes: Array<unknown> = []; // @ts-ignore 385fb726d48Sopenharmony_ci this.selectMultipleRootEl.childNodes.forEach((a: unknown) => { 386fb726d48Sopenharmony_ci // @ts-ignore 387fb726d48Sopenharmony_ci if (a.tagName === 'DIV') { 388fb726d48Sopenharmony_ci delNodes.push(a); 389fb726d48Sopenharmony_ci } 390fb726d48Sopenharmony_ci }); 391fb726d48Sopenharmony_ci for (let i = 0; i < delNodes.length; i++) { 392fb726d48Sopenharmony_ci // @ts-ignore 393fb726d48Sopenharmony_ci delNodes[i].remove(); 394fb726d48Sopenharmony_ci } 395fb726d48Sopenharmony_ci if (this.shadowRoot!.querySelectorAll('.tag').length === 0) { 396fb726d48Sopenharmony_ci // @ts-ignore 397fb726d48Sopenharmony_ci this.selectInputEl.style.width = 'auto'; // @ts-ignore 398fb726d48Sopenharmony_ci this.selectInputEl.placeholder = this.defaultPlaceholder; 399fb726d48Sopenharmony_ci } 400fb726d48Sopenharmony_ci } 401fb726d48Sopenharmony_ci this.querySelectorAll('lit-select-option').forEach((a) => a.removeAttribute('selected')); // @ts-ignore 402fb726d48Sopenharmony_ci this.selectInputEl.value = ''; // @ts-ignore 403fb726d48Sopenharmony_ci this.selectClearEl.style.display = 'none'; // @ts-ignore 404fb726d48Sopenharmony_ci this.selectIconEl.style.display = 'flex'; 405fb726d48Sopenharmony_ci this.blur(); // @ts-ignore 406fb726d48Sopenharmony_ci ev.stopPropagation(); 407fb726d48Sopenharmony_ci this.dispatchEvent(new CustomEvent('onClear', { detail: ev })); 408fb726d48Sopenharmony_ci }; 409fb726d48Sopenharmony_ci this.initOptions(); 410fb726d48Sopenharmony_ci this.onclick = (ev: unknown): void => { 411fb726d48Sopenharmony_ci // @ts-ignore 412fb726d48Sopenharmony_ci if (ev.target.tagName === 'LIT-SELECT') { 413fb726d48Sopenharmony_ci if (this.focused === false) { 414fb726d48Sopenharmony_ci // @ts-ignore 415fb726d48Sopenharmony_ci this.selectInputEl.focus(); 416fb726d48Sopenharmony_ci this.focused = true; // @ts-ignore 417fb726d48Sopenharmony_ci this.bodyEl!.style.display = 'flex'; 418fb726d48Sopenharmony_ci } else { 419fb726d48Sopenharmony_ci this.focused = false; 420fb726d48Sopenharmony_ci } 421fb726d48Sopenharmony_ci } 422fb726d48Sopenharmony_ci }; 423fb726d48Sopenharmony_ci } 424fb726d48Sopenharmony_ci 425fb726d48Sopenharmony_ci setEvent(): void { 426fb726d48Sopenharmony_ci this.onmouseover = this.onfocus = (ev): void => { 427fb726d48Sopenharmony_ci if (this.focused === false && this.hasAttribute('adaptive-expansion')) { 428fb726d48Sopenharmony_ci // @ts-ignore 429fb726d48Sopenharmony_ci if (this.parentElement!.offsetTop < this.bodyEl!.clientHeight) { 430fb726d48Sopenharmony_ci // @ts-ignore 431fb726d48Sopenharmony_ci this.bodyEl!.classList.add('body-bottom'); 432fb726d48Sopenharmony_ci } else { 433fb726d48Sopenharmony_ci // @ts-ignore 434fb726d48Sopenharmony_ci this.bodyEl!.classList.remove('body-bottom'); 435fb726d48Sopenharmony_ci } 436fb726d48Sopenharmony_ci } 437fb726d48Sopenharmony_ci if (this.hasAttribute('allow-clear')) { 438fb726d48Sopenharmony_ci // @ts-ignore 439fb726d48Sopenharmony_ci if (this.selectInputEl.value.length > 0 || this.selectInputEl.placeholder !== this.defaultPlaceholder) { 440fb726d48Sopenharmony_ci // @ts-ignore 441fb726d48Sopenharmony_ci this.selectClearEl.style.display = 'flex'; // @ts-ignore 442fb726d48Sopenharmony_ci this.selectIconEl.style.display = 'none'; 443fb726d48Sopenharmony_ci } else { 444fb726d48Sopenharmony_ci // @ts-ignore 445fb726d48Sopenharmony_ci this.selectClearEl.style.display = 'none'; // @ts-ignore 446fb726d48Sopenharmony_ci this.selectIconEl.style.display = 'flex'; 447fb726d48Sopenharmony_ci } 448fb726d48Sopenharmony_ci } 449fb726d48Sopenharmony_ci }; 450fb726d48Sopenharmony_ci this.onmouseout = this.onblur = (ev): void => { 451fb726d48Sopenharmony_ci if (this.hasAttribute('allow-clear')) { 452fb726d48Sopenharmony_ci // @ts-ignore 453fb726d48Sopenharmony_ci this.selectClearEl.style.display = 'none'; // @ts-ignore 454fb726d48Sopenharmony_ci this.selectIconEl.style.display = 'flex'; 455fb726d48Sopenharmony_ci } 456fb726d48Sopenharmony_ci this.focused = false; 457fb726d48Sopenharmony_ci }; // @ts-ignore 458fb726d48Sopenharmony_ci this.selectInputEl.onfocus = (ev: unknown): void => { 459fb726d48Sopenharmony_ci if (this.hasAttribute('disabled')) { 460fb726d48Sopenharmony_ci return; 461fb726d48Sopenharmony_ci } 462fb726d48Sopenharmony_ci if (this.hasAttribute('show-search')) { 463fb726d48Sopenharmony_ci // @ts-ignore 464fb726d48Sopenharmony_ci this.selectSearchEl.style.display = 'flex'; // @ts-ignore 465fb726d48Sopenharmony_ci this.selectIconEl.style.display = 'none'; 466fb726d48Sopenharmony_ci } 467fb726d48Sopenharmony_ci this.querySelectorAll('lit-select-option').forEach((a) => { 468fb726d48Sopenharmony_ci // @ts-ignore 469fb726d48Sopenharmony_ci a.style.display = 'flex'; 470fb726d48Sopenharmony_ci }); 471fb726d48Sopenharmony_ci }; 472fb726d48Sopenharmony_ci } 473fb726d48Sopenharmony_ci 474fb726d48Sopenharmony_ci setOnkeydown(): void { 475fb726d48Sopenharmony_ci // @ts-ignore 476fb726d48Sopenharmony_ci this.selectInputEl.onkeydown = (ev: KeyboardEvent): void => { 477fb726d48Sopenharmony_ci ev.stopPropagation(); 478fb726d48Sopenharmony_ci if (this.hasAttribute('tabselect')) { 479fb726d48Sopenharmony_ci // @ts-ignore 480fb726d48Sopenharmony_ci this.selectInputEl.readOnly = true; 481fb726d48Sopenharmony_ci } else { 482fb726d48Sopenharmony_ci // @ts-ignore 483fb726d48Sopenharmony_ci if (ev.key === 'Backspace') { 484fb726d48Sopenharmony_ci if (this.isMultiple()) { 485fb726d48Sopenharmony_ci // @ts-ignore 486fb726d48Sopenharmony_ci let tag = this.selectMultipleRootEl.lastElementChild.previousElementSibling; 487fb726d48Sopenharmony_ci if (tag) { 488fb726d48Sopenharmony_ci this.querySelector(`lit-select-option[value=${tag.value}]`)?.removeAttribute('selected'); 489fb726d48Sopenharmony_ci tag.remove(); 490fb726d48Sopenharmony_ci if (this.shadowRoot!.querySelectorAll('.tag').length === 0) { 491fb726d48Sopenharmony_ci // @ts-ignore 492fb726d48Sopenharmony_ci this.selectInputEl.style.width = 'auto'; // @ts-ignore 493fb726d48Sopenharmony_ci this.selectInputEl.placeholder = this.defaultPlaceholder; 494fb726d48Sopenharmony_ci } 495fb726d48Sopenharmony_ci } 496fb726d48Sopenharmony_ci } else { 497fb726d48Sopenharmony_ci this.clear(); 498fb726d48Sopenharmony_ci this.dispatchEvent(new CustomEvent('onClear', { detail: ev })); //向外派发清理事件 499fb726d48Sopenharmony_ci } // @ts-ignore 500fb726d48Sopenharmony_ci } else if (ev.key === 'Enter') { 501fb726d48Sopenharmony_ci if (!this.canInsert) { 502fb726d48Sopenharmony_ci let filter = [...this.querySelectorAll('lit-select-option')].filter( 503fb726d48Sopenharmony_ci // @ts-ignore 504fb726d48Sopenharmony_ci (a: unknown) => a.style.display !== 'none' 505fb726d48Sopenharmony_ci ); 506fb726d48Sopenharmony_ci if (filter.length > 0) { 507fb726d48Sopenharmony_ci // @ts-ignore 508fb726d48Sopenharmony_ci this.selectInputEl.value = filter[0].textContent; // @ts-ignore 509fb726d48Sopenharmony_ci this.selectInputEl.placeholder = filter[0].textContent; 510fb726d48Sopenharmony_ci this.blur(); 511fb726d48Sopenharmony_ci // @ts-ignore 512fb726d48Sopenharmony_ci this.value = filter[0].getAttribute('value'); 513fb726d48Sopenharmony_ci this.dispatchEvent( 514fb726d48Sopenharmony_ci new CustomEvent('change', { 515fb726d48Sopenharmony_ci detail: { 516fb726d48Sopenharmony_ci selected: true, 517fb726d48Sopenharmony_ci value: filter[0].getAttribute('value'), 518fb726d48Sopenharmony_ci text: filter[0].textContent, 519fb726d48Sopenharmony_ci }, 520fb726d48Sopenharmony_ci }) 521fb726d48Sopenharmony_ci ); 522fb726d48Sopenharmony_ci } 523fb726d48Sopenharmony_ci }// @ts-ignore 524fb726d48Sopenharmony_ci } else if (ev.key === '0' && ev.target.value.length === 1 && ev.target.value === '0') { 525fb726d48Sopenharmony_ci // @ts-ignore 526fb726d48Sopenharmony_ci ev.preventDefault(); 527fb726d48Sopenharmony_ci } 528fb726d48Sopenharmony_ci } 529fb726d48Sopenharmony_ci }; 530fb726d48Sopenharmony_ci } 531fb726d48Sopenharmony_ci 532fb726d48Sopenharmony_ci initOptions(): void { 533fb726d48Sopenharmony_ci this.querySelectorAll('lit-select-option').forEach((a) => { 534fb726d48Sopenharmony_ci if (this.isMultiple()) { 535fb726d48Sopenharmony_ci a.setAttribute('check', ''); 536fb726d48Sopenharmony_ci if (a.getAttribute('value') === this.defaultValue) { 537fb726d48Sopenharmony_ci let tag = this.newTag(a.getAttribute('value'), a.textContent); // @ts-ignore 538fb726d48Sopenharmony_ci this.selectMultipleRootEl.insertBefore(tag, this.selectInputEl); // @ts-ignore 539fb726d48Sopenharmony_ci this.selectInputEl.placeholder = ''; // @ts-ignore 540fb726d48Sopenharmony_ci this.selectInputEl.value = ''; // @ts-ignore 541fb726d48Sopenharmony_ci this.selectInputEl.style.width = '1px'; 542fb726d48Sopenharmony_ci a.setAttribute('selected', ''); 543fb726d48Sopenharmony_ci } 544fb726d48Sopenharmony_ci } else { 545fb726d48Sopenharmony_ci if (a.hasAttribute('selected')) { 546fb726d48Sopenharmony_ci a.removeAttribute('selected'); 547fb726d48Sopenharmony_ci } 548fb726d48Sopenharmony_ci if (a.getAttribute('value') === this.defaultValue) { 549fb726d48Sopenharmony_ci // @ts-ignore 550fb726d48Sopenharmony_ci this.selectInputEl.value = a.textContent; 551fb726d48Sopenharmony_ci a.setAttribute('selected', ''); 552fb726d48Sopenharmony_ci } 553fb726d48Sopenharmony_ci } 554fb726d48Sopenharmony_ci a.addEventListener('mouseup', (e) => { 555fb726d48Sopenharmony_ci e.stopPropagation(); 556fb726d48Sopenharmony_ci }); 557fb726d48Sopenharmony_ci a.addEventListener('mousedown', (e) => { 558fb726d48Sopenharmony_ci e.stopPropagation(); 559fb726d48Sopenharmony_ci }); 560fb726d48Sopenharmony_ci this.onSelectedEvent(a); 561fb726d48Sopenharmony_ci }); 562fb726d48Sopenharmony_ci } 563fb726d48Sopenharmony_ci 564fb726d48Sopenharmony_ci onSelectedEvent(a: Element): void { 565fb726d48Sopenharmony_ci a.addEventListener('onSelected', (e: unknown) => { 566fb726d48Sopenharmony_ci if (this.isMultiple()) { 567fb726d48Sopenharmony_ci if (a.hasAttribute('selected')) { 568fb726d48Sopenharmony_ci // @ts-ignore 569fb726d48Sopenharmony_ci let tag = this.shadowRoot!.querySelector(`div[data-value=${e.detail.value}]`) as HTMLElement; 570fb726d48Sopenharmony_ci if (tag) { 571fb726d48Sopenharmony_ci tag.parentElement!.removeChild(tag); 572fb726d48Sopenharmony_ci } // @ts-ignore 573fb726d48Sopenharmony_ci e.detail.selected = false; 574fb726d48Sopenharmony_ci } else { 575fb726d48Sopenharmony_ci // @ts-ignore 576fb726d48Sopenharmony_ci let tag = this.newTag(e.detail.value, e.detail.text); // @ts-ignore 577fb726d48Sopenharmony_ci this.selectMultipleRootEl.insertBefore(tag, this.selectInputEl); // @ts-ignore 578fb726d48Sopenharmony_ci this.selectInputEl.placeholder = ''; // @ts-ignore 579fb726d48Sopenharmony_ci this.selectInputEl.value = ''; // @ts-ignore 580fb726d48Sopenharmony_ci this.selectInputEl.style.width = '1px'; 581fb726d48Sopenharmony_ci } 582fb726d48Sopenharmony_ci if (this.shadowRoot!.querySelectorAll('.tag').length === 0) { 583fb726d48Sopenharmony_ci // @ts-ignore 584fb726d48Sopenharmony_ci this.selectInputEl.style.width = 'auto'; // @ts-ignore 585fb726d48Sopenharmony_ci this.selectInputEl.placeholder = this.defaultPlaceholder; 586fb726d48Sopenharmony_ci } // @ts-ignore 587fb726d48Sopenharmony_ci this.selectInputEl.focus(); 588fb726d48Sopenharmony_ci } else { 589fb726d48Sopenharmony_ci [...this.querySelectorAll('lit-select-option')].forEach((item) => { 590fb726d48Sopenharmony_ci if (item.hasAttribute('selected')) { 591fb726d48Sopenharmony_ci this.currentSelectedValue = item.getAttribute('value') || ''; 592fb726d48Sopenharmony_ci } 593fb726d48Sopenharmony_ci item.removeAttribute('selected'); 594fb726d48Sopenharmony_ci }); 595fb726d48Sopenharmony_ci this.blur(); // @ts-ignore 596fb726d48Sopenharmony_ci this.bodyEl!.style.display = 'none'; 597fb726d48Sopenharmony_ci // @ts-ignore 598fb726d48Sopenharmony_ci this.selectInputEl.value = e.detail.text; 599fb726d48Sopenharmony_ci } 600fb726d48Sopenharmony_ci if (a.getAttribute('value') === this.currentSelectedValue) { 601fb726d48Sopenharmony_ci a.removeAttribute('selected'); 602fb726d48Sopenharmony_ci this.currentSelectedValue = ''; 603fb726d48Sopenharmony_ci // @ts-ignore 604fb726d48Sopenharmony_ci this.selectInputEl.value = ''; 605fb726d48Sopenharmony_ci // @ts-ignore 606fb726d48Sopenharmony_ci this.selectInputEl.placeholder = this.defaultPlaceholder; 607fb726d48Sopenharmony_ci } else { 608fb726d48Sopenharmony_ci this.currentSelectedValue = a.getAttribute('value') || ''; 609fb726d48Sopenharmony_ci a.setAttribute('selected', ''); 610fb726d48Sopenharmony_ci } 611fb726d48Sopenharmony_ci this.value = this.currentSelectedValue; 612fb726d48Sopenharmony_ci // @ts-ignore 613fb726d48Sopenharmony_ci this.dispatchEvent(new CustomEvent('change', { detail: { selectValue: this.currentSelectedValue, text: e.detail.text } })); //向外层派发change事件,返回当前选中项 614fb726d48Sopenharmony_ci }); 615fb726d48Sopenharmony_ci } 616fb726d48Sopenharmony_ci 617fb726d48Sopenharmony_ci clear(): void { 618fb726d48Sopenharmony_ci // @ts-ignore 619fb726d48Sopenharmony_ci this.selectInputEl.value = ''; // @ts-ignore 620fb726d48Sopenharmony_ci this.selectInputEl.placeholder = this.defaultPlaceholder; 621fb726d48Sopenharmony_ci } 622fb726d48Sopenharmony_ci 623fb726d48Sopenharmony_ci reset(): void { 624fb726d48Sopenharmony_ci this.querySelectorAll('lit-select-option').forEach((a) => { 625fb726d48Sopenharmony_ci [...this.querySelectorAll('lit-select-option')].forEach((a) => a.removeAttribute('selected')); 626fb726d48Sopenharmony_ci if (a.getAttribute('value') === this.defaultValue) { 627fb726d48Sopenharmony_ci // @ts-ignore 628fb726d48Sopenharmony_ci this.selectInputEl.value = a.textContent; 629fb726d48Sopenharmony_ci a.setAttribute('selected', ''); 630fb726d48Sopenharmony_ci } 631fb726d48Sopenharmony_ci }); 632fb726d48Sopenharmony_ci } 633fb726d48Sopenharmony_ci 634fb726d48Sopenharmony_ci disconnectedCallback(): void { } 635fb726d48Sopenharmony_ci 636fb726d48Sopenharmony_ci adoptedCallback(): void { } 637fb726d48Sopenharmony_ci 638fb726d48Sopenharmony_ci attributeChangedCallback(name: unknown, oldValue: unknown, newValue: unknown): void { 639fb726d48Sopenharmony_ci if (name === 'value' && this.selectInputEl) { 640fb726d48Sopenharmony_ci if (newValue) { 641fb726d48Sopenharmony_ci [...this.querySelectorAll('lit-select-option')].forEach((a) => { 642fb726d48Sopenharmony_ci if (a.getAttribute('value') === newValue) { 643fb726d48Sopenharmony_ci this.currentSelectedValue = a.getAttribute('value') || ''; 644fb726d48Sopenharmony_ci a.setAttribute('selected', ''); // @ts-ignore 645fb726d48Sopenharmony_ci this.selectInputEl.value = a.textContent; 646fb726d48Sopenharmony_ci } else { 647fb726d48Sopenharmony_ci a.removeAttribute('selected'); 648fb726d48Sopenharmony_ci } 649fb726d48Sopenharmony_ci }); 650fb726d48Sopenharmony_ci } else { 651fb726d48Sopenharmony_ci this.clear(); 652fb726d48Sopenharmony_ci } 653fb726d48Sopenharmony_ci } 654fb726d48Sopenharmony_ci } 655fb726d48Sopenharmony_ci} 656