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