@duskmoon-dev/el-select 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1120 @@
1
+ // src/el-dm-select.ts
2
+ import { BaseElement, css } from "@duskmoon-dev/el-core";
3
+ var chevronDownIcon = `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>`;
4
+ var chevronRightIcon = `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m9 18 6-6-6-6"/></svg>`;
5
+ var checkIcon = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></svg>`;
6
+ var closeIcon = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>`;
7
+ var searchIcon = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>`;
8
+ var styles = css`
9
+ :host {
10
+ display: inline-block;
11
+ width: 100%;
12
+ }
13
+
14
+ .select {
15
+ position: relative;
16
+ width: 100%;
17
+ }
18
+
19
+ /* Trigger Button */
20
+ .select-trigger {
21
+ display: flex;
22
+ align-items: center;
23
+ gap: 0.5rem;
24
+ width: 100%;
25
+ min-height: 2.75rem;
26
+ padding: 0.5rem 0.75rem;
27
+ font-size: var(--font-size-md, 1rem);
28
+ line-height: 1.5;
29
+ color: var(--color-on-surface);
30
+ background-color: var(--color-surface);
31
+ border: 1px solid var(--color-outline);
32
+ border-radius: var(--radius-md, 0.5rem);
33
+ cursor: pointer;
34
+ transition: border-color 150ms ease, box-shadow 150ms ease;
35
+ }
36
+
37
+ .select-trigger:hover:not(:disabled) {
38
+ border-color: var(--color-on-surface-variant);
39
+ }
40
+
41
+ .select-trigger:focus {
42
+ outline: none;
43
+ border-color: var(--color-primary);
44
+ box-shadow: 0 0 0 3px color-mix(in oklch, var(--color-primary) 15%, transparent);
45
+ }
46
+
47
+ .select-trigger:disabled {
48
+ cursor: not-allowed;
49
+ opacity: 0.5;
50
+ background-color: var(--color-surface-container);
51
+ }
52
+
53
+ /* Value Display */
54
+ .select-value {
55
+ flex: 1;
56
+ overflow: hidden;
57
+ text-overflow: ellipsis;
58
+ white-space: nowrap;
59
+ text-align: left;
60
+ }
61
+
62
+ .select-placeholder {
63
+ color: var(--color-on-surface-variant);
64
+ opacity: 0.7;
65
+ }
66
+
67
+ /* Tags Container */
68
+ .select-tags {
69
+ display: flex;
70
+ flex-wrap: wrap;
71
+ gap: 0.25rem;
72
+ flex: 1;
73
+ min-width: 0;
74
+ }
75
+
76
+ .select-tag {
77
+ display: inline-flex;
78
+ align-items: center;
79
+ gap: 0.25rem;
80
+ max-width: 100%;
81
+ padding: 0.125rem 0.25rem 0.125rem 0.5rem;
82
+ font-size: var(--font-size-sm, 0.875rem);
83
+ line-height: 1.25rem;
84
+ background-color: var(--color-surface-container-high, #e8e8e8);
85
+ color: var(--color-on-surface);
86
+ border-radius: 1rem;
87
+ }
88
+
89
+ .select-tag-text {
90
+ overflow: hidden;
91
+ text-overflow: ellipsis;
92
+ white-space: nowrap;
93
+ }
94
+
95
+ .select-tag-remove {
96
+ display: inline-flex;
97
+ align-items: center;
98
+ justify-content: center;
99
+ width: 16px;
100
+ height: 16px;
101
+ padding: 0;
102
+ color: inherit;
103
+ background-color: transparent;
104
+ border-radius: 50%;
105
+ cursor: pointer;
106
+ opacity: 0.7;
107
+ transition: opacity 150ms ease, background-color 150ms ease;
108
+ }
109
+
110
+ .select-tag-remove svg {
111
+ width: 10px;
112
+ height: 10px;
113
+ display: block;
114
+ }
115
+
116
+ .select-tag-remove:hover {
117
+ opacity: 1;
118
+ background-color: color-mix(in oklch, currentColor 15%, transparent);
119
+ }
120
+
121
+ .select-tag-overflow {
122
+ padding: 0.125rem 0.5rem;
123
+ background-color: var(--color-surface-container);
124
+ color: var(--color-on-surface-variant);
125
+ }
126
+
127
+ /* Icons */
128
+ .select-arrow {
129
+ display: inline-flex;
130
+ align-items: center;
131
+ justify-content: center;
132
+ width: 20px;
133
+ height: 20px;
134
+ flex-shrink: 0;
135
+ color: var(--color-on-surface-variant);
136
+ transition: transform 150ms ease;
137
+ }
138
+
139
+ .select-arrow svg {
140
+ width: 16px;
141
+ height: 16px;
142
+ display: block;
143
+ }
144
+
145
+ .select.open .select-arrow {
146
+ transform: rotate(180deg);
147
+ }
148
+
149
+ .select-clear {
150
+ display: inline-flex;
151
+ align-items: center;
152
+ justify-content: center;
153
+ width: 20px;
154
+ height: 20px;
155
+ padding: 0;
156
+ color: var(--color-on-surface-variant);
157
+ background-color: transparent;
158
+ border-radius: 50%;
159
+ cursor: pointer;
160
+ flex-shrink: 0;
161
+ transition: background-color 150ms ease;
162
+ }
163
+
164
+ .select-clear svg {
165
+ width: 14px;
166
+ height: 14px;
167
+ display: block;
168
+ }
169
+
170
+ .select-clear:hover {
171
+ background-color: var(--color-surface-container-high);
172
+ }
173
+
174
+ /* Dropdown - uses Popover API (top-layer requires position: fixed) */
175
+ .select-dropdown {
176
+ position: fixed;
177
+ margin: 0;
178
+ padding: 0;
179
+ border: 1px solid var(--color-outline-variant);
180
+ border-radius: var(--radius-md, 0.5rem);
181
+ background-color: var(--color-surface);
182
+ box-shadow: var(--shadow-md, 0 4px 6px -1px rgba(0, 0, 0, 0.1));
183
+ max-height: 18rem;
184
+ overflow: hidden;
185
+ display: none;
186
+ flex-direction: column;
187
+ z-index: 1000;
188
+ }
189
+
190
+ .select-dropdown:popover-open {
191
+ display: flex;
192
+ }
193
+
194
+ /* Search */
195
+ .select-search {
196
+ display: flex;
197
+ align-items: center;
198
+ gap: 0.5rem;
199
+ padding: 0.5rem;
200
+ border-bottom: 1px solid var(--color-outline-variant);
201
+ }
202
+
203
+ .select-search-icon {
204
+ display: inline-flex;
205
+ align-items: center;
206
+ justify-content: center;
207
+ width: 16px;
208
+ height: 16px;
209
+ color: var(--color-on-surface-variant);
210
+ flex-shrink: 0;
211
+ }
212
+
213
+ .select-search-icon svg {
214
+ width: 14px;
215
+ height: 14px;
216
+ display: block;
217
+ }
218
+
219
+ .select-search-input {
220
+ flex: 1;
221
+ padding: 0.375rem 0.5rem;
222
+ font-size: var(--font-size-sm, 0.875rem);
223
+ color: var(--color-on-surface);
224
+ background-color: var(--color-surface-container);
225
+ border: none;
226
+ border-radius: var(--radius-sm, 0.25rem);
227
+ outline: none;
228
+ }
229
+
230
+ .select-search-input:focus {
231
+ background-color: var(--color-surface-container-high);
232
+ }
233
+
234
+ .select-search-input::placeholder {
235
+ color: var(--color-on-surface-variant);
236
+ opacity: 0.7;
237
+ }
238
+
239
+ /* Options List */
240
+ .select-options {
241
+ flex: 1;
242
+ overflow-y: auto;
243
+ padding: 0.25rem;
244
+ }
245
+
246
+ /* Option */
247
+ .select-option {
248
+ display: flex;
249
+ align-items: center;
250
+ gap: 0.5rem;
251
+ width: 100%;
252
+ padding: 0.5rem 0.75rem;
253
+ font-size: var(--font-size-sm, 0.875rem);
254
+ color: var(--color-on-surface);
255
+ background-color: transparent;
256
+ border: none;
257
+ border-radius: var(--radius-sm, 0.25rem);
258
+ cursor: pointer;
259
+ text-align: left;
260
+ transition: background-color 150ms ease;
261
+ }
262
+
263
+ .select-option:hover:not(.disabled) {
264
+ background-color: var(--color-surface-container);
265
+ }
266
+
267
+ .select-option.selected {
268
+ background-color: var(--color-primary-container, #e8def8);
269
+ color: var(--color-on-primary-container, #1d1b20);
270
+ }
271
+
272
+ .select-option.selected:hover {
273
+ background-color: color-mix(in oklch, var(--color-primary-container, #e8def8), black 5%);
274
+ }
275
+
276
+ .select-option.highlighted {
277
+ background-color: var(--color-surface-container-high);
278
+ }
279
+
280
+ .select-option.disabled {
281
+ opacity: 0.5;
282
+ cursor: not-allowed;
283
+ }
284
+
285
+ .select-option-checkbox {
286
+ display: flex;
287
+ align-items: center;
288
+ justify-content: center;
289
+ width: 1rem;
290
+ height: 1rem;
291
+ background-color: transparent;
292
+ border: 2px solid var(--color-on-surface-variant);
293
+ border-radius: 0.125rem;
294
+ flex-shrink: 0;
295
+ }
296
+
297
+ .select-option.selected .select-option-checkbox {
298
+ background-color: var(--color-primary);
299
+ border-color: var(--color-primary);
300
+ color: var(--color-on-primary, white);
301
+ }
302
+
303
+ .select-option-label {
304
+ flex: 1;
305
+ overflow: hidden;
306
+ text-overflow: ellipsis;
307
+ white-space: nowrap;
308
+ }
309
+
310
+ .select-option-check {
311
+ display: none;
312
+ color: var(--color-primary);
313
+ flex-shrink: 0;
314
+ }
315
+
316
+ .select-option.selected .select-option-check {
317
+ display: flex;
318
+ }
319
+
320
+ /* Group Header */
321
+ .select-group-header {
322
+ padding: 0.5rem 0.75rem 0.25rem;
323
+ font-size: var(--font-size-xs, 0.75rem);
324
+ font-weight: 600;
325
+ color: var(--color-on-surface-variant);
326
+ text-transform: uppercase;
327
+ letter-spacing: 0.05em;
328
+ }
329
+
330
+ /* Tree Node */
331
+ .select-tree-node {
332
+ display: flex;
333
+ align-items: center;
334
+ gap: 0.25rem;
335
+ width: 100%;
336
+ padding: 0.5rem 0.5rem;
337
+ font-size: var(--font-size-sm, 0.875rem);
338
+ color: var(--color-on-surface);
339
+ background-color: transparent;
340
+ border: none;
341
+ border-radius: var(--radius-sm, 0.25rem);
342
+ cursor: pointer;
343
+ text-align: left;
344
+ transition: background-color 150ms ease;
345
+ }
346
+
347
+ .select-tree-node:hover:not(.disabled) {
348
+ background-color: var(--color-surface-container);
349
+ }
350
+
351
+ .select-tree-node.selected {
352
+ background-color: var(--color-primary-container, #e8def8);
353
+ color: var(--color-on-primary-container, #1d1b20);
354
+ }
355
+
356
+ .select-tree-node.highlighted {
357
+ background-color: var(--color-surface-container-high);
358
+ }
359
+
360
+ .select-tree-node.disabled {
361
+ opacity: 0.5;
362
+ cursor: not-allowed;
363
+ }
364
+
365
+ .select-tree-toggle {
366
+ display: flex;
367
+ align-items: center;
368
+ justify-content: center;
369
+ width: 1.25rem;
370
+ height: 1.25rem;
371
+ padding: 0;
372
+ color: var(--color-on-surface-variant);
373
+ background-color: transparent;
374
+ border: none;
375
+ border-radius: var(--radius-sm, 0.25rem);
376
+ cursor: pointer;
377
+ flex-shrink: 0;
378
+ transition: transform 150ms ease;
379
+ }
380
+
381
+ .select-tree-toggle:hover {
382
+ background-color: var(--color-surface-container-high);
383
+ }
384
+
385
+ .select-tree-toggle.expanded {
386
+ transform: rotate(90deg);
387
+ }
388
+
389
+ .select-tree-toggle.hidden {
390
+ visibility: hidden;
391
+ }
392
+
393
+ .select-tree-indent {
394
+ flex-shrink: 0;
395
+ }
396
+
397
+ /* Empty State */
398
+ .select-empty {
399
+ padding: 1.5rem;
400
+ text-align: center;
401
+ color: var(--color-on-surface-variant);
402
+ font-size: var(--font-size-sm, 0.875rem);
403
+ }
404
+
405
+ /* Size Variants */
406
+ :host([size="sm"]) .select-trigger {
407
+ min-height: 2.25rem;
408
+ padding: 0.375rem 0.5rem;
409
+ font-size: var(--font-size-sm, 0.875rem);
410
+ border-radius: var(--radius-sm, 0.375rem);
411
+ }
412
+
413
+ :host([size="lg"]) .select-trigger {
414
+ min-height: 3.25rem;
415
+ padding: 0.625rem 1rem;
416
+ font-size: var(--font-size-lg, 1.125rem);
417
+ border-radius: var(--radius-lg, 0.625rem);
418
+ }
419
+
420
+ /* Validation States */
421
+ :host([validation-state="invalid"]) .select-trigger {
422
+ border-color: var(--color-error);
423
+ }
424
+
425
+ :host([validation-state="invalid"]) .select-trigger:focus {
426
+ border-color: var(--color-error);
427
+ box-shadow: 0 0 0 3px color-mix(in oklch, var(--color-error) 15%, transparent);
428
+ }
429
+
430
+ :host([validation-state="valid"]) .select-trigger {
431
+ border-color: var(--color-success);
432
+ }
433
+
434
+ /* Disabled State */
435
+ :host([disabled]) {
436
+ pointer-events: none;
437
+ }
438
+
439
+ :host([disabled]) .select-trigger {
440
+ cursor: not-allowed;
441
+ opacity: 0.5;
442
+ background-color: var(--color-surface-container);
443
+ }
444
+ `;
445
+
446
+ class ElDmSelect extends BaseElement {
447
+ static properties = {
448
+ value: { type: String, reflect: true, default: "" },
449
+ placeholder: { type: String, default: "Select..." },
450
+ disabled: { type: Boolean, reflect: true, default: false },
451
+ readonly: { type: Boolean, reflect: true, default: false },
452
+ multiple: { type: Boolean, reflect: true, default: false },
453
+ treeData: { type: Boolean, reflect: true, attribute: "tree-data", default: false },
454
+ searchable: { type: Boolean, reflect: true, default: false },
455
+ clearable: { type: Boolean, reflect: true, default: false },
456
+ cascade: { type: Boolean, reflect: true, default: false },
457
+ checkStrictly: { type: Boolean, reflect: true, attribute: "check-strictly", default: false },
458
+ defaultExpandAll: { type: Boolean, reflect: true, attribute: "default-expand-all", default: false },
459
+ maxTagCount: { type: Number, attribute: "max-tag-count", default: -1 },
460
+ size: { type: String, reflect: true, default: "md" },
461
+ validationState: { type: String, reflect: true, attribute: "validation-state" },
462
+ options: { type: String, default: "" },
463
+ treeOptions: { type: String, attribute: "tree-options", default: "" }
464
+ };
465
+ _isOpen = false;
466
+ _searchValue = "";
467
+ _highlightedIndex = -1;
468
+ _expandedKeys = new Set;
469
+ _options = [];
470
+ _treeOptions = [];
471
+ _handleOutsideClick = this._onOutsideClick.bind(this);
472
+ _handleKeyDown = this._onKeyDown.bind(this);
473
+ _handleScroll = this._onScroll.bind(this);
474
+ _handleResize = this._onResize.bind(this);
475
+ constructor() {
476
+ super();
477
+ this.attachStyles(styles);
478
+ }
479
+ connectedCallback() {
480
+ super.connectedCallback();
481
+ document.addEventListener("click", this._handleOutsideClick);
482
+ document.addEventListener("keydown", this._handleKeyDown);
483
+ this._parseOptionsFromAttributes();
484
+ if (this.defaultExpandAll && this.treeData) {
485
+ this._expandAllNodes();
486
+ }
487
+ this._setupEventDelegation();
488
+ }
489
+ _parseOptionsFromAttributes() {
490
+ if (this.options) {
491
+ try {
492
+ const parsed = JSON.parse(this.options);
493
+ if (Array.isArray(parsed)) {
494
+ this._options = parsed;
495
+ }
496
+ } catch {}
497
+ }
498
+ if (this.treeOptions) {
499
+ try {
500
+ const parsed = JSON.parse(this.treeOptions);
501
+ if (Array.isArray(parsed)) {
502
+ this._treeOptions = parsed;
503
+ }
504
+ } catch {}
505
+ }
506
+ }
507
+ disconnectedCallback() {
508
+ super.disconnectedCallback();
509
+ document.removeEventListener("click", this._handleOutsideClick);
510
+ document.removeEventListener("keydown", this._handleKeyDown);
511
+ this._removeScrollListeners();
512
+ }
513
+ _addScrollListeners() {
514
+ window.addEventListener("scroll", this._handleScroll, true);
515
+ window.addEventListener("resize", this._handleResize);
516
+ }
517
+ _removeScrollListeners() {
518
+ window.removeEventListener("scroll", this._handleScroll, true);
519
+ window.removeEventListener("resize", this._handleResize);
520
+ }
521
+ _onScroll() {
522
+ if (this._isOpen) {
523
+ const dropdown = this.shadowRoot?.querySelector(".select-dropdown");
524
+ const trigger = this.shadowRoot?.querySelector(".select-trigger");
525
+ if (dropdown && trigger) {
526
+ this._positionDropdown(dropdown, trigger);
527
+ }
528
+ }
529
+ }
530
+ _onResize() {
531
+ if (this._isOpen) {
532
+ this._close();
533
+ }
534
+ }
535
+ setOptions(options) {
536
+ this._options = options;
537
+ this.update();
538
+ }
539
+ setTreeOptions(options) {
540
+ this._treeOptions = options;
541
+ if (this.defaultExpandAll) {
542
+ this._expandAllNodes();
543
+ }
544
+ this.update();
545
+ }
546
+ _getSelectedValues() {
547
+ if (!this.value)
548
+ return [];
549
+ if (this.multiple || this.treeData) {
550
+ try {
551
+ const parsed = JSON.parse(this.value);
552
+ return Array.isArray(parsed) ? parsed : [this.value];
553
+ } catch {
554
+ return this.value ? [this.value] : [];
555
+ }
556
+ }
557
+ return this.value ? [this.value] : [];
558
+ }
559
+ _getFilteredOptions() {
560
+ if (!this._searchValue)
561
+ return this._options;
562
+ const search = this._searchValue.toLowerCase();
563
+ return this._options.filter((opt) => opt.label.toLowerCase().includes(search) || opt.value.toLowerCase().includes(search));
564
+ }
565
+ _getFlattenedTreeOptions() {
566
+ const result = [];
567
+ const search = this._searchValue.toLowerCase();
568
+ const flatten = (options, level) => {
569
+ for (const option of options) {
570
+ const hasChildren = !!(option.children && option.children.length > 0);
571
+ const isExpanded = this._expandedKeys.has(option.value);
572
+ if (search) {
573
+ const matches = option.label.toLowerCase().includes(search);
574
+ const hasMatchingChild = hasChildren && this._hasMatchingDescendant(option, search);
575
+ if (!matches && !hasMatchingChild)
576
+ continue;
577
+ }
578
+ result.push({ option, level, hasChildren, isExpanded });
579
+ if (hasChildren && isExpanded) {
580
+ flatten(option.children, level + 1);
581
+ }
582
+ }
583
+ };
584
+ flatten(this._treeOptions, 0);
585
+ return result;
586
+ }
587
+ _hasMatchingDescendant(option, search) {
588
+ if (!option.children)
589
+ return false;
590
+ for (const child of option.children) {
591
+ if (child.label.toLowerCase().includes(search))
592
+ return true;
593
+ if (this._hasMatchingDescendant(child, search))
594
+ return true;
595
+ }
596
+ return false;
597
+ }
598
+ _expandAllNodes() {
599
+ const expandAll = (options) => {
600
+ for (const option of options) {
601
+ if (option.children && option.children.length > 0) {
602
+ this._expandedKeys.add(option.value);
603
+ expandAll(option.children);
604
+ }
605
+ }
606
+ };
607
+ expandAll(this._treeOptions);
608
+ }
609
+ _getDescendantValues(option) {
610
+ const values = [];
611
+ const collect = (opt) => {
612
+ values.push(opt.value);
613
+ if (opt.children) {
614
+ opt.children.forEach(collect);
615
+ }
616
+ };
617
+ collect(option);
618
+ return values;
619
+ }
620
+ _findOption(value) {
621
+ return this._options.find((opt) => opt.value === value);
622
+ }
623
+ _findTreeOption(value, options = this._treeOptions) {
624
+ for (const opt of options) {
625
+ if (opt.value === value)
626
+ return opt;
627
+ if (opt.children) {
628
+ const found = this._findTreeOption(value, opt.children);
629
+ if (found)
630
+ return found;
631
+ }
632
+ }
633
+ return;
634
+ }
635
+ _onOutsideClick(e) {
636
+ if (!this.contains(e.target)) {
637
+ this._close();
638
+ }
639
+ }
640
+ _onKeyDown(e) {
641
+ if (!this._isOpen) {
642
+ if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
643
+ if (document.activeElement === this || this.contains(document.activeElement)) {
644
+ e.preventDefault();
645
+ this._open();
646
+ }
647
+ }
648
+ return;
649
+ }
650
+ const options = this.treeData ? this._getFlattenedTreeOptions() : this._getFilteredOptions();
651
+ const enabledOptions = this.treeData ? options.filter((n) => !n.option.disabled) : options.filter((o) => !o.disabled);
652
+ switch (e.key) {
653
+ case "ArrowDown":
654
+ e.preventDefault();
655
+ this._highlightedIndex = Math.min(this._highlightedIndex + 1, enabledOptions.length - 1);
656
+ this.update();
657
+ break;
658
+ case "ArrowUp":
659
+ e.preventDefault();
660
+ this._highlightedIndex = Math.max(this._highlightedIndex - 1, 0);
661
+ this.update();
662
+ break;
663
+ case "Enter":
664
+ e.preventDefault();
665
+ if (this._highlightedIndex >= 0 && this._highlightedIndex < enabledOptions.length) {
666
+ const option = this.treeData ? enabledOptions[this._highlightedIndex].option : enabledOptions[this._highlightedIndex];
667
+ this._selectOption(option.value);
668
+ }
669
+ break;
670
+ case "Escape":
671
+ e.preventDefault();
672
+ this._close();
673
+ break;
674
+ case "Backspace":
675
+ if (this.multiple && !this._searchValue) {
676
+ e.preventDefault();
677
+ const selected = this._getSelectedValues();
678
+ if (selected.length > 0) {
679
+ this._removeTag(selected[selected.length - 1]);
680
+ }
681
+ }
682
+ break;
683
+ }
684
+ }
685
+ _open() {
686
+ if (this.disabled || this.readonly)
687
+ return;
688
+ this._isOpen = true;
689
+ this._highlightedIndex = -1;
690
+ this.update();
691
+ this._addScrollListeners();
692
+ requestAnimationFrame(() => {
693
+ const dropdown = this.shadowRoot?.querySelector(".select-dropdown");
694
+ const trigger = this.shadowRoot?.querySelector(".select-trigger");
695
+ if (dropdown && trigger) {
696
+ const triggerRect = trigger.getBoundingClientRect();
697
+ dropdown.style.top = `${triggerRect.bottom + 4}px`;
698
+ dropdown.style.left = `${triggerRect.left}px`;
699
+ dropdown.style.width = `${triggerRect.width}px`;
700
+ dropdown.showPopover();
701
+ requestAnimationFrame(() => {
702
+ this._positionDropdown(dropdown, trigger);
703
+ });
704
+ }
705
+ const searchInput = this.shadowRoot?.querySelector(".select-search-input");
706
+ if (searchInput) {
707
+ searchInput.focus();
708
+ }
709
+ });
710
+ }
711
+ _close() {
712
+ this._isOpen = false;
713
+ this._searchValue = "";
714
+ this._highlightedIndex = -1;
715
+ this._removeScrollListeners();
716
+ const dropdown = this.shadowRoot?.querySelector(".select-dropdown");
717
+ if (dropdown) {
718
+ try {
719
+ dropdown.hidePopover();
720
+ } catch {}
721
+ }
722
+ this.update();
723
+ }
724
+ _positionDropdown(dropdown, trigger) {
725
+ const triggerRect = trigger.getBoundingClientRect();
726
+ const dropdownRect = dropdown.getBoundingClientRect();
727
+ const viewportHeight = window.innerHeight;
728
+ let top = triggerRect.bottom + 4;
729
+ let left = triggerRect.left;
730
+ if (top + dropdownRect.height > viewportHeight && triggerRect.top > dropdownRect.height) {
731
+ top = triggerRect.top - dropdownRect.height - 4;
732
+ }
733
+ const viewportWidth = window.innerWidth;
734
+ if (left + triggerRect.width > viewportWidth) {
735
+ left = viewportWidth - triggerRect.width - 8;
736
+ }
737
+ dropdown.style.top = `${top}px`;
738
+ dropdown.style.left = `${left}px`;
739
+ dropdown.style.width = `${triggerRect.width}px`;
740
+ }
741
+ _toggle() {
742
+ if (this._isOpen) {
743
+ this._close();
744
+ } else {
745
+ this._open();
746
+ }
747
+ }
748
+ _selectOption(value) {
749
+ const selectedValues = this._getSelectedValues();
750
+ if (this.multiple) {
751
+ const index = selectedValues.indexOf(value);
752
+ if (index >= 0) {
753
+ selectedValues.splice(index, 1);
754
+ } else {
755
+ if (this.treeData && this.cascade && !this.checkStrictly) {
756
+ const option = this._findTreeOption(value);
757
+ if (option) {
758
+ const descendants = this._getDescendantValues(option);
759
+ for (const v of descendants) {
760
+ if (!selectedValues.includes(v)) {
761
+ selectedValues.push(v);
762
+ }
763
+ }
764
+ }
765
+ } else {
766
+ selectedValues.push(value);
767
+ }
768
+ }
769
+ this.value = JSON.stringify(selectedValues);
770
+ } else {
771
+ this.value = value;
772
+ this._close();
773
+ }
774
+ this._emitChange();
775
+ this.update();
776
+ }
777
+ _removeTag(value) {
778
+ const selectedValues = this._getSelectedValues();
779
+ const index = selectedValues.indexOf(value);
780
+ if (index >= 0) {
781
+ selectedValues.splice(index, 1);
782
+ this.value = selectedValues.length > 0 ? JSON.stringify(selectedValues) : "";
783
+ this._emitChange();
784
+ this.update();
785
+ }
786
+ }
787
+ _toggleExpand(value, e) {
788
+ e.stopPropagation();
789
+ const option = this._findTreeOption(value);
790
+ if (!option)
791
+ return;
792
+ const wasExpanded = this._expandedKeys.has(value);
793
+ if (wasExpanded) {
794
+ this._expandedKeys.delete(value);
795
+ } else {
796
+ this._expandedKeys.add(value);
797
+ }
798
+ this.emit("expand", {
799
+ node: option,
800
+ expanded: !wasExpanded
801
+ });
802
+ this.update();
803
+ }
804
+ _handleSearch(e) {
805
+ const input = e.target;
806
+ this._searchValue = input.value;
807
+ this._highlightedIndex = -1;
808
+ this.emit("search", { searchValue: this._searchValue });
809
+ this.update();
810
+ }
811
+ _handleClear(e) {
812
+ e.stopPropagation();
813
+ const previousValue = this.value;
814
+ this.value = "";
815
+ this.emit("clear", { previousValue });
816
+ this._emitChange();
817
+ this.update();
818
+ }
819
+ _emitChange() {
820
+ const selectedValues = this._getSelectedValues();
821
+ let selectedOptions;
822
+ if (this.treeData) {
823
+ selectedOptions = selectedValues.map((v) => this._findTreeOption(v)).filter((o) => o !== undefined);
824
+ } else {
825
+ selectedOptions = selectedValues.map((v) => this._findOption(v)).filter((o) => o !== undefined);
826
+ }
827
+ this.emit("change", {
828
+ value: this.value,
829
+ selectedOptions
830
+ });
831
+ }
832
+ render() {
833
+ return `
834
+ <div class="select ${this._isOpen ? "open" : ""}">
835
+ ${this._renderTrigger()}
836
+ ${this._renderDropdown()}
837
+ </div>
838
+ `;
839
+ }
840
+ _renderTrigger() {
841
+ const selectedValues = this._getSelectedValues();
842
+ const hasValue = selectedValues.length > 0;
843
+ const showClear = this.clearable && hasValue && !this.disabled && !this.readonly;
844
+ return `
845
+ <button
846
+ type="button"
847
+ class="select-trigger"
848
+ aria-haspopup="listbox"
849
+ aria-expanded="${this._isOpen}"
850
+ ${this.disabled ? "disabled" : ""}
851
+ data-action="toggle"
852
+ >
853
+ ${this.multiple && hasValue ? this._renderTags() : this._renderValue()}
854
+ ${showClear ? `<span class="select-clear" role="button" tabindex="-1" data-action="clear">${closeIcon}</span>` : ""}
855
+ <span class="select-arrow">${chevronDownIcon}</span>
856
+ </button>
857
+ `;
858
+ }
859
+ _renderValue() {
860
+ const selectedValues = this._getSelectedValues();
861
+ if (selectedValues.length === 0) {
862
+ return `<span class="select-value select-placeholder">${this.placeholder}</span>`;
863
+ }
864
+ const value = selectedValues[0];
865
+ let label = value;
866
+ if (this.treeData) {
867
+ const option = this._findTreeOption(value);
868
+ if (option)
869
+ label = option.label;
870
+ } else {
871
+ const option = this._findOption(value);
872
+ if (option)
873
+ label = option.label;
874
+ }
875
+ return `<span class="select-value">${this._escapeHtml(label)}</span>`;
876
+ }
877
+ _renderTags() {
878
+ const selectedValues = this._getSelectedValues();
879
+ const maxCount = this.maxTagCount;
880
+ const showAll = maxCount < 0;
881
+ const visibleValues = showAll ? selectedValues : selectedValues.slice(0, maxCount);
882
+ const overflowCount = showAll ? 0 : Math.max(0, selectedValues.length - maxCount);
883
+ let tagsHtml = visibleValues.map((value) => {
884
+ let label = value;
885
+ if (this.treeData) {
886
+ const option = this._findTreeOption(value);
887
+ if (option)
888
+ label = option.label;
889
+ } else {
890
+ const option = this._findOption(value);
891
+ if (option)
892
+ label = option.label;
893
+ }
894
+ return `
895
+ <span class="select-tag">
896
+ <span class="select-tag-text">${this._escapeHtml(label)}</span>
897
+ <span class="select-tag-remove" role="button" tabindex="-1" data-action="remove-tag" data-value="${this._escapeHtml(value)}">${closeIcon}</span>
898
+ </span>
899
+ `;
900
+ }).join("");
901
+ if (overflowCount > 0) {
902
+ tagsHtml += `<span class="select-tag select-tag-overflow">+${overflowCount}</span>`;
903
+ }
904
+ return `<div class="select-tags">${tagsHtml || `<span class="select-placeholder">${this.placeholder}</span>`}</div>`;
905
+ }
906
+ _renderDropdown() {
907
+ return `
908
+ <div class="select-dropdown" role="listbox" popover="manual">
909
+ ${this.searchable ? this._renderSearch() : ""}
910
+ <div class="select-options">
911
+ ${this.treeData ? this._renderTreeOptions() : this._renderFlatOptions()}
912
+ </div>
913
+ </div>
914
+ `;
915
+ }
916
+ _renderSearch() {
917
+ return `
918
+ <div class="select-search">
919
+ <span class="select-search-icon">${searchIcon}</span>
920
+ <input
921
+ type="text"
922
+ class="select-search-input"
923
+ placeholder="Search..."
924
+ value="${this._escapeHtml(this._searchValue)}"
925
+ data-action="search"
926
+ />
927
+ </div>
928
+ `;
929
+ }
930
+ _renderFlatOptions() {
931
+ const options = this._getFilteredOptions();
932
+ if (options.length === 0) {
933
+ return `<div class="select-empty">No options available</div>`;
934
+ }
935
+ const selectedValues = this._getSelectedValues();
936
+ const grouped = new Map;
937
+ const ungrouped = [];
938
+ for (const opt of options) {
939
+ if (opt.group) {
940
+ const group = grouped.get(opt.group) || [];
941
+ group.push(opt);
942
+ grouped.set(opt.group, group);
943
+ } else {
944
+ ungrouped.push(opt);
945
+ }
946
+ }
947
+ let html = "";
948
+ let index = 0;
949
+ for (const opt of ungrouped) {
950
+ html += this._renderOption(opt, selectedValues, index);
951
+ index++;
952
+ }
953
+ for (const [groupName, groupOptions] of grouped) {
954
+ html += `<div class="select-group-header">${this._escapeHtml(groupName)}</div>`;
955
+ for (const opt of groupOptions) {
956
+ html += this._renderOption(opt, selectedValues, index);
957
+ index++;
958
+ }
959
+ }
960
+ return html;
961
+ }
962
+ _renderOption(option, selectedValues, index) {
963
+ const isSelected = selectedValues.includes(option.value);
964
+ const isHighlighted = index === this._highlightedIndex;
965
+ const classes = [
966
+ "select-option",
967
+ isSelected ? "selected" : "",
968
+ isHighlighted ? "highlighted" : "",
969
+ option.disabled ? "disabled" : ""
970
+ ].filter(Boolean).join(" ");
971
+ return `
972
+ <button
973
+ type="button"
974
+ class="${classes}"
975
+ role="option"
976
+ aria-selected="${isSelected}"
977
+ ${option.disabled ? 'aria-disabled="true"' : ""}
978
+ data-action="select"
979
+ data-value="${this._escapeHtml(option.value)}"
980
+ >
981
+ ${this.multiple ? `<span class="select-option-checkbox">${isSelected ? checkIcon : ""}</span>` : ""}
982
+ <span class="select-option-label">${this._escapeHtml(option.label)}</span>
983
+ ${!this.multiple ? `<span class="select-option-check">${checkIcon}</span>` : ""}
984
+ </button>
985
+ `;
986
+ }
987
+ _renderTreeOptions() {
988
+ const flatNodes = this._getFlattenedTreeOptions();
989
+ if (flatNodes.length === 0) {
990
+ return `<div class="select-empty">No options available</div>`;
991
+ }
992
+ const selectedValues = this._getSelectedValues();
993
+ return flatNodes.map((node, index) => this._renderTreeNode(node, selectedValues, index)).join("");
994
+ }
995
+ _renderTreeNode(node, selectedValues, index) {
996
+ const { option, level, hasChildren, isExpanded } = node;
997
+ const isSelected = selectedValues.includes(option.value);
998
+ const isHighlighted = index === this._highlightedIndex;
999
+ const indentWidth = level * 1.25;
1000
+ const classes = [
1001
+ "select-tree-node",
1002
+ isSelected ? "selected" : "",
1003
+ isHighlighted ? "highlighted" : "",
1004
+ option.disabled ? "disabled" : ""
1005
+ ].filter(Boolean).join(" ");
1006
+ const toggleClasses = [
1007
+ "select-tree-toggle",
1008
+ isExpanded ? "expanded" : "",
1009
+ !hasChildren ? "hidden" : ""
1010
+ ].filter(Boolean).join(" ");
1011
+ return `
1012
+ <button
1013
+ type="button"
1014
+ class="${classes}"
1015
+ role="treeitem"
1016
+ aria-selected="${isSelected}"
1017
+ aria-expanded="${hasChildren ? isExpanded : undefined}"
1018
+ ${option.disabled ? 'aria-disabled="true"' : ""}
1019
+ data-action="select-tree"
1020
+ data-value="${this._escapeHtml(option.value)}"
1021
+ >
1022
+ <span class="select-tree-indent" style="width: ${indentWidth}rem"></span>
1023
+ <span class="${toggleClasses}" data-action="toggle-expand" data-value="${this._escapeHtml(option.value)}">${chevronRightIcon}</span>
1024
+ ${this.multiple ? `<span class="select-option-checkbox">${isSelected ? checkIcon : ""}</span>` : ""}
1025
+ <span class="select-option-label">${this._escapeHtml(option.label)}</span>
1026
+ ${!this.multiple ? `<span class="select-option-check">${checkIcon}</span>` : ""}
1027
+ </button>
1028
+ `;
1029
+ }
1030
+ _escapeHtml(str) {
1031
+ const div = document.createElement("div");
1032
+ div.textContent = str;
1033
+ return div.innerHTML;
1034
+ }
1035
+ update() {
1036
+ const searchInput = this.shadowRoot?.querySelector(".select-search-input");
1037
+ const hadFocus = searchInput && this.shadowRoot?.activeElement === searchInput;
1038
+ const cursorPosition = hadFocus ? searchInput.selectionStart : null;
1039
+ super.update();
1040
+ if (this._isOpen) {
1041
+ const dropdown = this.shadowRoot?.querySelector(".select-dropdown");
1042
+ const trigger = this.shadowRoot?.querySelector(".select-trigger");
1043
+ if (dropdown && trigger) {
1044
+ try {
1045
+ dropdown.showPopover();
1046
+ this._positionDropdown(dropdown, trigger);
1047
+ } catch {}
1048
+ }
1049
+ if (hadFocus) {
1050
+ const newSearchInput = this.shadowRoot?.querySelector(".select-search-input");
1051
+ if (newSearchInput) {
1052
+ newSearchInput.focus();
1053
+ if (cursorPosition !== null) {
1054
+ newSearchInput.setSelectionRange(cursorPosition, cursorPosition);
1055
+ }
1056
+ }
1057
+ }
1058
+ }
1059
+ }
1060
+ _setupEventDelegation() {
1061
+ this.shadowRoot?.addEventListener("click", (e) => {
1062
+ const target = e.target;
1063
+ const trigger = target.closest('[data-action="toggle"]');
1064
+ if (trigger && !target.closest('[data-action="clear"]') && !target.closest('[data-action="remove-tag"]')) {
1065
+ this._toggle();
1066
+ return;
1067
+ }
1068
+ if (target.closest('[data-action="clear"]')) {
1069
+ this._handleClear(e);
1070
+ return;
1071
+ }
1072
+ const removeTag = target.closest('[data-action="remove-tag"]');
1073
+ if (removeTag) {
1074
+ e.stopPropagation();
1075
+ const value = removeTag.getAttribute("data-value");
1076
+ if (value)
1077
+ this._removeTag(value);
1078
+ return;
1079
+ }
1080
+ const option = target.closest('[data-action="select"]');
1081
+ if (option) {
1082
+ const value = option.getAttribute("data-value");
1083
+ if (value)
1084
+ this._selectOption(value);
1085
+ return;
1086
+ }
1087
+ const toggle = target.closest('[data-action="toggle-expand"]');
1088
+ if (toggle) {
1089
+ const value = toggle.getAttribute("data-value");
1090
+ if (value)
1091
+ this._toggleExpand(value, e);
1092
+ return;
1093
+ }
1094
+ const treeNode = target.closest('[data-action="select-tree"]');
1095
+ if (treeNode) {
1096
+ const value = treeNode.getAttribute("data-value");
1097
+ if (value)
1098
+ this._selectOption(value);
1099
+ return;
1100
+ }
1101
+ });
1102
+ this.shadowRoot?.addEventListener("input", (e) => {
1103
+ const target = e.target;
1104
+ if (target.matches('[data-action="search"]')) {
1105
+ this._handleSearch(e);
1106
+ }
1107
+ });
1108
+ }
1109
+ }
1110
+ function register() {
1111
+ if (!customElements.get("el-dm-select")) {
1112
+ customElements.define("el-dm-select", ElDmSelect);
1113
+ }
1114
+ }
1115
+
1116
+ // src/register.ts
1117
+ register();
1118
+
1119
+ //# debugId=147406AA789C509564756E2164756E21
1120
+ //# sourceMappingURL=register.js.map