@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,1412 @@
1
+ /**
2
+ * @duskmoon-dev/el-select
3
+ *
4
+ * A unified select component supporting single, multi-select, and tree-select modes.
5
+ */
6
+
7
+ import { BaseElement, css } from '@duskmoon-dev/el-core';
8
+ import type { Size, ValidationState } from '@duskmoon-dev/el-core';
9
+
10
+ // Icons with explicit dimensions for proper rendering
11
+ const 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>`;
12
+ const 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>`;
13
+ const 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>`;
14
+ const 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>`;
15
+ const 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>`;
16
+
17
+ /**
18
+ * Option structure for flat select
19
+ */
20
+ export interface SelectOption {
21
+ value: string;
22
+ label: string;
23
+ disabled?: boolean;
24
+ group?: string;
25
+ }
26
+
27
+ /**
28
+ * Option structure for tree select
29
+ */
30
+ export interface TreeSelectOption {
31
+ value: string;
32
+ label: string;
33
+ disabled?: boolean;
34
+ children?: TreeSelectOption[];
35
+ }
36
+
37
+ /**
38
+ * Flattened tree node for rendering
39
+ */
40
+ interface FlattenedTreeNode {
41
+ option: TreeSelectOption;
42
+ level: number;
43
+ hasChildren: boolean;
44
+ isExpanded: boolean;
45
+ }
46
+
47
+ /**
48
+ * Event details
49
+ */
50
+ export interface SelectChangeEventDetail {
51
+ value: string;
52
+ selectedOptions: SelectOption[] | TreeSelectOption[];
53
+ }
54
+
55
+ export interface SelectSearchEventDetail {
56
+ searchValue: string;
57
+ }
58
+
59
+ export interface SelectExpandEventDetail {
60
+ node: TreeSelectOption;
61
+ expanded: boolean;
62
+ }
63
+
64
+ // Styles
65
+ const styles = css`
66
+ :host {
67
+ display: inline-block;
68
+ width: 100%;
69
+ }
70
+
71
+ .select {
72
+ position: relative;
73
+ width: 100%;
74
+ }
75
+
76
+ /* Trigger Button */
77
+ .select-trigger {
78
+ display: flex;
79
+ align-items: center;
80
+ gap: 0.5rem;
81
+ width: 100%;
82
+ min-height: 2.75rem;
83
+ padding: 0.5rem 0.75rem;
84
+ font-size: var(--font-size-md, 1rem);
85
+ line-height: 1.5;
86
+ color: var(--color-on-surface);
87
+ background-color: var(--color-surface);
88
+ border: 1px solid var(--color-outline);
89
+ border-radius: var(--radius-md, 0.5rem);
90
+ cursor: pointer;
91
+ transition: border-color 150ms ease, box-shadow 150ms ease;
92
+ }
93
+
94
+ .select-trigger:hover:not(:disabled) {
95
+ border-color: var(--color-on-surface-variant);
96
+ }
97
+
98
+ .select-trigger:focus {
99
+ outline: none;
100
+ border-color: var(--color-primary);
101
+ box-shadow: 0 0 0 3px color-mix(in oklch, var(--color-primary) 15%, transparent);
102
+ }
103
+
104
+ .select-trigger:disabled {
105
+ cursor: not-allowed;
106
+ opacity: 0.5;
107
+ background-color: var(--color-surface-container);
108
+ }
109
+
110
+ /* Value Display */
111
+ .select-value {
112
+ flex: 1;
113
+ overflow: hidden;
114
+ text-overflow: ellipsis;
115
+ white-space: nowrap;
116
+ text-align: left;
117
+ }
118
+
119
+ .select-placeholder {
120
+ color: var(--color-on-surface-variant);
121
+ opacity: 0.7;
122
+ }
123
+
124
+ /* Tags Container */
125
+ .select-tags {
126
+ display: flex;
127
+ flex-wrap: wrap;
128
+ gap: 0.25rem;
129
+ flex: 1;
130
+ min-width: 0;
131
+ }
132
+
133
+ .select-tag {
134
+ display: inline-flex;
135
+ align-items: center;
136
+ gap: 0.25rem;
137
+ max-width: 100%;
138
+ padding: 0.125rem 0.25rem 0.125rem 0.5rem;
139
+ font-size: var(--font-size-sm, 0.875rem);
140
+ line-height: 1.25rem;
141
+ background-color: var(--color-surface-container-high, #e8e8e8);
142
+ color: var(--color-on-surface);
143
+ border-radius: 1rem;
144
+ }
145
+
146
+ .select-tag-text {
147
+ overflow: hidden;
148
+ text-overflow: ellipsis;
149
+ white-space: nowrap;
150
+ }
151
+
152
+ .select-tag-remove {
153
+ display: inline-flex;
154
+ align-items: center;
155
+ justify-content: center;
156
+ width: 16px;
157
+ height: 16px;
158
+ padding: 0;
159
+ color: inherit;
160
+ background-color: transparent;
161
+ border-radius: 50%;
162
+ cursor: pointer;
163
+ opacity: 0.7;
164
+ transition: opacity 150ms ease, background-color 150ms ease;
165
+ }
166
+
167
+ .select-tag-remove svg {
168
+ width: 10px;
169
+ height: 10px;
170
+ display: block;
171
+ }
172
+
173
+ .select-tag-remove:hover {
174
+ opacity: 1;
175
+ background-color: color-mix(in oklch, currentColor 15%, transparent);
176
+ }
177
+
178
+ .select-tag-overflow {
179
+ padding: 0.125rem 0.5rem;
180
+ background-color: var(--color-surface-container);
181
+ color: var(--color-on-surface-variant);
182
+ }
183
+
184
+ /* Icons */
185
+ .select-arrow {
186
+ display: inline-flex;
187
+ align-items: center;
188
+ justify-content: center;
189
+ width: 20px;
190
+ height: 20px;
191
+ flex-shrink: 0;
192
+ color: var(--color-on-surface-variant);
193
+ transition: transform 150ms ease;
194
+ }
195
+
196
+ .select-arrow svg {
197
+ width: 16px;
198
+ height: 16px;
199
+ display: block;
200
+ }
201
+
202
+ .select.open .select-arrow {
203
+ transform: rotate(180deg);
204
+ }
205
+
206
+ .select-clear {
207
+ display: inline-flex;
208
+ align-items: center;
209
+ justify-content: center;
210
+ width: 20px;
211
+ height: 20px;
212
+ padding: 0;
213
+ color: var(--color-on-surface-variant);
214
+ background-color: transparent;
215
+ border-radius: 50%;
216
+ cursor: pointer;
217
+ flex-shrink: 0;
218
+ transition: background-color 150ms ease;
219
+ }
220
+
221
+ .select-clear svg {
222
+ width: 14px;
223
+ height: 14px;
224
+ display: block;
225
+ }
226
+
227
+ .select-clear:hover {
228
+ background-color: var(--color-surface-container-high);
229
+ }
230
+
231
+ /* Dropdown - uses Popover API (top-layer requires position: fixed) */
232
+ .select-dropdown {
233
+ position: fixed;
234
+ margin: 0;
235
+ padding: 0;
236
+ border: 1px solid var(--color-outline-variant);
237
+ border-radius: var(--radius-md, 0.5rem);
238
+ background-color: var(--color-surface);
239
+ box-shadow: var(--shadow-md, 0 4px 6px -1px rgba(0, 0, 0, 0.1));
240
+ max-height: 18rem;
241
+ overflow: hidden;
242
+ display: none;
243
+ flex-direction: column;
244
+ z-index: 1000;
245
+ }
246
+
247
+ .select-dropdown:popover-open {
248
+ display: flex;
249
+ }
250
+
251
+ /* Search */
252
+ .select-search {
253
+ display: flex;
254
+ align-items: center;
255
+ gap: 0.5rem;
256
+ padding: 0.5rem;
257
+ border-bottom: 1px solid var(--color-outline-variant);
258
+ }
259
+
260
+ .select-search-icon {
261
+ display: inline-flex;
262
+ align-items: center;
263
+ justify-content: center;
264
+ width: 16px;
265
+ height: 16px;
266
+ color: var(--color-on-surface-variant);
267
+ flex-shrink: 0;
268
+ }
269
+
270
+ .select-search-icon svg {
271
+ width: 14px;
272
+ height: 14px;
273
+ display: block;
274
+ }
275
+
276
+ .select-search-input {
277
+ flex: 1;
278
+ padding: 0.375rem 0.5rem;
279
+ font-size: var(--font-size-sm, 0.875rem);
280
+ color: var(--color-on-surface);
281
+ background-color: var(--color-surface-container);
282
+ border: none;
283
+ border-radius: var(--radius-sm, 0.25rem);
284
+ outline: none;
285
+ }
286
+
287
+ .select-search-input:focus {
288
+ background-color: var(--color-surface-container-high);
289
+ }
290
+
291
+ .select-search-input::placeholder {
292
+ color: var(--color-on-surface-variant);
293
+ opacity: 0.7;
294
+ }
295
+
296
+ /* Options List */
297
+ .select-options {
298
+ flex: 1;
299
+ overflow-y: auto;
300
+ padding: 0.25rem;
301
+ }
302
+
303
+ /* Option */
304
+ .select-option {
305
+ display: flex;
306
+ align-items: center;
307
+ gap: 0.5rem;
308
+ width: 100%;
309
+ padding: 0.5rem 0.75rem;
310
+ font-size: var(--font-size-sm, 0.875rem);
311
+ color: var(--color-on-surface);
312
+ background-color: transparent;
313
+ border: none;
314
+ border-radius: var(--radius-sm, 0.25rem);
315
+ cursor: pointer;
316
+ text-align: left;
317
+ transition: background-color 150ms ease;
318
+ }
319
+
320
+ .select-option:hover:not(.disabled) {
321
+ background-color: var(--color-surface-container);
322
+ }
323
+
324
+ .select-option.selected {
325
+ background-color: var(--color-primary-container, #e8def8);
326
+ color: var(--color-on-primary-container, #1d1b20);
327
+ }
328
+
329
+ .select-option.selected:hover {
330
+ background-color: color-mix(in oklch, var(--color-primary-container, #e8def8), black 5%);
331
+ }
332
+
333
+ .select-option.highlighted {
334
+ background-color: var(--color-surface-container-high);
335
+ }
336
+
337
+ .select-option.disabled {
338
+ opacity: 0.5;
339
+ cursor: not-allowed;
340
+ }
341
+
342
+ .select-option-checkbox {
343
+ display: flex;
344
+ align-items: center;
345
+ justify-content: center;
346
+ width: 1rem;
347
+ height: 1rem;
348
+ background-color: transparent;
349
+ border: 2px solid var(--color-on-surface-variant);
350
+ border-radius: 0.125rem;
351
+ flex-shrink: 0;
352
+ }
353
+
354
+ .select-option.selected .select-option-checkbox {
355
+ background-color: var(--color-primary);
356
+ border-color: var(--color-primary);
357
+ color: var(--color-on-primary, white);
358
+ }
359
+
360
+ .select-option-label {
361
+ flex: 1;
362
+ overflow: hidden;
363
+ text-overflow: ellipsis;
364
+ white-space: nowrap;
365
+ }
366
+
367
+ .select-option-check {
368
+ display: none;
369
+ color: var(--color-primary);
370
+ flex-shrink: 0;
371
+ }
372
+
373
+ .select-option.selected .select-option-check {
374
+ display: flex;
375
+ }
376
+
377
+ /* Group Header */
378
+ .select-group-header {
379
+ padding: 0.5rem 0.75rem 0.25rem;
380
+ font-size: var(--font-size-xs, 0.75rem);
381
+ font-weight: 600;
382
+ color: var(--color-on-surface-variant);
383
+ text-transform: uppercase;
384
+ letter-spacing: 0.05em;
385
+ }
386
+
387
+ /* Tree Node */
388
+ .select-tree-node {
389
+ display: flex;
390
+ align-items: center;
391
+ gap: 0.25rem;
392
+ width: 100%;
393
+ padding: 0.5rem 0.5rem;
394
+ font-size: var(--font-size-sm, 0.875rem);
395
+ color: var(--color-on-surface);
396
+ background-color: transparent;
397
+ border: none;
398
+ border-radius: var(--radius-sm, 0.25rem);
399
+ cursor: pointer;
400
+ text-align: left;
401
+ transition: background-color 150ms ease;
402
+ }
403
+
404
+ .select-tree-node:hover:not(.disabled) {
405
+ background-color: var(--color-surface-container);
406
+ }
407
+
408
+ .select-tree-node.selected {
409
+ background-color: var(--color-primary-container, #e8def8);
410
+ color: var(--color-on-primary-container, #1d1b20);
411
+ }
412
+
413
+ .select-tree-node.highlighted {
414
+ background-color: var(--color-surface-container-high);
415
+ }
416
+
417
+ .select-tree-node.disabled {
418
+ opacity: 0.5;
419
+ cursor: not-allowed;
420
+ }
421
+
422
+ .select-tree-toggle {
423
+ display: flex;
424
+ align-items: center;
425
+ justify-content: center;
426
+ width: 1.25rem;
427
+ height: 1.25rem;
428
+ padding: 0;
429
+ color: var(--color-on-surface-variant);
430
+ background-color: transparent;
431
+ border: none;
432
+ border-radius: var(--radius-sm, 0.25rem);
433
+ cursor: pointer;
434
+ flex-shrink: 0;
435
+ transition: transform 150ms ease;
436
+ }
437
+
438
+ .select-tree-toggle:hover {
439
+ background-color: var(--color-surface-container-high);
440
+ }
441
+
442
+ .select-tree-toggle.expanded {
443
+ transform: rotate(90deg);
444
+ }
445
+
446
+ .select-tree-toggle.hidden {
447
+ visibility: hidden;
448
+ }
449
+
450
+ .select-tree-indent {
451
+ flex-shrink: 0;
452
+ }
453
+
454
+ /* Empty State */
455
+ .select-empty {
456
+ padding: 1.5rem;
457
+ text-align: center;
458
+ color: var(--color-on-surface-variant);
459
+ font-size: var(--font-size-sm, 0.875rem);
460
+ }
461
+
462
+ /* Size Variants */
463
+ :host([size="sm"]) .select-trigger {
464
+ min-height: 2.25rem;
465
+ padding: 0.375rem 0.5rem;
466
+ font-size: var(--font-size-sm, 0.875rem);
467
+ border-radius: var(--radius-sm, 0.375rem);
468
+ }
469
+
470
+ :host([size="lg"]) .select-trigger {
471
+ min-height: 3.25rem;
472
+ padding: 0.625rem 1rem;
473
+ font-size: var(--font-size-lg, 1.125rem);
474
+ border-radius: var(--radius-lg, 0.625rem);
475
+ }
476
+
477
+ /* Validation States */
478
+ :host([validation-state="invalid"]) .select-trigger {
479
+ border-color: var(--color-error);
480
+ }
481
+
482
+ :host([validation-state="invalid"]) .select-trigger:focus {
483
+ border-color: var(--color-error);
484
+ box-shadow: 0 0 0 3px color-mix(in oklch, var(--color-error) 15%, transparent);
485
+ }
486
+
487
+ :host([validation-state="valid"]) .select-trigger {
488
+ border-color: var(--color-success);
489
+ }
490
+
491
+ /* Disabled State */
492
+ :host([disabled]) {
493
+ pointer-events: none;
494
+ }
495
+
496
+ :host([disabled]) .select-trigger {
497
+ cursor: not-allowed;
498
+ opacity: 0.5;
499
+ background-color: var(--color-surface-container);
500
+ }
501
+ `;
502
+
503
+ /**
504
+ * DuskMoon Select Element
505
+ *
506
+ * @element el-dm-select
507
+ *
508
+ * @attr {string} value - Selected value(s). JSON string for multiple/tree modes.
509
+ * @attr {string} placeholder - Placeholder text when no selection.
510
+ * @attr {boolean} disabled - Disable the select.
511
+ * @attr {boolean} readonly - Make the select readonly.
512
+ * @attr {boolean} multiple - Enable multi-select mode with tags.
513
+ * @attr {boolean} tree-data - Enable tree-select mode.
514
+ * @attr {boolean} searchable - Enable search/filter functionality.
515
+ * @attr {boolean} clearable - Show clear button.
516
+ * @attr {boolean} cascade - Enable cascade selection for tree mode.
517
+ * @attr {boolean} check-strictly - Independent selection for tree mode.
518
+ * @attr {boolean} default-expand-all - Expand all tree nodes by default.
519
+ * @attr {number} max-tag-count - Max visible tags in multi-select mode.
520
+ * @attr {string} size - Size variant: 'sm' | 'md' | 'lg'.
521
+ * @attr {string} validation-state - Validation state: 'valid' | 'invalid'.
522
+ *
523
+ * @fires change - Fired when selection changes.
524
+ * @fires search - Fired when search input changes.
525
+ * @fires clear - Fired when clear button is clicked.
526
+ * @fires expand - Fired when tree node is expanded/collapsed.
527
+ */
528
+ export class ElDmSelect extends BaseElement {
529
+ static properties = {
530
+ value: { type: String, reflect: true, default: '' },
531
+ placeholder: { type: String, default: 'Select...' },
532
+ disabled: { type: Boolean, reflect: true, default: false },
533
+ readonly: { type: Boolean, reflect: true, default: false },
534
+ multiple: { type: Boolean, reflect: true, default: false },
535
+ treeData: { type: Boolean, reflect: true, attribute: 'tree-data', default: false },
536
+ searchable: { type: Boolean, reflect: true, default: false },
537
+ clearable: { type: Boolean, reflect: true, default: false },
538
+ cascade: { type: Boolean, reflect: true, default: false },
539
+ checkStrictly: { type: Boolean, reflect: true, attribute: 'check-strictly', default: false },
540
+ defaultExpandAll: { type: Boolean, reflect: true, attribute: 'default-expand-all', default: false },
541
+ maxTagCount: { type: Number, attribute: 'max-tag-count', default: -1 },
542
+ size: { type: String, reflect: true, default: 'md' },
543
+ validationState: { type: String, reflect: true, attribute: 'validation-state' },
544
+ options: { type: String, default: '' },
545
+ treeOptions: { type: String, attribute: 'tree-options', default: '' },
546
+ };
547
+
548
+ // Declared properties
549
+ declare value: string;
550
+ declare placeholder: string;
551
+ declare disabled: boolean;
552
+ declare readonly: boolean;
553
+ declare multiple: boolean;
554
+ declare treeData: boolean;
555
+ declare searchable: boolean;
556
+ declare clearable: boolean;
557
+ declare cascade: boolean;
558
+ declare checkStrictly: boolean;
559
+ declare defaultExpandAll: boolean;
560
+ declare maxTagCount: number;
561
+ declare size: Size;
562
+ declare validationState: ValidationState;
563
+ declare options: string;
564
+ declare treeOptions: string;
565
+
566
+ // Internal state
567
+ private _isOpen = false;
568
+ private _searchValue = '';
569
+ private _highlightedIndex = -1;
570
+ private _expandedKeys = new Set<string>();
571
+ private _options: SelectOption[] = [];
572
+ private _treeOptions: TreeSelectOption[] = [];
573
+
574
+ // Bound handlers
575
+ private _handleOutsideClick = this._onOutsideClick.bind(this);
576
+ private _handleKeyDown = this._onKeyDown.bind(this);
577
+ private _handleScroll = this._onScroll.bind(this);
578
+ private _handleResize = this._onResize.bind(this);
579
+
580
+ constructor() {
581
+ super();
582
+ this.attachStyles(styles);
583
+ }
584
+
585
+ connectedCallback(): void {
586
+ super.connectedCallback();
587
+ document.addEventListener('click', this._handleOutsideClick);
588
+ document.addEventListener('keydown', this._handleKeyDown);
589
+
590
+ // Parse options from attributes (for static HTML/MDX usage)
591
+ this._parseOptionsFromAttributes();
592
+
593
+ // Initialize expanded keys if defaultExpandAll
594
+ if (this.defaultExpandAll && this.treeData) {
595
+ this._expandAllNodes();
596
+ }
597
+
598
+ // Set up event delegation once
599
+ this._setupEventDelegation();
600
+ }
601
+
602
+ /**
603
+ * Parse options from JSON string attributes
604
+ */
605
+ private _parseOptionsFromAttributes(): void {
606
+ // Parse flat options
607
+ if (this.options) {
608
+ try {
609
+ const parsed = JSON.parse(this.options);
610
+ if (Array.isArray(parsed)) {
611
+ this._options = parsed;
612
+ }
613
+ } catch {
614
+ // Invalid JSON, ignore
615
+ }
616
+ }
617
+
618
+ // Parse tree options
619
+ if (this.treeOptions) {
620
+ try {
621
+ const parsed = JSON.parse(this.treeOptions);
622
+ if (Array.isArray(parsed)) {
623
+ this._treeOptions = parsed;
624
+ }
625
+ } catch {
626
+ // Invalid JSON, ignore
627
+ }
628
+ }
629
+ }
630
+
631
+ disconnectedCallback(): void {
632
+ super.disconnectedCallback();
633
+ document.removeEventListener('click', this._handleOutsideClick);
634
+ document.removeEventListener('keydown', this._handleKeyDown);
635
+ this._removeScrollListeners();
636
+ }
637
+
638
+ private _addScrollListeners(): void {
639
+ window.addEventListener('scroll', this._handleScroll, true);
640
+ window.addEventListener('resize', this._handleResize);
641
+ }
642
+
643
+ private _removeScrollListeners(): void {
644
+ window.removeEventListener('scroll', this._handleScroll, true);
645
+ window.removeEventListener('resize', this._handleResize);
646
+ }
647
+
648
+ private _onScroll(): void {
649
+ if (this._isOpen) {
650
+ const dropdown = this.shadowRoot?.querySelector('.select-dropdown') as HTMLElement;
651
+ const trigger = this.shadowRoot?.querySelector('.select-trigger') as HTMLElement;
652
+ if (dropdown && trigger) {
653
+ this._positionDropdown(dropdown, trigger);
654
+ }
655
+ }
656
+ }
657
+
658
+ private _onResize(): void {
659
+ if (this._isOpen) {
660
+ this._close();
661
+ }
662
+ }
663
+
664
+ /**
665
+ * Set options for flat select mode
666
+ */
667
+ setOptions(options: SelectOption[]): void {
668
+ this._options = options;
669
+ this.update();
670
+ }
671
+
672
+ /**
673
+ * Set options for tree select mode
674
+ */
675
+ setTreeOptions(options: TreeSelectOption[]): void {
676
+ this._treeOptions = options;
677
+ if (this.defaultExpandAll) {
678
+ this._expandAllNodes();
679
+ }
680
+ this.update();
681
+ }
682
+
683
+ /**
684
+ * Get selected values as array
685
+ */
686
+ private _getSelectedValues(): string[] {
687
+ if (!this.value) return [];
688
+ if (this.multiple || this.treeData) {
689
+ try {
690
+ const parsed = JSON.parse(this.value);
691
+ return Array.isArray(parsed) ? parsed : [this.value];
692
+ } catch {
693
+ return this.value ? [this.value] : [];
694
+ }
695
+ }
696
+ return this.value ? [this.value] : [];
697
+ }
698
+
699
+ /**
700
+ * Get filtered options based on search
701
+ */
702
+ private _getFilteredOptions(): SelectOption[] {
703
+ if (!this._searchValue) return this._options;
704
+ const search = this._searchValue.toLowerCase();
705
+ return this._options.filter(
706
+ (opt) =>
707
+ opt.label.toLowerCase().includes(search) ||
708
+ opt.value.toLowerCase().includes(search)
709
+ );
710
+ }
711
+
712
+ /**
713
+ * Flatten tree options for rendering
714
+ */
715
+ private _getFlattenedTreeOptions(): FlattenedTreeNode[] {
716
+ const result: FlattenedTreeNode[] = [];
717
+ const search = this._searchValue.toLowerCase();
718
+
719
+ const flatten = (options: TreeSelectOption[], level: number): void => {
720
+ for (const option of options) {
721
+ const hasChildren = !!(option.children && option.children.length > 0);
722
+ const isExpanded = this._expandedKeys.has(option.value);
723
+
724
+ // Filter by search if searching
725
+ if (search) {
726
+ const matches = option.label.toLowerCase().includes(search);
727
+ const hasMatchingChild = hasChildren && this._hasMatchingDescendant(option, search);
728
+ if (!matches && !hasMatchingChild) continue;
729
+ }
730
+
731
+ result.push({ option, level, hasChildren, isExpanded });
732
+
733
+ if (hasChildren && isExpanded) {
734
+ flatten(option.children!, level + 1);
735
+ }
736
+ }
737
+ };
738
+
739
+ flatten(this._treeOptions, 0);
740
+ return result;
741
+ }
742
+
743
+ /**
744
+ * Check if a tree node has matching descendants
745
+ */
746
+ private _hasMatchingDescendant(option: TreeSelectOption, search: string): boolean {
747
+ if (!option.children) return false;
748
+ for (const child of option.children) {
749
+ if (child.label.toLowerCase().includes(search)) return true;
750
+ if (this._hasMatchingDescendant(child, search)) return true;
751
+ }
752
+ return false;
753
+ }
754
+
755
+ /**
756
+ * Expand all tree nodes
757
+ */
758
+ private _expandAllNodes(): void {
759
+ const expandAll = (options: TreeSelectOption[]): void => {
760
+ for (const option of options) {
761
+ if (option.children && option.children.length > 0) {
762
+ this._expandedKeys.add(option.value);
763
+ expandAll(option.children);
764
+ }
765
+ }
766
+ };
767
+ expandAll(this._treeOptions);
768
+ }
769
+
770
+ /**
771
+ * Get all descendant values of a tree node
772
+ */
773
+ private _getDescendantValues(option: TreeSelectOption): string[] {
774
+ const values: string[] = [];
775
+ const collect = (opt: TreeSelectOption): void => {
776
+ values.push(opt.value);
777
+ if (opt.children) {
778
+ opt.children.forEach(collect);
779
+ }
780
+ };
781
+ collect(option);
782
+ return values;
783
+ }
784
+
785
+ /**
786
+ * Find option by value in flat options
787
+ */
788
+ private _findOption(value: string): SelectOption | undefined {
789
+ return this._options.find((opt) => opt.value === value);
790
+ }
791
+
792
+ /**
793
+ * Find option by value in tree options
794
+ */
795
+ private _findTreeOption(value: string, options = this._treeOptions): TreeSelectOption | undefined {
796
+ for (const opt of options) {
797
+ if (opt.value === value) return opt;
798
+ if (opt.children) {
799
+ const found = this._findTreeOption(value, opt.children);
800
+ if (found) return found;
801
+ }
802
+ }
803
+ return undefined;
804
+ }
805
+
806
+ // Event handlers
807
+ private _onOutsideClick(e: MouseEvent): void {
808
+ if (!this.contains(e.target as Node)) {
809
+ this._close();
810
+ }
811
+ }
812
+
813
+ private _onKeyDown(e: KeyboardEvent): void {
814
+ if (!this._isOpen) {
815
+ if (e.key === 'Enter' || e.key === ' ' || e.key === 'ArrowDown') {
816
+ if (document.activeElement === this || this.contains(document.activeElement)) {
817
+ e.preventDefault();
818
+ this._open();
819
+ }
820
+ }
821
+ return;
822
+ }
823
+
824
+ const options = this.treeData ? this._getFlattenedTreeOptions() : this._getFilteredOptions();
825
+ const enabledOptions = this.treeData
826
+ ? (options as FlattenedTreeNode[]).filter((n) => !n.option.disabled)
827
+ : (options as SelectOption[]).filter((o) => !o.disabled);
828
+
829
+ switch (e.key) {
830
+ case 'ArrowDown':
831
+ e.preventDefault();
832
+ this._highlightedIndex = Math.min(
833
+ this._highlightedIndex + 1,
834
+ enabledOptions.length - 1
835
+ );
836
+ this.update();
837
+ break;
838
+
839
+ case 'ArrowUp':
840
+ e.preventDefault();
841
+ this._highlightedIndex = Math.max(this._highlightedIndex - 1, 0);
842
+ this.update();
843
+ break;
844
+
845
+ case 'Enter':
846
+ e.preventDefault();
847
+ if (this._highlightedIndex >= 0 && this._highlightedIndex < enabledOptions.length) {
848
+ const option = this.treeData
849
+ ? (enabledOptions[this._highlightedIndex] as FlattenedTreeNode).option
850
+ : (enabledOptions[this._highlightedIndex] as SelectOption);
851
+ this._selectOption(option.value);
852
+ }
853
+ break;
854
+
855
+ case 'Escape':
856
+ e.preventDefault();
857
+ this._close();
858
+ break;
859
+
860
+ case 'Backspace':
861
+ if (this.multiple && !this._searchValue) {
862
+ e.preventDefault();
863
+ const selected = this._getSelectedValues();
864
+ if (selected.length > 0) {
865
+ this._removeTag(selected[selected.length - 1]);
866
+ }
867
+ }
868
+ break;
869
+ }
870
+ }
871
+
872
+ private _open(): void {
873
+ if (this.disabled || this.readonly) return;
874
+ this._isOpen = true;
875
+ this._highlightedIndex = -1;
876
+ this.update();
877
+
878
+ // Add scroll/resize listeners to keep dropdown positioned
879
+ this._addScrollListeners();
880
+
881
+ // Show popover and position it
882
+ requestAnimationFrame(() => {
883
+ const dropdown = this.shadowRoot?.querySelector('.select-dropdown') as HTMLElement;
884
+ const trigger = this.shadowRoot?.querySelector('.select-trigger') as HTMLElement;
885
+ if (dropdown && trigger) {
886
+ // Set initial position before showing (based on trigger only)
887
+ const triggerRect = trigger.getBoundingClientRect();
888
+ dropdown.style.top = `${triggerRect.bottom + 4}px`;
889
+ dropdown.style.left = `${triggerRect.left}px`;
890
+ dropdown.style.width = `${triggerRect.width}px`;
891
+
892
+ // Show the popover
893
+ dropdown.showPopover();
894
+
895
+ // Recalculate position after layout is complete
896
+ requestAnimationFrame(() => {
897
+ this._positionDropdown(dropdown, trigger);
898
+ });
899
+ }
900
+
901
+ // Focus search input if searchable
902
+ const searchInput = this.shadowRoot?.querySelector('.select-search-input') as HTMLInputElement;
903
+ if (searchInput) {
904
+ searchInput.focus();
905
+ }
906
+ });
907
+ }
908
+
909
+ private _close(): void {
910
+ this._isOpen = false;
911
+ this._searchValue = '';
912
+ this._highlightedIndex = -1;
913
+
914
+ // Remove scroll/resize listeners
915
+ this._removeScrollListeners();
916
+
917
+ // Hide popover
918
+ const dropdown = this.shadowRoot?.querySelector('.select-dropdown') as HTMLElement;
919
+ if (dropdown) {
920
+ try {
921
+ dropdown.hidePopover();
922
+ } catch {
923
+ // Ignore if already hidden
924
+ }
925
+ }
926
+
927
+ this.update();
928
+ }
929
+
930
+ private _positionDropdown(dropdown: HTMLElement, trigger: HTMLElement): void {
931
+ const triggerRect = trigger.getBoundingClientRect();
932
+ const dropdownRect = dropdown.getBoundingClientRect();
933
+ const viewportHeight = window.innerHeight;
934
+
935
+ // Position below the trigger by default
936
+ let top = triggerRect.bottom + 4;
937
+ let left = triggerRect.left;
938
+
939
+ // If not enough space below, position above
940
+ if (top + dropdownRect.height > viewportHeight && triggerRect.top > dropdownRect.height) {
941
+ top = triggerRect.top - dropdownRect.height - 4;
942
+ }
943
+
944
+ // Ensure dropdown doesn't go off-screen horizontally
945
+ const viewportWidth = window.innerWidth;
946
+ if (left + triggerRect.width > viewportWidth) {
947
+ left = viewportWidth - triggerRect.width - 8;
948
+ }
949
+
950
+ dropdown.style.top = `${top}px`;
951
+ dropdown.style.left = `${left}px`;
952
+ dropdown.style.width = `${triggerRect.width}px`;
953
+ }
954
+
955
+ private _toggle(): void {
956
+ if (this._isOpen) {
957
+ this._close();
958
+ } else {
959
+ this._open();
960
+ }
961
+ }
962
+
963
+ private _selectOption(value: string): void {
964
+ const selectedValues = this._getSelectedValues();
965
+
966
+ if (this.multiple) {
967
+ // Toggle selection
968
+ const index = selectedValues.indexOf(value);
969
+ if (index >= 0) {
970
+ selectedValues.splice(index, 1);
971
+ } else {
972
+ // Handle cascade for tree mode
973
+ if (this.treeData && this.cascade && !this.checkStrictly) {
974
+ const option = this._findTreeOption(value);
975
+ if (option) {
976
+ const descendants = this._getDescendantValues(option);
977
+ for (const v of descendants) {
978
+ if (!selectedValues.includes(v)) {
979
+ selectedValues.push(v);
980
+ }
981
+ }
982
+ }
983
+ } else {
984
+ selectedValues.push(value);
985
+ }
986
+ }
987
+ this.value = JSON.stringify(selectedValues);
988
+ } else {
989
+ this.value = value;
990
+ this._close();
991
+ }
992
+
993
+ this._emitChange();
994
+ this.update();
995
+ }
996
+
997
+ private _removeTag(value: string): void {
998
+ const selectedValues = this._getSelectedValues();
999
+ const index = selectedValues.indexOf(value);
1000
+ if (index >= 0) {
1001
+ selectedValues.splice(index, 1);
1002
+ this.value = selectedValues.length > 0 ? JSON.stringify(selectedValues) : '';
1003
+ this._emitChange();
1004
+ this.update();
1005
+ }
1006
+ }
1007
+
1008
+ private _toggleExpand(value: string, e: Event): void {
1009
+ e.stopPropagation();
1010
+ const option = this._findTreeOption(value);
1011
+ if (!option) return;
1012
+
1013
+ const wasExpanded = this._expandedKeys.has(value);
1014
+ if (wasExpanded) {
1015
+ this._expandedKeys.delete(value);
1016
+ } else {
1017
+ this._expandedKeys.add(value);
1018
+ }
1019
+
1020
+ this.emit<SelectExpandEventDetail>('expand', {
1021
+ node: option,
1022
+ expanded: !wasExpanded,
1023
+ });
1024
+ this.update();
1025
+ }
1026
+
1027
+ private _handleSearch(e: Event): void {
1028
+ const input = e.target as HTMLInputElement;
1029
+ this._searchValue = input.value;
1030
+ this._highlightedIndex = -1;
1031
+ this.emit<SelectSearchEventDetail>('search', { searchValue: this._searchValue });
1032
+ this.update();
1033
+ }
1034
+
1035
+ private _handleClear(e: Event): void {
1036
+ e.stopPropagation();
1037
+ const previousValue = this.value;
1038
+ this.value = '';
1039
+ this.emit('clear', { previousValue });
1040
+ this._emitChange();
1041
+ this.update();
1042
+ }
1043
+
1044
+ private _emitChange(): void {
1045
+ const selectedValues = this._getSelectedValues();
1046
+ let selectedOptions: SelectOption[] | TreeSelectOption[];
1047
+
1048
+ if (this.treeData) {
1049
+ selectedOptions = selectedValues
1050
+ .map((v) => this._findTreeOption(v))
1051
+ .filter((o): o is TreeSelectOption => o !== undefined);
1052
+ } else {
1053
+ selectedOptions = selectedValues
1054
+ .map((v) => this._findOption(v))
1055
+ .filter((o): o is SelectOption => o !== undefined);
1056
+ }
1057
+
1058
+ this.emit<SelectChangeEventDetail>('change', {
1059
+ value: this.value,
1060
+ selectedOptions,
1061
+ });
1062
+ }
1063
+
1064
+ // Rendering
1065
+ protected render(): string {
1066
+ return `
1067
+ <div class="select ${this._isOpen ? 'open' : ''}">
1068
+ ${this._renderTrigger()}
1069
+ ${this._renderDropdown()}
1070
+ </div>
1071
+ `;
1072
+ }
1073
+
1074
+ private _renderTrigger(): string {
1075
+ const selectedValues = this._getSelectedValues();
1076
+ const hasValue = selectedValues.length > 0;
1077
+ const showClear = this.clearable && hasValue && !this.disabled && !this.readonly;
1078
+
1079
+ return `
1080
+ <button
1081
+ type="button"
1082
+ class="select-trigger"
1083
+ aria-haspopup="listbox"
1084
+ aria-expanded="${this._isOpen}"
1085
+ ${this.disabled ? 'disabled' : ''}
1086
+ data-action="toggle"
1087
+ >
1088
+ ${this.multiple && hasValue ? this._renderTags() : this._renderValue()}
1089
+ ${showClear ? `<span class="select-clear" role="button" tabindex="-1" data-action="clear">${closeIcon}</span>` : ''}
1090
+ <span class="select-arrow">${chevronDownIcon}</span>
1091
+ </button>
1092
+ `;
1093
+ }
1094
+
1095
+ private _renderValue(): string {
1096
+ const selectedValues = this._getSelectedValues();
1097
+ if (selectedValues.length === 0) {
1098
+ return `<span class="select-value select-placeholder">${this.placeholder}</span>`;
1099
+ }
1100
+
1101
+ const value = selectedValues[0];
1102
+ let label = value;
1103
+
1104
+ if (this.treeData) {
1105
+ const option = this._findTreeOption(value);
1106
+ if (option) label = option.label;
1107
+ } else {
1108
+ const option = this._findOption(value);
1109
+ if (option) label = option.label;
1110
+ }
1111
+
1112
+ return `<span class="select-value">${this._escapeHtml(label)}</span>`;
1113
+ }
1114
+
1115
+ private _renderTags(): string {
1116
+ const selectedValues = this._getSelectedValues();
1117
+ const maxCount = this.maxTagCount;
1118
+ const showAll = maxCount < 0;
1119
+ const visibleValues = showAll ? selectedValues : selectedValues.slice(0, maxCount);
1120
+ const overflowCount = showAll ? 0 : Math.max(0, selectedValues.length - maxCount);
1121
+
1122
+ let tagsHtml = visibleValues
1123
+ .map((value) => {
1124
+ let label = value;
1125
+ if (this.treeData) {
1126
+ const option = this._findTreeOption(value);
1127
+ if (option) label = option.label;
1128
+ } else {
1129
+ const option = this._findOption(value);
1130
+ if (option) label = option.label;
1131
+ }
1132
+
1133
+ return `
1134
+ <span class="select-tag">
1135
+ <span class="select-tag-text">${this._escapeHtml(label)}</span>
1136
+ <span class="select-tag-remove" role="button" tabindex="-1" data-action="remove-tag" data-value="${this._escapeHtml(value)}">${closeIcon}</span>
1137
+ </span>
1138
+ `;
1139
+ })
1140
+ .join('');
1141
+
1142
+ if (overflowCount > 0) {
1143
+ tagsHtml += `<span class="select-tag select-tag-overflow">+${overflowCount}</span>`;
1144
+ }
1145
+
1146
+ return `<div class="select-tags">${tagsHtml || `<span class="select-placeholder">${this.placeholder}</span>`}</div>`;
1147
+ }
1148
+
1149
+ private _renderDropdown(): string {
1150
+ return `
1151
+ <div class="select-dropdown" role="listbox" popover="manual">
1152
+ ${this.searchable ? this._renderSearch() : ''}
1153
+ <div class="select-options">
1154
+ ${this.treeData ? this._renderTreeOptions() : this._renderFlatOptions()}
1155
+ </div>
1156
+ </div>
1157
+ `;
1158
+ }
1159
+
1160
+ private _renderSearch(): string {
1161
+ return `
1162
+ <div class="select-search">
1163
+ <span class="select-search-icon">${searchIcon}</span>
1164
+ <input
1165
+ type="text"
1166
+ class="select-search-input"
1167
+ placeholder="Search..."
1168
+ value="${this._escapeHtml(this._searchValue)}"
1169
+ data-action="search"
1170
+ />
1171
+ </div>
1172
+ `;
1173
+ }
1174
+
1175
+ private _renderFlatOptions(): string {
1176
+ const options = this._getFilteredOptions();
1177
+ if (options.length === 0) {
1178
+ return `<div class="select-empty">No options available</div>`;
1179
+ }
1180
+
1181
+ const selectedValues = this._getSelectedValues();
1182
+
1183
+ // Group options if they have groups
1184
+ const grouped = new Map<string, SelectOption[]>();
1185
+ const ungrouped: SelectOption[] = [];
1186
+
1187
+ for (const opt of options) {
1188
+ if (opt.group) {
1189
+ const group = grouped.get(opt.group) || [];
1190
+ group.push(opt);
1191
+ grouped.set(opt.group, group);
1192
+ } else {
1193
+ ungrouped.push(opt);
1194
+ }
1195
+ }
1196
+
1197
+ let html = '';
1198
+ let index = 0;
1199
+
1200
+ // Render ungrouped first
1201
+ for (const opt of ungrouped) {
1202
+ html += this._renderOption(opt, selectedValues, index);
1203
+ index++;
1204
+ }
1205
+
1206
+ // Render grouped
1207
+ for (const [groupName, groupOptions] of grouped) {
1208
+ html += `<div class="select-group-header">${this._escapeHtml(groupName)}</div>`;
1209
+ for (const opt of groupOptions) {
1210
+ html += this._renderOption(opt, selectedValues, index);
1211
+ index++;
1212
+ }
1213
+ }
1214
+
1215
+ return html;
1216
+ }
1217
+
1218
+ private _renderOption(option: SelectOption, selectedValues: string[], index: number): string {
1219
+ const isSelected = selectedValues.includes(option.value);
1220
+ const isHighlighted = index === this._highlightedIndex;
1221
+ const classes = [
1222
+ 'select-option',
1223
+ isSelected ? 'selected' : '',
1224
+ isHighlighted ? 'highlighted' : '',
1225
+ option.disabled ? 'disabled' : '',
1226
+ ].filter(Boolean).join(' ');
1227
+
1228
+ return `
1229
+ <button
1230
+ type="button"
1231
+ class="${classes}"
1232
+ role="option"
1233
+ aria-selected="${isSelected}"
1234
+ ${option.disabled ? 'aria-disabled="true"' : ''}
1235
+ data-action="select"
1236
+ data-value="${this._escapeHtml(option.value)}"
1237
+ >
1238
+ ${this.multiple ? `<span class="select-option-checkbox">${isSelected ? checkIcon : ''}</span>` : ''}
1239
+ <span class="select-option-label">${this._escapeHtml(option.label)}</span>
1240
+ ${!this.multiple ? `<span class="select-option-check">${checkIcon}</span>` : ''}
1241
+ </button>
1242
+ `;
1243
+ }
1244
+
1245
+ private _renderTreeOptions(): string {
1246
+ const flatNodes = this._getFlattenedTreeOptions();
1247
+ if (flatNodes.length === 0) {
1248
+ return `<div class="select-empty">No options available</div>`;
1249
+ }
1250
+
1251
+ const selectedValues = this._getSelectedValues();
1252
+
1253
+ return flatNodes
1254
+ .map((node, index) => this._renderTreeNode(node, selectedValues, index))
1255
+ .join('');
1256
+ }
1257
+
1258
+ private _renderTreeNode(node: FlattenedTreeNode, selectedValues: string[], index: number): string {
1259
+ const { option, level, hasChildren, isExpanded } = node;
1260
+ const isSelected = selectedValues.includes(option.value);
1261
+ const isHighlighted = index === this._highlightedIndex;
1262
+ const indentWidth = level * 1.25;
1263
+
1264
+ const classes = [
1265
+ 'select-tree-node',
1266
+ isSelected ? 'selected' : '',
1267
+ isHighlighted ? 'highlighted' : '',
1268
+ option.disabled ? 'disabled' : '',
1269
+ ].filter(Boolean).join(' ');
1270
+
1271
+ const toggleClasses = [
1272
+ 'select-tree-toggle',
1273
+ isExpanded ? 'expanded' : '',
1274
+ !hasChildren ? 'hidden' : '',
1275
+ ].filter(Boolean).join(' ');
1276
+
1277
+ return `
1278
+ <button
1279
+ type="button"
1280
+ class="${classes}"
1281
+ role="treeitem"
1282
+ aria-selected="${isSelected}"
1283
+ aria-expanded="${hasChildren ? isExpanded : undefined}"
1284
+ ${option.disabled ? 'aria-disabled="true"' : ''}
1285
+ data-action="select-tree"
1286
+ data-value="${this._escapeHtml(option.value)}"
1287
+ >
1288
+ <span class="select-tree-indent" style="width: ${indentWidth}rem"></span>
1289
+ <span class="${toggleClasses}" data-action="toggle-expand" data-value="${this._escapeHtml(option.value)}">${chevronRightIcon}</span>
1290
+ ${this.multiple ? `<span class="select-option-checkbox">${isSelected ? checkIcon : ''}</span>` : ''}
1291
+ <span class="select-option-label">${this._escapeHtml(option.label)}</span>
1292
+ ${!this.multiple ? `<span class="select-option-check">${checkIcon}</span>` : ''}
1293
+ </button>
1294
+ `;
1295
+ }
1296
+
1297
+ private _escapeHtml(str: string): string {
1298
+ const div = document.createElement('div');
1299
+ div.textContent = str;
1300
+ return div.innerHTML;
1301
+ }
1302
+
1303
+ protected update(): void {
1304
+ // Preserve search input focus state before DOM replacement
1305
+ const searchInput = this.shadowRoot?.querySelector('.select-search-input') as HTMLInputElement;
1306
+ const hadFocus = searchInput && this.shadowRoot?.activeElement === searchInput;
1307
+ const cursorPosition = hadFocus ? searchInput.selectionStart : null;
1308
+
1309
+ super.update();
1310
+ // Event delegation is set up once in connectedCallback
1311
+
1312
+ // If dropdown is open, re-show the popover after DOM update
1313
+ // (since update() replaces the DOM, the new dropdown element needs showPopover())
1314
+ if (this._isOpen) {
1315
+ const dropdown = this.shadowRoot?.querySelector('.select-dropdown') as HTMLElement;
1316
+ const trigger = this.shadowRoot?.querySelector('.select-trigger') as HTMLElement;
1317
+ if (dropdown && trigger) {
1318
+ try {
1319
+ dropdown.showPopover();
1320
+ this._positionDropdown(dropdown, trigger);
1321
+ } catch {
1322
+ // Ignore if already shown or other errors
1323
+ }
1324
+ }
1325
+
1326
+ // Restore search input focus if it had focus before update
1327
+ if (hadFocus) {
1328
+ const newSearchInput = this.shadowRoot?.querySelector('.select-search-input') as HTMLInputElement;
1329
+ if (newSearchInput) {
1330
+ newSearchInput.focus();
1331
+ // Restore cursor position
1332
+ if (cursorPosition !== null) {
1333
+ newSearchInput.setSelectionRange(cursorPosition, cursorPosition);
1334
+ }
1335
+ }
1336
+ }
1337
+ }
1338
+ }
1339
+
1340
+ /**
1341
+ * Set up event delegation once (called in connectedCallback)
1342
+ */
1343
+ private _setupEventDelegation(): void {
1344
+ // Use event delegation on shadow root for all interactive elements
1345
+ this.shadowRoot?.addEventListener('click', (e) => {
1346
+ const target = e.target as HTMLElement;
1347
+
1348
+ // Toggle trigger
1349
+ const trigger = target.closest('[data-action="toggle"]');
1350
+ if (trigger && !target.closest('[data-action="clear"]') && !target.closest('[data-action="remove-tag"]')) {
1351
+ this._toggle();
1352
+ return;
1353
+ }
1354
+
1355
+ // Clear button
1356
+ if (target.closest('[data-action="clear"]')) {
1357
+ this._handleClear(e);
1358
+ return;
1359
+ }
1360
+
1361
+ // Tag remove button
1362
+ const removeTag = target.closest('[data-action="remove-tag"]');
1363
+ if (removeTag) {
1364
+ e.stopPropagation();
1365
+ const value = removeTag.getAttribute('data-value');
1366
+ if (value) this._removeTag(value);
1367
+ return;
1368
+ }
1369
+
1370
+ // Flat option
1371
+ const option = target.closest('[data-action="select"]');
1372
+ if (option) {
1373
+ const value = option.getAttribute('data-value');
1374
+ if (value) this._selectOption(value);
1375
+ return;
1376
+ }
1377
+
1378
+ // Tree expand toggle (check before tree node)
1379
+ const toggle = target.closest('[data-action="toggle-expand"]');
1380
+ if (toggle) {
1381
+ const value = toggle.getAttribute('data-value');
1382
+ if (value) this._toggleExpand(value, e);
1383
+ return;
1384
+ }
1385
+
1386
+ // Tree node
1387
+ const treeNode = target.closest('[data-action="select-tree"]');
1388
+ if (treeNode) {
1389
+ const value = treeNode.getAttribute('data-value');
1390
+ if (value) this._selectOption(value);
1391
+ return;
1392
+ }
1393
+ });
1394
+
1395
+ // Input event for search
1396
+ this.shadowRoot?.addEventListener('input', (e) => {
1397
+ const target = e.target as HTMLElement;
1398
+ if (target.matches('[data-action="search"]')) {
1399
+ this._handleSearch(e);
1400
+ }
1401
+ });
1402
+ }
1403
+ }
1404
+
1405
+ /**
1406
+ * Register the custom element
1407
+ */
1408
+ export function register(): void {
1409
+ if (!customElements.get('el-dm-select')) {
1410
+ customElements.define('el-dm-select', ElDmSelect);
1411
+ }
1412
+ }