@dodlhuat/basix 1.2.0 → 1.2.1
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.
- package/README.md +56 -1
- package/css/accordion.scss +86 -87
- package/css/alert.scss +137 -137
- package/css/button.scss +48 -0
- package/css/calendar.scss +957 -0
- package/css/card.scss +65 -65
- package/css/chart.scss +270 -157
- package/css/chat-bubbles.scss +134 -68
- package/css/chips.scss +109 -19
- package/css/colors.scss +32 -32
- package/css/datepicker.scss +336 -336
- package/css/defaults.scss +90 -90
- package/css/docs.scss +529 -0
- package/css/editor.scss +36 -0
- package/css/file-uploader.scss +1 -1
- package/css/flyout-menu.scss +361 -361
- package/css/form.scss +0 -15
- package/css/gallery.scss +65 -6
- package/css/grid.scss +41 -40
- package/css/group-picker.scss +345 -0
- package/css/guitar-chords.css +250 -250
- package/css/icons.scss +330 -330
- package/css/parameters.scss +3 -3
- package/css/placeholder.scss +33 -33
- package/css/popover.scss +206 -0
- package/css/progress.scss +76 -32
- package/css/properties.scss +51 -36
- package/css/push-menu.scss +302 -174
- package/css/reset.scss +39 -39
- package/css/scrollbar.scss +62 -5
- package/css/sidebar-nav.scss +92 -0
- package/css/spinner.scss +65 -65
- package/css/stepper.scss +48 -12
- package/css/style.css +3159 -254
- package/css/style.css.map +1 -1
- package/css/style.min.css +1 -1
- package/css/style.scss +51 -45
- package/css/table.scss +199 -199
- package/css/tabs.scss +154 -123
- package/css/timeline.scss +83 -38
- package/css/timepicker.scss +100 -5
- package/css/toast.scss +81 -81
- package/css/virtual-dropdown.scss +35 -29
- package/js/calendar.js +532 -0
- package/js/calendar.ts +706 -0
- package/js/chart.js +573 -257
- package/js/chart.ts +692 -0
- package/js/code-viewer.js +10 -10
- package/js/code-viewer.ts +188 -188
- package/js/datepicker.ts +627 -627
- package/js/docs-nav.js +204 -0
- package/js/dropdown.ts +179 -179
- package/js/editor.js +50 -6
- package/js/editor.ts +483 -444
- package/js/file-uploader.js +1 -0
- package/js/file-uploader.ts +1 -0
- package/js/flyout-menu.js +14 -14
- package/js/flyout-menu.ts +249 -249
- package/js/form-builder.js +106 -106
- package/js/gallery.js +14 -8
- package/js/gallery.ts +245 -236
- package/js/group-picker.js +342 -0
- package/js/group-picker.ts +447 -0
- package/js/guitar-chords.js +268 -268
- package/js/lazy-loader.js +121 -121
- package/js/modal.ts +166 -166
- package/js/popover.js +163 -0
- package/js/popover.ts +219 -0
- package/js/position.js +108 -0
- package/js/position.ts +111 -0
- package/js/push-menu.js +113 -0
- package/js/push-menu.ts +284 -145
- package/js/request.js +50 -50
- package/js/scroll.ts +47 -47
- package/js/scrollbar.js +13 -0
- package/js/scrollbar.ts +324 -307
- package/js/select.ts +216 -216
- package/js/sidebar-nav.js +41 -0
- package/js/sidebar-nav.ts +66 -0
- package/js/table.ts +452 -452
- package/js/tabs.ts +279 -279
- package/js/theme.js +17 -6
- package/js/theme.ts +234 -224
- package/js/toast.ts +137 -137
- package/js/tooltip.js +6 -60
- package/js/tooltip.ts +184 -251
- package/js/tsconfig.json +18 -18
- package/js/utils.ts +83 -83
- package/js/virtual-dropdown.js +25 -25
- package/js/virtual-dropdown.ts +365 -365
- package/package.json +39 -39
- package/js/index.js +0 -816
- package/js/index.ts +0 -987
package/js/virtual-dropdown.ts
CHANGED
|
@@ -1,366 +1,366 @@
|
|
|
1
|
-
interface DropdownOption {
|
|
2
|
-
label: string;
|
|
3
|
-
value: string | number;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
interface VirtualDropdownConfig {
|
|
7
|
-
container: string | HTMLElement;
|
|
8
|
-
options: DropdownOption[];
|
|
9
|
-
multiSelect?: boolean;
|
|
10
|
-
searchable?: boolean;
|
|
11
|
-
placeholder?: string;
|
|
12
|
-
renderLimit?: number;
|
|
13
|
-
itemHeight?: number;
|
|
14
|
-
onSelect?: (selectedValues: Array<string | number>) => void;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class VirtualDropdown {
|
|
18
|
-
private readonly container: HTMLElement;
|
|
19
|
-
private readonly options: DropdownOption[];
|
|
20
|
-
private readonly multiSelect: boolean;
|
|
21
|
-
private readonly searchable: boolean;
|
|
22
|
-
private readonly placeholder: string;
|
|
23
|
-
private readonly renderLimit: number;
|
|
24
|
-
private readonly itemHeight: number;
|
|
25
|
-
private readonly onSelect: ((selectedValues: Array<string | number>) => void) | null;
|
|
26
|
-
|
|
27
|
-
private trigger!: HTMLElement;
|
|
28
|
-
private triggerText!: HTMLElement;
|
|
29
|
-
private menu!: HTMLElement;
|
|
30
|
-
private listWrapper!: HTMLElement;
|
|
31
|
-
private scroller!: HTMLElement;
|
|
32
|
-
private spacer!: HTMLElement;
|
|
33
|
-
private content!: HTMLElement;
|
|
34
|
-
private searchInput?: HTMLInputElement;
|
|
35
|
-
|
|
36
|
-
private selectedValues: Set<string | number>;
|
|
37
|
-
private filteredOptions: DropdownOption[];
|
|
38
|
-
private isOpen: boolean;
|
|
39
|
-
private scrollTop: number;
|
|
40
|
-
|
|
41
|
-
private boundHandlers: Map<string, EventListener>;
|
|
42
|
-
|
|
43
|
-
constructor(config: VirtualDropdownConfig) {
|
|
44
|
-
const containerElement = typeof config.container === 'string'
|
|
45
|
-
? document.querySelector<HTMLElement>(config.container)
|
|
46
|
-
: config.container;
|
|
47
|
-
|
|
48
|
-
if (!containerElement) {
|
|
49
|
-
throw new Error('Container element not found');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this.container = containerElement;
|
|
53
|
-
this.options = config.options || [];
|
|
54
|
-
this.multiSelect = config.multiSelect ?? false;
|
|
55
|
-
this.searchable = config.searchable ?? false;
|
|
56
|
-
this.placeholder = config.placeholder || 'Select...';
|
|
57
|
-
this.renderLimit = config.renderLimit || 20;
|
|
58
|
-
this.itemHeight = config.itemHeight || 40;
|
|
59
|
-
this.onSelect = config.onSelect ?? null;
|
|
60
|
-
|
|
61
|
-
this.selectedValues = new Set();
|
|
62
|
-
this.filteredOptions = [...this.options];
|
|
63
|
-
this.isOpen = false;
|
|
64
|
-
this.scrollTop = 0;
|
|
65
|
-
this.boundHandlers = new Map();
|
|
66
|
-
|
|
67
|
-
this.init();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private init(): void {
|
|
71
|
-
this.container.classList.add('custom-dropdown');
|
|
72
|
-
this.renderBase();
|
|
73
|
-
this.bindEvents();
|
|
74
|
-
this.updateTrigger();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private renderBase(): void {
|
|
78
|
-
this.container.innerHTML = `
|
|
79
|
-
<div class="dropdown-trigger" tabindex="0" role="button" aria-haspopup="listbox" aria-expanded="false">
|
|
80
|
-
<span class="trigger-text">${this.escapeHtml(this.placeholder)}</span>
|
|
81
|
-
<span class="trigger-arrow" aria-hidden="true">▼</span>
|
|
82
|
-
</div>
|
|
83
|
-
<div class="dropdown-menu" role="listbox">
|
|
84
|
-
${this.searchable ? '<div class="dropdown-search"><input type="text" placeholder="Search..." aria-label="Search options"></div>' : ''}
|
|
85
|
-
<div class="dropdown-list-wrapper">
|
|
86
|
-
<div class="dropdown-list-scroller">
|
|
87
|
-
<div class="virtual-spacer"></div>
|
|
88
|
-
<div class="virtual-content"></div>
|
|
89
|
-
</div>
|
|
90
|
-
</div>
|
|
91
|
-
</div>
|
|
92
|
-
`;
|
|
93
|
-
|
|
94
|
-
this.trigger = this.querySelector('.dropdown-trigger');
|
|
95
|
-
this.triggerText = this.querySelector('.trigger-text');
|
|
96
|
-
this.menu = this.querySelector('.dropdown-menu');
|
|
97
|
-
this.listWrapper = this.querySelector('.dropdown-list-wrapper');
|
|
98
|
-
this.scroller = this.querySelector('.dropdown-list-scroller');
|
|
99
|
-
this.spacer = this.querySelector('.virtual-spacer');
|
|
100
|
-
this.content = this.querySelector('.virtual-content');
|
|
101
|
-
|
|
102
|
-
if (this.searchable) {
|
|
103
|
-
this.searchInput = this.querySelector('.dropdown-search input');
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
private querySelector<T extends HTMLElement>(selector: string): T {
|
|
108
|
-
const element = this.container.querySelector<T>(selector);
|
|
109
|
-
if (!element) {
|
|
110
|
-
throw new Error(`Required element not found: ${selector}`);
|
|
111
|
-
}
|
|
112
|
-
return element;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
private bindEvents(): void {
|
|
116
|
-
const handleTriggerClick = (): void => this.toggle();
|
|
117
|
-
this.trigger.addEventListener('click', handleTriggerClick);
|
|
118
|
-
this.boundHandlers.set('triggerClick', handleTriggerClick);
|
|
119
|
-
|
|
120
|
-
const handleTriggerKeydown = (e: KeyboardEvent): void => {
|
|
121
|
-
if (e.key === 'Enter' || e.key === ' ') {
|
|
122
|
-
e.preventDefault();
|
|
123
|
-
this.toggle();
|
|
124
|
-
} else if (e.key === 'Escape' && this.isOpen) {
|
|
125
|
-
this.close();
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
this.trigger.addEventListener('keydown', handleTriggerKeydown as EventListener);
|
|
129
|
-
this.boundHandlers.set('triggerKeydown', handleTriggerKeydown as EventListener);
|
|
130
|
-
|
|
131
|
-
const handleDocumentClick = (e: MouseEvent): void => {
|
|
132
|
-
if (!this.container.contains(e.target as Node)) {
|
|
133
|
-
this.close();
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
document.addEventListener('click', handleDocumentClick as EventListener);
|
|
137
|
-
this.boundHandlers.set('documentClick', handleDocumentClick as EventListener);
|
|
138
|
-
|
|
139
|
-
if (this.searchable && this.searchInput) {
|
|
140
|
-
const handleSearchInput = (e: Event): void => {
|
|
141
|
-
const target = e.target as HTMLInputElement;
|
|
142
|
-
this.handleSearch(target.value);
|
|
143
|
-
};
|
|
144
|
-
this.searchInput.addEventListener('input', handleSearchInput);
|
|
145
|
-
this.boundHandlers.set('searchInput', handleSearchInput);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const handleScroll = (e: Event): void => {
|
|
149
|
-
const target = e.target as HTMLElement;
|
|
150
|
-
this.scrollTop = target.scrollTop;
|
|
151
|
-
this.renderList();
|
|
152
|
-
};
|
|
153
|
-
this.listWrapper.addEventListener('scroll', handleScroll);
|
|
154
|
-
this.boundHandlers.set('scroll', handleScroll);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
private toggle(): void {
|
|
158
|
-
this.isOpen ? this.close() : this.open();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private open(): void {
|
|
162
|
-
this.isOpen = true;
|
|
163
|
-
this.container.classList.add('open');
|
|
164
|
-
this.menu.classList.add('open');
|
|
165
|
-
this.trigger.setAttribute('aria-expanded', 'true');
|
|
166
|
-
this.renderList();
|
|
167
|
-
|
|
168
|
-
if (this.searchable && this.searchInput) {
|
|
169
|
-
this.searchInput.focus();
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
private close(): void {
|
|
174
|
-
this.isOpen = false;
|
|
175
|
-
this.container.classList.remove('open');
|
|
176
|
-
this.menu.classList.remove('open');
|
|
177
|
-
this.trigger.setAttribute('aria-expanded', 'false');
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
private handleSearch(query: string): void {
|
|
181
|
-
if (!query.trim()) {
|
|
182
|
-
this.filteredOptions = [...this.options];
|
|
183
|
-
} else {
|
|
184
|
-
const lowerQuery = query.toLowerCase();
|
|
185
|
-
this.filteredOptions = this.options.filter(opt =>
|
|
186
|
-
opt.label.toLowerCase().includes(lowerQuery)
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
this.listWrapper.scrollTop = 0;
|
|
191
|
-
this.scrollTop = 0;
|
|
192
|
-
this.renderList();
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
private renderList(): void {
|
|
196
|
-
const totalHeight = this.filteredOptions.length * this.itemHeight;
|
|
197
|
-
this.spacer.style.height = `${totalHeight}px`;
|
|
198
|
-
|
|
199
|
-
const startIdx = Math.floor(this.scrollTop / this.itemHeight);
|
|
200
|
-
const buffer = 5;
|
|
201
|
-
const renderStart = Math.max(0, startIdx - buffer);
|
|
202
|
-
const renderEnd = Math.min(
|
|
203
|
-
this.filteredOptions.length,
|
|
204
|
-
startIdx + this.renderLimit + buffer
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
const offsetY = renderStart * this.itemHeight;
|
|
208
|
-
this.content.style.transform = `translateY(${offsetY}px)`;
|
|
209
|
-
|
|
210
|
-
const visibleOptions = this.filteredOptions.slice(renderStart, renderEnd);
|
|
211
|
-
|
|
212
|
-
this.content.innerHTML = visibleOptions
|
|
213
|
-
.map((opt, idx) => {
|
|
214
|
-
const realIdx = renderStart + idx;
|
|
215
|
-
const isSelected = this.selectedValues.has(opt.value);
|
|
216
|
-
return `
|
|
217
|
-
<div class="dropdown-item ${isSelected ? 'selected' : ''}"
|
|
218
|
-
data-value="${this.escapeHtml(String(opt.value))}"
|
|
219
|
-
data-idx="${realIdx}"
|
|
220
|
-
role="option"
|
|
221
|
-
aria-selected="${isSelected}"
|
|
222
|
-
tabindex="0"
|
|
223
|
-
style="height: ${this.itemHeight}px; line-height: ${this.itemHeight}px;">
|
|
224
|
-
${this.multiSelect ? `<input type="checkbox" ${isSelected ? 'checked' : ''} tabindex="-1" aria-hidden="true">` : ''}
|
|
225
|
-
<span class="item-label">${this.escapeHtml(opt.label)}</span>
|
|
226
|
-
</div>
|
|
227
|
-
`;
|
|
228
|
-
})
|
|
229
|
-
.join('');
|
|
230
|
-
|
|
231
|
-
this.content.querySelectorAll('.dropdown-item').forEach(item => {
|
|
232
|
-
const handleItemClick = (e: Event): void => {
|
|
233
|
-
e.stopPropagation();
|
|
234
|
-
const value = (item as HTMLElement).dataset.value;
|
|
235
|
-
if (value) {
|
|
236
|
-
this.handleSelect(value);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
const handleItemKeydown = (e: KeyboardEvent): void => {
|
|
241
|
-
if (e.key === 'Enter' || e.key === ' ') {
|
|
242
|
-
e.preventDefault();
|
|
243
|
-
const value = (item as HTMLElement).dataset.value;
|
|
244
|
-
if (value) {
|
|
245
|
-
this.handleSelect(value);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
item.addEventListener('click', handleItemClick);
|
|
251
|
-
item.addEventListener('keydown', handleItemKeydown as EventListener);
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
private handleSelect(valueString: string): void {
|
|
256
|
-
const selectedOpt = this.filteredOptions.find(
|
|
257
|
-
o => String(o.value) === valueString
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
if (!selectedOpt) return;
|
|
261
|
-
|
|
262
|
-
const val = selectedOpt.value;
|
|
263
|
-
|
|
264
|
-
if (this.multiSelect) {
|
|
265
|
-
if (this.selectedValues.has(val)) {
|
|
266
|
-
this.selectedValues.delete(val);
|
|
267
|
-
} else {
|
|
268
|
-
this.selectedValues.add(val);
|
|
269
|
-
}
|
|
270
|
-
this.renderList();
|
|
271
|
-
} else {
|
|
272
|
-
this.selectedValues.clear();
|
|
273
|
-
this.selectedValues.add(val);
|
|
274
|
-
this.close();
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
this.updateTrigger();
|
|
278
|
-
|
|
279
|
-
if (this.onSelect) {
|
|
280
|
-
this.onSelect(Array.from(this.selectedValues));
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
private updateTrigger(): void {
|
|
285
|
-
if (this.selectedValues.size === 0) {
|
|
286
|
-
this.triggerText.textContent = this.placeholder;
|
|
287
|
-
this.triggerText.classList.remove('has-value');
|
|
288
|
-
} else {
|
|
289
|
-
this.triggerText.classList.add('has-value');
|
|
290
|
-
|
|
291
|
-
if (this.multiSelect) {
|
|
292
|
-
const count = this.selectedValues.size;
|
|
293
|
-
this.triggerText.textContent = `${count} item${count !== 1 ? 's' : ''} selected`;
|
|
294
|
-
} else {
|
|
295
|
-
const val = Array.from(this.selectedValues)[0];
|
|
296
|
-
const opt = this.options.find(o => o.value === val);
|
|
297
|
-
this.triggerText.textContent = opt ? opt.label : String(val);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
private escapeHtml(text: string): string {
|
|
303
|
-
const div = document.createElement('div');
|
|
304
|
-
div.textContent = text;
|
|
305
|
-
return div.innerHTML;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
public getValue(): Array<string | number> {
|
|
309
|
-
return Array.from(this.selectedValues);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
public setValue(values: Array<string | number>): void {
|
|
313
|
-
this.selectedValues.clear();
|
|
314
|
-
values.forEach(val => {
|
|
315
|
-
if (this.options.some(opt => opt.value === val)) {
|
|
316
|
-
this.selectedValues.add(val);
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
this.updateTrigger();
|
|
320
|
-
if (this.isOpen) {
|
|
321
|
-
this.renderList();
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
public clearSelection(): void {
|
|
326
|
-
this.selectedValues.clear();
|
|
327
|
-
this.updateTrigger();
|
|
328
|
-
if (this.isOpen) {
|
|
329
|
-
this.renderList();
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
public destroy(): void {
|
|
334
|
-
const triggerClick = this.boundHandlers.get('triggerClick');
|
|
335
|
-
if (triggerClick) {
|
|
336
|
-
this.trigger.removeEventListener('click', triggerClick);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
const triggerKeydown = this.boundHandlers.get('triggerKeydown');
|
|
340
|
-
if (triggerKeydown) {
|
|
341
|
-
this.trigger.removeEventListener('keydown', triggerKeydown);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
const documentClick = this.boundHandlers.get('documentClick');
|
|
345
|
-
if (documentClick) {
|
|
346
|
-
document.removeEventListener('click', documentClick);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
const searchInput = this.boundHandlers.get('searchInput');
|
|
350
|
-
if (searchInput && this.searchInput) {
|
|
351
|
-
this.searchInput.removeEventListener('input', searchInput);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
const scroll = this.boundHandlers.get('scroll');
|
|
355
|
-
if (scroll) {
|
|
356
|
-
this.listWrapper.removeEventListener('scroll', scroll);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
this.boundHandlers.clear();
|
|
360
|
-
|
|
361
|
-
this.container.innerHTML = '';
|
|
362
|
-
this.container.classList.remove('custom-dropdown', 'open');
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
1
|
+
interface DropdownOption {
|
|
2
|
+
label: string;
|
|
3
|
+
value: string | number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface VirtualDropdownConfig {
|
|
7
|
+
container: string | HTMLElement;
|
|
8
|
+
options: DropdownOption[];
|
|
9
|
+
multiSelect?: boolean;
|
|
10
|
+
searchable?: boolean;
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
renderLimit?: number;
|
|
13
|
+
itemHeight?: number;
|
|
14
|
+
onSelect?: (selectedValues: Array<string | number>) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class VirtualDropdown {
|
|
18
|
+
private readonly container: HTMLElement;
|
|
19
|
+
private readonly options: DropdownOption[];
|
|
20
|
+
private readonly multiSelect: boolean;
|
|
21
|
+
private readonly searchable: boolean;
|
|
22
|
+
private readonly placeholder: string;
|
|
23
|
+
private readonly renderLimit: number;
|
|
24
|
+
private readonly itemHeight: number;
|
|
25
|
+
private readonly onSelect: ((selectedValues: Array<string | number>) => void) | null;
|
|
26
|
+
|
|
27
|
+
private trigger!: HTMLElement;
|
|
28
|
+
private triggerText!: HTMLElement;
|
|
29
|
+
private menu!: HTMLElement;
|
|
30
|
+
private listWrapper!: HTMLElement;
|
|
31
|
+
private scroller!: HTMLElement;
|
|
32
|
+
private spacer!: HTMLElement;
|
|
33
|
+
private content!: HTMLElement;
|
|
34
|
+
private searchInput?: HTMLInputElement;
|
|
35
|
+
|
|
36
|
+
private selectedValues: Set<string | number>;
|
|
37
|
+
private filteredOptions: DropdownOption[];
|
|
38
|
+
private isOpen: boolean;
|
|
39
|
+
private scrollTop: number;
|
|
40
|
+
|
|
41
|
+
private boundHandlers: Map<string, EventListener>;
|
|
42
|
+
|
|
43
|
+
constructor(config: VirtualDropdownConfig) {
|
|
44
|
+
const containerElement = typeof config.container === 'string'
|
|
45
|
+
? document.querySelector<HTMLElement>(config.container)
|
|
46
|
+
: config.container;
|
|
47
|
+
|
|
48
|
+
if (!containerElement) {
|
|
49
|
+
throw new Error('Container element not found');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.container = containerElement;
|
|
53
|
+
this.options = config.options || [];
|
|
54
|
+
this.multiSelect = config.multiSelect ?? false;
|
|
55
|
+
this.searchable = config.searchable ?? false;
|
|
56
|
+
this.placeholder = config.placeholder || 'Select...';
|
|
57
|
+
this.renderLimit = config.renderLimit || 20;
|
|
58
|
+
this.itemHeight = config.itemHeight || 40;
|
|
59
|
+
this.onSelect = config.onSelect ?? null;
|
|
60
|
+
|
|
61
|
+
this.selectedValues = new Set();
|
|
62
|
+
this.filteredOptions = [...this.options];
|
|
63
|
+
this.isOpen = false;
|
|
64
|
+
this.scrollTop = 0;
|
|
65
|
+
this.boundHandlers = new Map();
|
|
66
|
+
|
|
67
|
+
this.init();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private init(): void {
|
|
71
|
+
this.container.classList.add('custom-dropdown');
|
|
72
|
+
this.renderBase();
|
|
73
|
+
this.bindEvents();
|
|
74
|
+
this.updateTrigger();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private renderBase(): void {
|
|
78
|
+
this.container.innerHTML = `
|
|
79
|
+
<div class="dropdown-trigger" tabindex="0" role="button" aria-haspopup="listbox" aria-expanded="false">
|
|
80
|
+
<span class="trigger-text">${this.escapeHtml(this.placeholder)}</span>
|
|
81
|
+
<span class="trigger-arrow" aria-hidden="true">▼</span>
|
|
82
|
+
</div>
|
|
83
|
+
<div class="dropdown-menu" role="listbox">
|
|
84
|
+
${this.searchable ? '<div class="dropdown-search"><input type="text" placeholder="Search..." aria-label="Search options"></div>' : ''}
|
|
85
|
+
<div class="dropdown-list-wrapper">
|
|
86
|
+
<div class="dropdown-list-scroller">
|
|
87
|
+
<div class="virtual-spacer"></div>
|
|
88
|
+
<div class="virtual-content"></div>
|
|
89
|
+
</div>
|
|
90
|
+
</div>
|
|
91
|
+
</div>
|
|
92
|
+
`;
|
|
93
|
+
|
|
94
|
+
this.trigger = this.querySelector('.dropdown-trigger');
|
|
95
|
+
this.triggerText = this.querySelector('.trigger-text');
|
|
96
|
+
this.menu = this.querySelector('.dropdown-menu');
|
|
97
|
+
this.listWrapper = this.querySelector('.dropdown-list-wrapper');
|
|
98
|
+
this.scroller = this.querySelector('.dropdown-list-scroller');
|
|
99
|
+
this.spacer = this.querySelector('.virtual-spacer');
|
|
100
|
+
this.content = this.querySelector('.virtual-content');
|
|
101
|
+
|
|
102
|
+
if (this.searchable) {
|
|
103
|
+
this.searchInput = this.querySelector('.dropdown-search input');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private querySelector<T extends HTMLElement>(selector: string): T {
|
|
108
|
+
const element = this.container.querySelector<T>(selector);
|
|
109
|
+
if (!element) {
|
|
110
|
+
throw new Error(`Required element not found: ${selector}`);
|
|
111
|
+
}
|
|
112
|
+
return element;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private bindEvents(): void {
|
|
116
|
+
const handleTriggerClick = (): void => this.toggle();
|
|
117
|
+
this.trigger.addEventListener('click', handleTriggerClick);
|
|
118
|
+
this.boundHandlers.set('triggerClick', handleTriggerClick);
|
|
119
|
+
|
|
120
|
+
const handleTriggerKeydown = (e: KeyboardEvent): void => {
|
|
121
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
122
|
+
e.preventDefault();
|
|
123
|
+
this.toggle();
|
|
124
|
+
} else if (e.key === 'Escape' && this.isOpen) {
|
|
125
|
+
this.close();
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
this.trigger.addEventListener('keydown', handleTriggerKeydown as EventListener);
|
|
129
|
+
this.boundHandlers.set('triggerKeydown', handleTriggerKeydown as EventListener);
|
|
130
|
+
|
|
131
|
+
const handleDocumentClick = (e: MouseEvent): void => {
|
|
132
|
+
if (!this.container.contains(e.target as Node)) {
|
|
133
|
+
this.close();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
document.addEventListener('click', handleDocumentClick as EventListener);
|
|
137
|
+
this.boundHandlers.set('documentClick', handleDocumentClick as EventListener);
|
|
138
|
+
|
|
139
|
+
if (this.searchable && this.searchInput) {
|
|
140
|
+
const handleSearchInput = (e: Event): void => {
|
|
141
|
+
const target = e.target as HTMLInputElement;
|
|
142
|
+
this.handleSearch(target.value);
|
|
143
|
+
};
|
|
144
|
+
this.searchInput.addEventListener('input', handleSearchInput);
|
|
145
|
+
this.boundHandlers.set('searchInput', handleSearchInput);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const handleScroll = (e: Event): void => {
|
|
149
|
+
const target = e.target as HTMLElement;
|
|
150
|
+
this.scrollTop = target.scrollTop;
|
|
151
|
+
this.renderList();
|
|
152
|
+
};
|
|
153
|
+
this.listWrapper.addEventListener('scroll', handleScroll);
|
|
154
|
+
this.boundHandlers.set('scroll', handleScroll);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private toggle(): void {
|
|
158
|
+
this.isOpen ? this.close() : this.open();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private open(): void {
|
|
162
|
+
this.isOpen = true;
|
|
163
|
+
this.container.classList.add('open');
|
|
164
|
+
this.menu.classList.add('open');
|
|
165
|
+
this.trigger.setAttribute('aria-expanded', 'true');
|
|
166
|
+
this.renderList();
|
|
167
|
+
|
|
168
|
+
if (this.searchable && this.searchInput) {
|
|
169
|
+
this.searchInput.focus();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private close(): void {
|
|
174
|
+
this.isOpen = false;
|
|
175
|
+
this.container.classList.remove('open');
|
|
176
|
+
this.menu.classList.remove('open');
|
|
177
|
+
this.trigger.setAttribute('aria-expanded', 'false');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private handleSearch(query: string): void {
|
|
181
|
+
if (!query.trim()) {
|
|
182
|
+
this.filteredOptions = [...this.options];
|
|
183
|
+
} else {
|
|
184
|
+
const lowerQuery = query.toLowerCase();
|
|
185
|
+
this.filteredOptions = this.options.filter(opt =>
|
|
186
|
+
opt.label.toLowerCase().includes(lowerQuery)
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
this.listWrapper.scrollTop = 0;
|
|
191
|
+
this.scrollTop = 0;
|
|
192
|
+
this.renderList();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private renderList(): void {
|
|
196
|
+
const totalHeight = this.filteredOptions.length * this.itemHeight;
|
|
197
|
+
this.spacer.style.height = `${totalHeight}px`;
|
|
198
|
+
|
|
199
|
+
const startIdx = Math.floor(this.scrollTop / this.itemHeight);
|
|
200
|
+
const buffer = 5;
|
|
201
|
+
const renderStart = Math.max(0, startIdx - buffer);
|
|
202
|
+
const renderEnd = Math.min(
|
|
203
|
+
this.filteredOptions.length,
|
|
204
|
+
startIdx + this.renderLimit + buffer
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const offsetY = renderStart * this.itemHeight;
|
|
208
|
+
this.content.style.transform = `translateY(${offsetY}px)`;
|
|
209
|
+
|
|
210
|
+
const visibleOptions = this.filteredOptions.slice(renderStart, renderEnd);
|
|
211
|
+
|
|
212
|
+
this.content.innerHTML = visibleOptions
|
|
213
|
+
.map((opt, idx) => {
|
|
214
|
+
const realIdx = renderStart + idx;
|
|
215
|
+
const isSelected = this.selectedValues.has(opt.value);
|
|
216
|
+
return `
|
|
217
|
+
<div class="dropdown-item ${isSelected ? 'selected' : ''}"
|
|
218
|
+
data-value="${this.escapeHtml(String(opt.value))}"
|
|
219
|
+
data-idx="${realIdx}"
|
|
220
|
+
role="option"
|
|
221
|
+
aria-selected="${isSelected}"
|
|
222
|
+
tabindex="0"
|
|
223
|
+
style="height: ${this.itemHeight}px; line-height: ${this.itemHeight}px;">
|
|
224
|
+
${this.multiSelect ? `<input type="checkbox" ${isSelected ? 'checked' : ''} tabindex="-1" aria-hidden="true">` : ''}
|
|
225
|
+
<span class="item-label">${this.escapeHtml(opt.label)}</span>
|
|
226
|
+
</div>
|
|
227
|
+
`;
|
|
228
|
+
})
|
|
229
|
+
.join('');
|
|
230
|
+
|
|
231
|
+
this.content.querySelectorAll('.dropdown-item').forEach(item => {
|
|
232
|
+
const handleItemClick = (e: Event): void => {
|
|
233
|
+
e.stopPropagation();
|
|
234
|
+
const value = (item as HTMLElement).dataset.value;
|
|
235
|
+
if (value) {
|
|
236
|
+
this.handleSelect(value);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const handleItemKeydown = (e: KeyboardEvent): void => {
|
|
241
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
242
|
+
e.preventDefault();
|
|
243
|
+
const value = (item as HTMLElement).dataset.value;
|
|
244
|
+
if (value) {
|
|
245
|
+
this.handleSelect(value);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
item.addEventListener('click', handleItemClick);
|
|
251
|
+
item.addEventListener('keydown', handleItemKeydown as EventListener);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private handleSelect(valueString: string): void {
|
|
256
|
+
const selectedOpt = this.filteredOptions.find(
|
|
257
|
+
o => String(o.value) === valueString
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
if (!selectedOpt) return;
|
|
261
|
+
|
|
262
|
+
const val = selectedOpt.value;
|
|
263
|
+
|
|
264
|
+
if (this.multiSelect) {
|
|
265
|
+
if (this.selectedValues.has(val)) {
|
|
266
|
+
this.selectedValues.delete(val);
|
|
267
|
+
} else {
|
|
268
|
+
this.selectedValues.add(val);
|
|
269
|
+
}
|
|
270
|
+
this.renderList();
|
|
271
|
+
} else {
|
|
272
|
+
this.selectedValues.clear();
|
|
273
|
+
this.selectedValues.add(val);
|
|
274
|
+
this.close();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
this.updateTrigger();
|
|
278
|
+
|
|
279
|
+
if (this.onSelect) {
|
|
280
|
+
this.onSelect(Array.from(this.selectedValues));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private updateTrigger(): void {
|
|
285
|
+
if (this.selectedValues.size === 0) {
|
|
286
|
+
this.triggerText.textContent = this.placeholder;
|
|
287
|
+
this.triggerText.classList.remove('has-value');
|
|
288
|
+
} else {
|
|
289
|
+
this.triggerText.classList.add('has-value');
|
|
290
|
+
|
|
291
|
+
if (this.multiSelect) {
|
|
292
|
+
const count = this.selectedValues.size;
|
|
293
|
+
this.triggerText.textContent = `${count} item${count !== 1 ? 's' : ''} selected`;
|
|
294
|
+
} else {
|
|
295
|
+
const val = Array.from(this.selectedValues)[0];
|
|
296
|
+
const opt = this.options.find(o => o.value === val);
|
|
297
|
+
this.triggerText.textContent = opt ? opt.label : String(val);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private escapeHtml(text: string): string {
|
|
303
|
+
const div = document.createElement('div');
|
|
304
|
+
div.textContent = text;
|
|
305
|
+
return div.innerHTML;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
public getValue(): Array<string | number> {
|
|
309
|
+
return Array.from(this.selectedValues);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
public setValue(values: Array<string | number>): void {
|
|
313
|
+
this.selectedValues.clear();
|
|
314
|
+
values.forEach(val => {
|
|
315
|
+
if (this.options.some(opt => opt.value === val)) {
|
|
316
|
+
this.selectedValues.add(val);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
this.updateTrigger();
|
|
320
|
+
if (this.isOpen) {
|
|
321
|
+
this.renderList();
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
public clearSelection(): void {
|
|
326
|
+
this.selectedValues.clear();
|
|
327
|
+
this.updateTrigger();
|
|
328
|
+
if (this.isOpen) {
|
|
329
|
+
this.renderList();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
public destroy(): void {
|
|
334
|
+
const triggerClick = this.boundHandlers.get('triggerClick');
|
|
335
|
+
if (triggerClick) {
|
|
336
|
+
this.trigger.removeEventListener('click', triggerClick);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const triggerKeydown = this.boundHandlers.get('triggerKeydown');
|
|
340
|
+
if (triggerKeydown) {
|
|
341
|
+
this.trigger.removeEventListener('keydown', triggerKeydown);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const documentClick = this.boundHandlers.get('documentClick');
|
|
345
|
+
if (documentClick) {
|
|
346
|
+
document.removeEventListener('click', documentClick);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const searchInput = this.boundHandlers.get('searchInput');
|
|
350
|
+
if (searchInput && this.searchInput) {
|
|
351
|
+
this.searchInput.removeEventListener('input', searchInput);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const scroll = this.boundHandlers.get('scroll');
|
|
355
|
+
if (scroll) {
|
|
356
|
+
this.listWrapper.removeEventListener('scroll', scroll);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
this.boundHandlers.clear();
|
|
360
|
+
|
|
361
|
+
this.container.innerHTML = '';
|
|
362
|
+
this.container.classList.remove('custom-dropdown', 'open');
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
366
|
export { VirtualDropdown, DropdownOption, VirtualDropdownConfig };
|