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