thekselect 1.0.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.
- package/LICENSE +21 -0
- package/README.md +338 -0
- package/dist/core/config-utils.d.ts +5 -0
- package/dist/core/dom-renderer.d.ts +38 -0
- package/dist/core/event-emitter.d.ts +7 -0
- package/dist/core/options-logic.d.ts +4 -0
- package/dist/core/selection-logic.d.ts +20 -0
- package/dist/core/state.d.ts +10 -0
- package/dist/core/thekselect.d.ts +42 -0
- package/dist/core/types.d.ts +40 -0
- package/dist/css/base.css +37 -0
- package/dist/css/blue.css +14 -0
- package/dist/css/bootstrap.css +15 -0
- package/dist/css/dark.css +14 -0
- package/dist/css/forest.css +14 -0
- package/dist/css/gray.css +14 -0
- package/dist/css/material.css +15 -0
- package/dist/css/red.css +14 -0
- package/dist/css/tailwind.css +15 -0
- package/dist/index.d.ts +2 -0
- package/dist/thekselect.js +1169 -0
- package/dist/thekselect.min.js +1 -0
- package/dist/thekselect.umd.cjs +1171 -0
- package/dist/thekselect.umd.min.cjs +1 -0
- package/dist/utils/debounce.d.ts +4 -0
- package/dist/utils/dom.d.ts +2 -0
- package/dist/utils/event-manager.d.ts +15 -0
- package/dist/utils/styles.d.ts +2 -0
- package/package.json +74 -0
|
@@ -0,0 +1,1169 @@
|
|
|
1
|
+
//#region src/core/state.ts
|
|
2
|
+
var StateManager = class {
|
|
3
|
+
state;
|
|
4
|
+
listeners = /* @__PURE__ */ new Set();
|
|
5
|
+
constructor(initialState) {
|
|
6
|
+
this.state = { ...initialState };
|
|
7
|
+
}
|
|
8
|
+
getState() {
|
|
9
|
+
return { ...this.state };
|
|
10
|
+
}
|
|
11
|
+
setState(newState) {
|
|
12
|
+
const oldState = this.state;
|
|
13
|
+
this.state = {
|
|
14
|
+
...this.state,
|
|
15
|
+
...newState
|
|
16
|
+
};
|
|
17
|
+
if (Object.keys(newState).some((key) => {
|
|
18
|
+
const val = newState[key];
|
|
19
|
+
const oldVal = oldState[key];
|
|
20
|
+
if (Array.isArray(val) && Array.isArray(oldVal)) return val.length !== oldVal.length || val.some((item, index) => item !== oldVal[index]);
|
|
21
|
+
return val !== oldVal;
|
|
22
|
+
})) this.notify();
|
|
23
|
+
}
|
|
24
|
+
subscribe(listener) {
|
|
25
|
+
this.listeners.add(listener);
|
|
26
|
+
return () => this.listeners.delete(listener);
|
|
27
|
+
}
|
|
28
|
+
notify() {
|
|
29
|
+
const currentState = this.getState();
|
|
30
|
+
this.listeners.forEach((listener) => listener(currentState));
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/utils/debounce.ts
|
|
35
|
+
function debounce(fn, delay) {
|
|
36
|
+
let timeoutId = null;
|
|
37
|
+
const debounced = ((...args) => {
|
|
38
|
+
if (timeoutId !== null) clearTimeout(timeoutId);
|
|
39
|
+
timeoutId = setTimeout(() => {
|
|
40
|
+
timeoutId = null;
|
|
41
|
+
fn(...args);
|
|
42
|
+
}, delay);
|
|
43
|
+
});
|
|
44
|
+
debounced.cancel = () => {
|
|
45
|
+
if (timeoutId !== null) {
|
|
46
|
+
clearTimeout(timeoutId);
|
|
47
|
+
timeoutId = null;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return debounced;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/utils/dom.ts
|
|
54
|
+
function generateId(prefix = "thek") {
|
|
55
|
+
return `${prefix}-${Math.random().toString(36).substr(2, 9)}`;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/utils/styles.ts
|
|
59
|
+
var BASE_STYLES = `:root {
|
|
60
|
+
--thek-primary: #0f172a;
|
|
61
|
+
--thek-primary-light: #f1f5f9;
|
|
62
|
+
--thek-bg-surface: #ffffff;
|
|
63
|
+
--thek-bg-panel: #f8fafc;
|
|
64
|
+
--thek-bg-subtle: #f1f5f9;
|
|
65
|
+
--thek-border: #e2e8f0;
|
|
66
|
+
--thek-border-strong: #cbd5e1;
|
|
67
|
+
--thek-text-main: #0f172a;
|
|
68
|
+
--thek-text-muted: #64748b;
|
|
69
|
+
--thek-text-inverse: #ffffff;
|
|
70
|
+
--thek-danger: #ef4444;
|
|
71
|
+
--thek-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
|
|
72
|
+
--thek-input-height: 40px;
|
|
73
|
+
--thek-height-sm: 32px;
|
|
74
|
+
--thek-height-md: 40px;
|
|
75
|
+
--thek-height-lg: 48px;
|
|
76
|
+
--thek-item-padding: 8px 10px;
|
|
77
|
+
--thek-font-family: inherit;
|
|
78
|
+
--thek-border-radius: 8px;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@media (prefers-color-scheme: dark) {
|
|
82
|
+
:root {
|
|
83
|
+
--thek-primary: #38bdf8;
|
|
84
|
+
--thek-primary-light: rgba(56, 189, 248, 0.15);
|
|
85
|
+
--thek-bg-surface: #0f172a;
|
|
86
|
+
--thek-bg-panel: #334155;
|
|
87
|
+
--thek-bg-subtle: #475569;
|
|
88
|
+
--thek-border: #334155;
|
|
89
|
+
--thek-border-strong: #475569;
|
|
90
|
+
--thek-text-main: #f8fafc;
|
|
91
|
+
--thek-text-muted: #94a3b8;
|
|
92
|
+
--thek-text-inverse: #0f172a;
|
|
93
|
+
--thek-danger: #f43f5e;
|
|
94
|
+
--thek-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -4px rgba(0, 0, 0, 0.4);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
[data-theme='dark'] {
|
|
99
|
+
--thek-primary: #38bdf8;
|
|
100
|
+
--thek-primary-light: rgba(56, 189, 248, 0.15);
|
|
101
|
+
--thek-bg-surface: #0f172a;
|
|
102
|
+
--thek-bg-panel: #334155;
|
|
103
|
+
--thek-bg-subtle: #475569;
|
|
104
|
+
--thek-border: #334155;
|
|
105
|
+
--thek-border-strong: #475569;
|
|
106
|
+
--thek-text-main: #f8fafc;
|
|
107
|
+
--thek-text-muted: #94a3b8;
|
|
108
|
+
--thek-text-inverse: #0f172a;
|
|
109
|
+
--thek-danger: #f43f5e;
|
|
110
|
+
--thek-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -4px rgba(0, 0, 0, 0.4);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.thek-select {
|
|
114
|
+
position: relative;
|
|
115
|
+
width: 100%;
|
|
116
|
+
font-family: var(--thek-font-family);
|
|
117
|
+
box-sizing: border-box;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.thek-select *, .thek-dropdown * {
|
|
121
|
+
box-sizing: border-box;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.thek-control {
|
|
125
|
+
display: flex;
|
|
126
|
+
align-items: center;
|
|
127
|
+
justify-content: space-between;
|
|
128
|
+
padding: 4px 12px;
|
|
129
|
+
cursor: pointer;
|
|
130
|
+
min-height: var(--thek-input-height);
|
|
131
|
+
background-color: var(--thek-bg-surface);
|
|
132
|
+
color: var(--thek-text-main);
|
|
133
|
+
border: 1px solid var(--thek-border);
|
|
134
|
+
border-radius: var(--thek-border-radius);
|
|
135
|
+
transition: all 0.2s ease;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
.thek-control:hover {
|
|
139
|
+
border-color: var(--thek-border-strong);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
.thek-select.thek-open .thek-control {
|
|
143
|
+
border-color: var(--thek-primary);
|
|
144
|
+
box-shadow: 0 0 0 2px var(--thek-primary-light);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.thek-placeholder {
|
|
148
|
+
color: var(--thek-text-muted);
|
|
149
|
+
margin-left: 2px;
|
|
150
|
+
font-size: 0.95em;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.thek-indicators {
|
|
154
|
+
display: flex;
|
|
155
|
+
align-items: center;
|
|
156
|
+
padding-left: 12px;
|
|
157
|
+
color: var(--thek-text-muted);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.thek-arrow {
|
|
161
|
+
font-size: 0.8em;
|
|
162
|
+
transition: transform 0.2s ease;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
.thek-open .thek-arrow {
|
|
166
|
+
transform: rotate(180deg);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.thek-selection {
|
|
170
|
+
display: flex;
|
|
171
|
+
flex-wrap: nowrap;
|
|
172
|
+
overflow: hidden;
|
|
173
|
+
gap: 6px;
|
|
174
|
+
flex: 1;
|
|
175
|
+
-webkit-mask-image: linear-gradient(to right, black 90%, transparent 100%);
|
|
176
|
+
mask-image: linear-gradient(to right, black 90%, transparent 100%);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.thek-summary-text {
|
|
180
|
+
font-size: 0.9em;
|
|
181
|
+
color: var(--thek-text-main);
|
|
182
|
+
white-space: nowrap;
|
|
183
|
+
font-weight: 500;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
.thek-tag {
|
|
187
|
+
flex-shrink: 0;
|
|
188
|
+
background-color: var(--thek-bg-panel);
|
|
189
|
+
border: 1px solid var(--thek-border);
|
|
190
|
+
color: var(--thek-text-main);
|
|
191
|
+
border-radius: calc(var(--thek-border-radius) - 2px);
|
|
192
|
+
padding: 2px 8px;
|
|
193
|
+
font-size: 0.8em;
|
|
194
|
+
display: flex;
|
|
195
|
+
align-items: center;
|
|
196
|
+
gap: 6px;
|
|
197
|
+
font-weight: 500;
|
|
198
|
+
transition: background-color 0.1s;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
.thek-tag:hover {
|
|
202
|
+
background-color: var(--thek-bg-subtle);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
.thek-tag-remove {
|
|
206
|
+
cursor: pointer;
|
|
207
|
+
color: var(--thek-text-muted);
|
|
208
|
+
font-size: 1.1em;
|
|
209
|
+
line-height: 1;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
.thek-tag-remove:hover {
|
|
213
|
+
color: var(--thek-danger);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
.thek-dropdown {
|
|
217
|
+
background-color: var(--thek-bg-surface);
|
|
218
|
+
border: 1px solid var(--thek-border);
|
|
219
|
+
border-radius: var(--thek-border-radius);
|
|
220
|
+
box-shadow: var(--thek-shadow);
|
|
221
|
+
overflow: hidden;
|
|
222
|
+
box-sizing: border-box;
|
|
223
|
+
margin-top: 4px;
|
|
224
|
+
animation: thek-fade-in 0.15s ease-out;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
@keyframes thek-fade-in {
|
|
228
|
+
from { opacity: 0; transform: translateY(-4px); }
|
|
229
|
+
to { opacity: 1; transform: translateY(0); }
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
.thek-search-wrapper {
|
|
233
|
+
padding: 10px;
|
|
234
|
+
border-bottom: 1px solid var(--thek-border);
|
|
235
|
+
position: relative;
|
|
236
|
+
background-color: var(--thek-bg-surface);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
.thek-input {
|
|
240
|
+
width: 100%;
|
|
241
|
+
border: 1px solid var(--thek-border);
|
|
242
|
+
background: var(--thek-bg-surface);
|
|
243
|
+
padding: 8px 12px 8px 34px;
|
|
244
|
+
border-radius: calc(var(--thek-border-radius) - 2px);
|
|
245
|
+
color: var(--thek-text-main);
|
|
246
|
+
outline: none;
|
|
247
|
+
font-size: 0.9em;
|
|
248
|
+
transition: border-color 0.2s;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
.thek-input:focus {
|
|
252
|
+
border-color: var(--thek-primary);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
.thek-search-icon {
|
|
256
|
+
position: absolute;
|
|
257
|
+
left: 22px;
|
|
258
|
+
top: 50%;
|
|
259
|
+
transform: translateY(-50%);
|
|
260
|
+
color: var(--thek-text-muted);
|
|
261
|
+
font-size: 0.85em;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
.thek-options {
|
|
265
|
+
list-style: none;
|
|
266
|
+
margin: 0;
|
|
267
|
+
padding: 6px;
|
|
268
|
+
max-height: 240px;
|
|
269
|
+
overflow-y: auto;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
.thek-option {
|
|
273
|
+
padding: var(--thek-item-padding);
|
|
274
|
+
border-radius: 6px;
|
|
275
|
+
cursor: pointer;
|
|
276
|
+
display: flex;
|
|
277
|
+
align-items: center;
|
|
278
|
+
gap: 10px;
|
|
279
|
+
transition: all 0.1s ease;
|
|
280
|
+
color: var(--thek-text-main);
|
|
281
|
+
font-size: 0.95em;
|
|
282
|
+
margin-bottom: 2px;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
.thek-option:last-child {
|
|
286
|
+
margin-bottom: 0;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.thek-option:hover {
|
|
290
|
+
background-color: var(--thek-bg-panel);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
.thek-option.thek-focused {
|
|
294
|
+
background-color: var(--thek-bg-panel);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
.thek-option.thek-selected {
|
|
298
|
+
background-color: var(--thek-primary-light);
|
|
299
|
+
color: var(--thek-primary);
|
|
300
|
+
font-weight: 500;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
.thek-checkbox {
|
|
304
|
+
width: 1.2em;
|
|
305
|
+
height: 1.2em;
|
|
306
|
+
border: 1.5px solid var(--thek-border-strong);
|
|
307
|
+
border-radius: 4px;
|
|
308
|
+
display: flex;
|
|
309
|
+
align-items: center;
|
|
310
|
+
justify-content: center;
|
|
311
|
+
background: var(--thek-bg-surface);
|
|
312
|
+
font-size: 0.7em;
|
|
313
|
+
flex-shrink: 0;
|
|
314
|
+
transition: all 0.2s;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
.thek-option.thek-selected .thek-checkbox {
|
|
318
|
+
background-color: var(--thek-primary);
|
|
319
|
+
border-color: var(--thek-primary);
|
|
320
|
+
color: var(--thek-text-inverse);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
.thek-option.thek-disabled {
|
|
324
|
+
opacity: 0.4;
|
|
325
|
+
cursor: not-allowed;
|
|
326
|
+
background-color: transparent;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
.thek-no-results, .thek-loading {
|
|
330
|
+
padding: 20px 12px;
|
|
331
|
+
text-align: center;
|
|
332
|
+
color: var(--thek-text-muted);
|
|
333
|
+
font-size: 0.9em;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
.thek-disabled .thek-control {
|
|
337
|
+
background-color: var(--thek-bg-subtle);
|
|
338
|
+
cursor: not-allowed;
|
|
339
|
+
opacity: 0.7;
|
|
340
|
+
border-color: var(--thek-border);
|
|
341
|
+
}
|
|
342
|
+
`;
|
|
343
|
+
var injected = false;
|
|
344
|
+
function injectStyles() {
|
|
345
|
+
if (injected || typeof document === "undefined") return;
|
|
346
|
+
const style = document.createElement("style");
|
|
347
|
+
style.id = "thekselect-base-styles";
|
|
348
|
+
style.textContent = BASE_STYLES;
|
|
349
|
+
document.head.appendChild(style);
|
|
350
|
+
injected = true;
|
|
351
|
+
}
|
|
352
|
+
//#endregion
|
|
353
|
+
//#region src/core/dom-renderer.ts
|
|
354
|
+
var DomRenderer = class {
|
|
355
|
+
wrapper;
|
|
356
|
+
control;
|
|
357
|
+
selectionContainer;
|
|
358
|
+
indicatorsContainer;
|
|
359
|
+
placeholderElement;
|
|
360
|
+
input;
|
|
361
|
+
dropdown;
|
|
362
|
+
optionsList;
|
|
363
|
+
lastState = null;
|
|
364
|
+
lastFilteredOptions = [];
|
|
365
|
+
constructor(config, id, callbacks) {
|
|
366
|
+
this.config = config;
|
|
367
|
+
this.id = id;
|
|
368
|
+
this.callbacks = callbacks;
|
|
369
|
+
}
|
|
370
|
+
normalizeHeight(value) {
|
|
371
|
+
if (typeof value === "number") return `${value}px`;
|
|
372
|
+
const trimmed = value.trim();
|
|
373
|
+
if (/^\d+(\.\d+)?$/.test(trimmed)) return `${trimmed}px`;
|
|
374
|
+
return trimmed;
|
|
375
|
+
}
|
|
376
|
+
applyHeight(height) {
|
|
377
|
+
const resolved = this.normalizeHeight(height);
|
|
378
|
+
this.wrapper.style.setProperty("--thek-input-height", resolved);
|
|
379
|
+
this.dropdown.style.setProperty("--thek-input-height", resolved);
|
|
380
|
+
}
|
|
381
|
+
createDom() {
|
|
382
|
+
this.wrapper = document.createElement("div");
|
|
383
|
+
this.wrapper.className = "thek-select";
|
|
384
|
+
if (this.config.disabled) this.wrapper.classList.add("thek-disabled");
|
|
385
|
+
if (this.config.multiple) this.wrapper.classList.add("thek-multiple");
|
|
386
|
+
this.control = document.createElement("div");
|
|
387
|
+
this.control.className = "thek-control";
|
|
388
|
+
this.control.setAttribute("role", "combobox");
|
|
389
|
+
this.control.setAttribute("aria-expanded", "false");
|
|
390
|
+
this.control.setAttribute("aria-haspopup", "listbox");
|
|
391
|
+
this.control.setAttribute("aria-controls", `${this.id}-list`);
|
|
392
|
+
this.selectionContainer = document.createElement("div");
|
|
393
|
+
this.selectionContainer.className = "thek-selection";
|
|
394
|
+
this.placeholderElement = document.createElement("span");
|
|
395
|
+
this.placeholderElement.className = "thek-placeholder";
|
|
396
|
+
this.placeholderElement.textContent = this.config.placeholder;
|
|
397
|
+
this.indicatorsContainer = document.createElement("div");
|
|
398
|
+
this.indicatorsContainer.className = "thek-indicators";
|
|
399
|
+
this.indicatorsContainer.innerHTML = "<i class=\"fa-solid fa-chevron-down thek-arrow\"></i>";
|
|
400
|
+
this.control.appendChild(this.selectionContainer);
|
|
401
|
+
this.control.appendChild(this.placeholderElement);
|
|
402
|
+
this.control.appendChild(this.indicatorsContainer);
|
|
403
|
+
this.dropdown = document.createElement("div");
|
|
404
|
+
this.dropdown.className = "thek-dropdown";
|
|
405
|
+
this.dropdown.hidden = true;
|
|
406
|
+
if (this.config.searchable) {
|
|
407
|
+
const searchWrapper = document.createElement("div");
|
|
408
|
+
searchWrapper.className = "thek-search-wrapper";
|
|
409
|
+
searchWrapper.innerHTML = "<i class=\"fa-solid fa-magnifying-glass thek-search-icon\"></i>";
|
|
410
|
+
this.input = document.createElement("input");
|
|
411
|
+
this.input.className = "thek-input";
|
|
412
|
+
this.input.type = "text";
|
|
413
|
+
this.input.autocomplete = "off";
|
|
414
|
+
this.input.placeholder = "Search...";
|
|
415
|
+
this.input.setAttribute("aria-autocomplete", "list");
|
|
416
|
+
searchWrapper.appendChild(this.input);
|
|
417
|
+
this.dropdown.appendChild(searchWrapper);
|
|
418
|
+
} else {
|
|
419
|
+
this.input = document.createElement("input");
|
|
420
|
+
this.input.type = "hidden";
|
|
421
|
+
}
|
|
422
|
+
this.optionsList = document.createElement("ul");
|
|
423
|
+
this.optionsList.className = "thek-options";
|
|
424
|
+
this.optionsList.id = `${this.id}-list`;
|
|
425
|
+
this.optionsList.setAttribute("role", "listbox");
|
|
426
|
+
this.optionsList.addEventListener("scroll", () => this.handleOptionsScroll());
|
|
427
|
+
this.optionsList.addEventListener("wheel", (e) => this.handleOptionsWheel(e), { passive: false });
|
|
428
|
+
this.dropdown.appendChild(this.optionsList);
|
|
429
|
+
this.wrapper.appendChild(this.control);
|
|
430
|
+
document.body.appendChild(this.dropdown);
|
|
431
|
+
this.applyHeight(this.config.height);
|
|
432
|
+
}
|
|
433
|
+
render(state, filteredOptions) {
|
|
434
|
+
this.lastState = state;
|
|
435
|
+
this.lastFilteredOptions = filteredOptions;
|
|
436
|
+
this.control.setAttribute("aria-expanded", state.isOpen.toString());
|
|
437
|
+
this.dropdown.hidden = !state.isOpen;
|
|
438
|
+
this.wrapper.classList.toggle("thek-open", state.isOpen);
|
|
439
|
+
if (state.isLoading) this.indicatorsContainer.innerHTML = "<i class=\"fa-solid fa-circle-notch fa-spin text-muted\"></i>";
|
|
440
|
+
else this.indicatorsContainer.innerHTML = "<i class=\"fa-solid fa-chevron-down thek-arrow\"></i>";
|
|
441
|
+
this.renderSelectionContent(state);
|
|
442
|
+
this.renderOptionsContent(state, filteredOptions);
|
|
443
|
+
if (state.isOpen) this.positionDropdown();
|
|
444
|
+
}
|
|
445
|
+
renderSelectionContent(state) {
|
|
446
|
+
this.selectionContainer.innerHTML = "";
|
|
447
|
+
const hasSelection = state.selectedValues.length > 0;
|
|
448
|
+
this.placeholderElement.style.display = hasSelection ? "none" : "block";
|
|
449
|
+
this.selectionContainer.style.display = hasSelection ? "flex" : "none";
|
|
450
|
+
if (hasSelection) {
|
|
451
|
+
const vField = this.config.valueField;
|
|
452
|
+
const dField = this.config.displayField;
|
|
453
|
+
if (this.config.multiple) if (state.selectedValues.length > this.config.maxSelectedLabels) {
|
|
454
|
+
const summary = document.createElement("span");
|
|
455
|
+
summary.className = "thek-summary-text";
|
|
456
|
+
summary.textContent = `${state.selectedValues.length} items selected`;
|
|
457
|
+
this.selectionContainer.appendChild(summary);
|
|
458
|
+
} else state.selectedValues.forEach((val, i) => {
|
|
459
|
+
const option = state.options.find((o) => o[vField] === val) || state.selectedOptionsByValue[val] || {
|
|
460
|
+
[vField]: val,
|
|
461
|
+
[dField]: val
|
|
462
|
+
};
|
|
463
|
+
const tag = document.createElement("span");
|
|
464
|
+
tag.className = "thek-tag";
|
|
465
|
+
tag.draggable = true;
|
|
466
|
+
tag.dataset.index = i.toString();
|
|
467
|
+
tag.dataset.value = val;
|
|
468
|
+
const label = document.createElement("span");
|
|
469
|
+
label.className = "thek-tag-label";
|
|
470
|
+
const content = this.config.renderSelection(option);
|
|
471
|
+
if (content instanceof HTMLElement) label.appendChild(content);
|
|
472
|
+
else label.textContent = content;
|
|
473
|
+
tag.appendChild(label);
|
|
474
|
+
const removeBtn = document.createElement("span");
|
|
475
|
+
removeBtn.className = "thek-tag-remove";
|
|
476
|
+
removeBtn.innerHTML = "×";
|
|
477
|
+
removeBtn.addEventListener("click", (e) => {
|
|
478
|
+
e.stopPropagation();
|
|
479
|
+
this.callbacks.onSelect(option);
|
|
480
|
+
});
|
|
481
|
+
tag.appendChild(removeBtn);
|
|
482
|
+
this.setupTagDnd(tag);
|
|
483
|
+
this.selectionContainer.appendChild(tag);
|
|
484
|
+
});
|
|
485
|
+
else {
|
|
486
|
+
const val = state.selectedValues[0];
|
|
487
|
+
const option = state.options.find((o) => o[vField] === val) || state.selectedOptionsByValue[val];
|
|
488
|
+
if (option) {
|
|
489
|
+
const content = this.config.renderSelection(option);
|
|
490
|
+
if (content instanceof HTMLElement) this.selectionContainer.appendChild(content);
|
|
491
|
+
else this.selectionContainer.textContent = content;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
renderOptionsContent(state, filteredOptions, alignFocused = true, preservedScrollTop) {
|
|
497
|
+
this.optionsList.innerHTML = "";
|
|
498
|
+
const vField = this.config.valueField;
|
|
499
|
+
const dField = this.config.displayField;
|
|
500
|
+
if (state.isLoading && filteredOptions.length === 0) {
|
|
501
|
+
const li = document.createElement("li");
|
|
502
|
+
li.className = "thek-option thek-loading";
|
|
503
|
+
li.textContent = "Loading...";
|
|
504
|
+
this.optionsList.appendChild(li);
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
const canCreate = this.config.canCreate && state.inputValue && !filteredOptions.some((o) => o[dField] && o[dField].toString().toLowerCase() === state.inputValue.toLowerCase());
|
|
508
|
+
const shouldVirtualize = this.config.virtualize && filteredOptions.length >= this.config.virtualThreshold && !canCreate;
|
|
509
|
+
const itemHeight = Math.max(20, this.config.virtualItemHeight);
|
|
510
|
+
const overscan = Math.max(0, this.config.virtualOverscan);
|
|
511
|
+
if (shouldVirtualize) {
|
|
512
|
+
const viewportHeight = this.optionsList.clientHeight || 240;
|
|
513
|
+
if (alignFocused && state.focusedIndex >= 0 && state.focusedIndex < filteredOptions.length) {
|
|
514
|
+
const focusedTop = state.focusedIndex * itemHeight;
|
|
515
|
+
const focusedBottom = focusedTop + itemHeight;
|
|
516
|
+
const currentTop = this.optionsList.scrollTop;
|
|
517
|
+
const currentBottom = currentTop + viewportHeight;
|
|
518
|
+
if (focusedTop < currentTop) this.optionsList.scrollTop = focusedTop;
|
|
519
|
+
else if (focusedBottom > currentBottom) this.optionsList.scrollTop = focusedBottom - viewportHeight;
|
|
520
|
+
}
|
|
521
|
+
const scrollTop = preservedScrollTop ?? this.optionsList.scrollTop;
|
|
522
|
+
const start = Math.max(0, Math.floor(scrollTop / itemHeight) - overscan);
|
|
523
|
+
const end = Math.min(filteredOptions.length, Math.ceil((scrollTop + viewportHeight) / itemHeight) + overscan);
|
|
524
|
+
if (start > 0) this.optionsList.appendChild(this.createSpacer(start * itemHeight));
|
|
525
|
+
for (let index = start; index < end; index++) this.optionsList.appendChild(this.createOptionItem(filteredOptions[index], index, state, vField));
|
|
526
|
+
if (end < filteredOptions.length) this.optionsList.appendChild(this.createSpacer((filteredOptions.length - end) * itemHeight));
|
|
527
|
+
if (typeof preservedScrollTop === "number") this.optionsList.scrollTop = preservedScrollTop;
|
|
528
|
+
} else filteredOptions.forEach((option, index) => {
|
|
529
|
+
this.optionsList.appendChild(this.createOptionItem(option, index, state, vField));
|
|
530
|
+
});
|
|
531
|
+
const exactMatch = filteredOptions.some((o) => o[dField] && o[dField].toString().toLowerCase() === state.inputValue.toLowerCase());
|
|
532
|
+
if (this.config.canCreate && state.inputValue && !exactMatch) {
|
|
533
|
+
const li = document.createElement("li");
|
|
534
|
+
li.className = "thek-option thek-create";
|
|
535
|
+
li.textContent = this.config.createText.replace("{%t}", state.inputValue);
|
|
536
|
+
if (state.focusedIndex === filteredOptions.length) li.classList.add("thek-focused");
|
|
537
|
+
li.addEventListener("click", (e) => {
|
|
538
|
+
e.stopPropagation();
|
|
539
|
+
this.callbacks.onCreate(state.inputValue);
|
|
540
|
+
});
|
|
541
|
+
this.optionsList.appendChild(li);
|
|
542
|
+
}
|
|
543
|
+
if (filteredOptions.length === 0 && (!this.config.canCreate || !state.inputValue)) {
|
|
544
|
+
const li = document.createElement("li");
|
|
545
|
+
li.className = "thek-option thek-no-results";
|
|
546
|
+
li.textContent = "No results found";
|
|
547
|
+
this.optionsList.appendChild(li);
|
|
548
|
+
}
|
|
549
|
+
const activeDescendantId = state.focusedIndex >= 0 && state.focusedIndex < filteredOptions.length && !!document.getElementById(`${this.id}-opt-${state.focusedIndex}`) ? `${this.id}-opt-${state.focusedIndex}` : null;
|
|
550
|
+
if (this.config.searchable) if (activeDescendantId) this.input.setAttribute("aria-activedescendant", activeDescendantId);
|
|
551
|
+
else this.input.removeAttribute("aria-activedescendant");
|
|
552
|
+
}
|
|
553
|
+
createSpacer(height) {
|
|
554
|
+
const spacer = document.createElement("li");
|
|
555
|
+
spacer.style.height = `${height}px`;
|
|
556
|
+
spacer.style.padding = "0";
|
|
557
|
+
spacer.style.margin = "0";
|
|
558
|
+
spacer.style.listStyle = "none";
|
|
559
|
+
spacer.setAttribute("aria-hidden", "true");
|
|
560
|
+
return spacer;
|
|
561
|
+
}
|
|
562
|
+
createOptionItem(option, index, state, valueField) {
|
|
563
|
+
const li = document.createElement("li");
|
|
564
|
+
li.className = "thek-option";
|
|
565
|
+
li.id = `${this.id}-opt-${index}`;
|
|
566
|
+
const isSelected = state.selectedValues.includes(option[valueField]);
|
|
567
|
+
if (option.disabled) li.classList.add("thek-disabled");
|
|
568
|
+
if (isSelected) li.classList.add("thek-selected");
|
|
569
|
+
if (state.focusedIndex === index) li.classList.add("thek-focused");
|
|
570
|
+
li.setAttribute("role", "option");
|
|
571
|
+
li.setAttribute("aria-selected", isSelected.toString());
|
|
572
|
+
if (this.config.multiple) {
|
|
573
|
+
const checkbox = document.createElement("div");
|
|
574
|
+
checkbox.className = "thek-checkbox";
|
|
575
|
+
if (isSelected) checkbox.innerHTML = "<i class=\"fa-solid fa-check\"></i>";
|
|
576
|
+
li.appendChild(checkbox);
|
|
577
|
+
}
|
|
578
|
+
const label = document.createElement("span");
|
|
579
|
+
label.className = "thek-option-label";
|
|
580
|
+
const content = this.config.renderOption(option);
|
|
581
|
+
if (content instanceof HTMLElement) label.appendChild(content);
|
|
582
|
+
else label.textContent = content;
|
|
583
|
+
li.appendChild(label);
|
|
584
|
+
li.addEventListener("click", (e) => {
|
|
585
|
+
e.stopPropagation();
|
|
586
|
+
this.callbacks.onSelect(option);
|
|
587
|
+
});
|
|
588
|
+
return li;
|
|
589
|
+
}
|
|
590
|
+
handleOptionsScroll() {
|
|
591
|
+
if (!this.config.virtualize || !this.lastState) return;
|
|
592
|
+
const scrollTop = this.optionsList.scrollTop;
|
|
593
|
+
this.renderOptionsContent(this.lastState, this.lastFilteredOptions, false, scrollTop);
|
|
594
|
+
}
|
|
595
|
+
handleOptionsWheel(e) {
|
|
596
|
+
if (!this.config.virtualize) return;
|
|
597
|
+
const list = this.optionsList;
|
|
598
|
+
const atTop = list.scrollTop <= 0;
|
|
599
|
+
const atBottom = list.scrollTop + list.clientHeight >= list.scrollHeight - 1;
|
|
600
|
+
const scrollingUp = e.deltaY < 0;
|
|
601
|
+
const scrollingDown = e.deltaY > 0;
|
|
602
|
+
if (scrollingUp && !atTop || scrollingDown && !atBottom) {
|
|
603
|
+
e.preventDefault();
|
|
604
|
+
list.scrollTop += e.deltaY;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
positionDropdown() {
|
|
608
|
+
const rect = this.control.getBoundingClientRect();
|
|
609
|
+
const viewportWidth = document.documentElement.clientWidth;
|
|
610
|
+
const scrollX = window.scrollX;
|
|
611
|
+
const scrollY = window.scrollY;
|
|
612
|
+
this.dropdown.style.position = "absolute";
|
|
613
|
+
this.dropdown.style.zIndex = "9999";
|
|
614
|
+
let width = rect.width;
|
|
615
|
+
if (width > viewportWidth - 20) width = viewportWidth - 20;
|
|
616
|
+
this.dropdown.style.width = `${width}px`;
|
|
617
|
+
let left = rect.left + scrollX;
|
|
618
|
+
if (rect.left + width > viewportWidth) left = viewportWidth - width - 10 + scrollX;
|
|
619
|
+
if (left < scrollX + 10) left = scrollX + 10;
|
|
620
|
+
this.dropdown.style.left = `${left}px`;
|
|
621
|
+
this.dropdown.style.top = `${rect.bottom + scrollY}px`;
|
|
622
|
+
}
|
|
623
|
+
setupTagDnd(tag) {
|
|
624
|
+
tag.addEventListener("dragstart", (e) => {
|
|
625
|
+
e.dataTransfer?.setData("text/plain", tag.dataset.index);
|
|
626
|
+
tag.classList.add("thek-dragging");
|
|
627
|
+
});
|
|
628
|
+
tag.addEventListener("dragend", () => {
|
|
629
|
+
tag.classList.remove("thek-dragging");
|
|
630
|
+
});
|
|
631
|
+
tag.addEventListener("dragover", (e) => {
|
|
632
|
+
e.preventDefault();
|
|
633
|
+
tag.classList.add("thek-drag-over");
|
|
634
|
+
});
|
|
635
|
+
tag.addEventListener("dragleave", () => {
|
|
636
|
+
tag.classList.remove("thek-drag-over");
|
|
637
|
+
});
|
|
638
|
+
tag.addEventListener("drop", (e) => {
|
|
639
|
+
e.preventDefault();
|
|
640
|
+
tag.classList.remove("thek-drag-over");
|
|
641
|
+
const fromIndex = parseInt(e.dataTransfer?.getData("text/plain") || "-1");
|
|
642
|
+
const toIndex = parseInt(tag.dataset.index);
|
|
643
|
+
if (fromIndex !== -1 && fromIndex !== toIndex) this.callbacks.onReorder(fromIndex, toIndex);
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
setHeight(height) {
|
|
647
|
+
this.config.height = height;
|
|
648
|
+
this.applyHeight(height);
|
|
649
|
+
}
|
|
650
|
+
updateConfig(newConfig) {
|
|
651
|
+
this.config = {
|
|
652
|
+
...this.config,
|
|
653
|
+
...newConfig
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
destroy() {
|
|
657
|
+
if (this.wrapper.parentNode) this.wrapper.parentNode.removeChild(this.wrapper);
|
|
658
|
+
if (this.dropdown.parentNode) this.dropdown.parentNode.removeChild(this.dropdown);
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
//#endregion
|
|
662
|
+
//#region src/core/config-utils.ts
|
|
663
|
+
var NOOP_LOAD_OPTIONS = async (_query) => [];
|
|
664
|
+
function parseSelectOptions(select) {
|
|
665
|
+
return Array.from(select.options).map((opt) => ({
|
|
666
|
+
value: opt.value,
|
|
667
|
+
label: opt.text,
|
|
668
|
+
disabled: opt.disabled,
|
|
669
|
+
selected: opt.selected
|
|
670
|
+
}));
|
|
671
|
+
}
|
|
672
|
+
function buildConfig(element, config, globalDefaults = {}) {
|
|
673
|
+
const isSelect = element instanceof HTMLSelectElement;
|
|
674
|
+
const finalConfig = {
|
|
675
|
+
options: isSelect ? parseSelectOptions(element) : config.options || [],
|
|
676
|
+
multiple: isSelect ? element.multiple : false,
|
|
677
|
+
searchable: true,
|
|
678
|
+
disabled: isSelect ? element.disabled : false,
|
|
679
|
+
placeholder: "Select...",
|
|
680
|
+
canCreate: false,
|
|
681
|
+
createText: "Create '{%t}'...",
|
|
682
|
+
height: 40,
|
|
683
|
+
debounce: 300,
|
|
684
|
+
maxSelectedLabels: 3,
|
|
685
|
+
displayField: "label",
|
|
686
|
+
valueField: "value",
|
|
687
|
+
maxOptions: null,
|
|
688
|
+
virtualize: false,
|
|
689
|
+
virtualItemHeight: 40,
|
|
690
|
+
virtualOverscan: 4,
|
|
691
|
+
virtualThreshold: 80,
|
|
692
|
+
loadOptions: NOOP_LOAD_OPTIONS,
|
|
693
|
+
renderOption: (o) => o.label,
|
|
694
|
+
renderSelection: (o) => o.label,
|
|
695
|
+
...globalDefaults,
|
|
696
|
+
...config
|
|
697
|
+
};
|
|
698
|
+
const hasCustomRenderOption = !!(globalDefaults.renderOption || config.renderOption);
|
|
699
|
+
const hasCustomRenderSelection = !!(globalDefaults.renderSelection || config.renderSelection);
|
|
700
|
+
if (!hasCustomRenderOption) finalConfig.renderOption = (o) => o[finalConfig.displayField];
|
|
701
|
+
if (!hasCustomRenderSelection) finalConfig.renderSelection = (o) => o[finalConfig.displayField];
|
|
702
|
+
return finalConfig;
|
|
703
|
+
}
|
|
704
|
+
function buildInitialState(config) {
|
|
705
|
+
const valueField = config.valueField;
|
|
706
|
+
const firstSelected = config.options.find((o) => o.selected);
|
|
707
|
+
const selectedValues = config.multiple ? config.options.filter((o) => o.selected).map((o) => o[valueField]) : firstSelected && valueField in firstSelected ? [firstSelected[valueField]] : [];
|
|
708
|
+
const selectedOptionsByValue = {};
|
|
709
|
+
selectedValues.forEach((value) => {
|
|
710
|
+
const option = config.options.find((o) => o[valueField] === value);
|
|
711
|
+
if (option) selectedOptionsByValue[value] = option;
|
|
712
|
+
});
|
|
713
|
+
return {
|
|
714
|
+
options: config.options,
|
|
715
|
+
selectedValues,
|
|
716
|
+
selectedOptionsByValue,
|
|
717
|
+
isOpen: false,
|
|
718
|
+
focusedIndex: -1,
|
|
719
|
+
inputValue: "",
|
|
720
|
+
isLoading: false
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
//#endregion
|
|
724
|
+
//#region src/core/options-logic.ts
|
|
725
|
+
function isRemoteMode(config) {
|
|
726
|
+
return config.loadOptions !== NOOP_LOAD_OPTIONS;
|
|
727
|
+
}
|
|
728
|
+
function getFilteredOptions(config, state) {
|
|
729
|
+
if (isRemoteMode(config) && state.inputValue) return state.options;
|
|
730
|
+
const query = state.inputValue.toLowerCase();
|
|
731
|
+
const displayField = config.displayField;
|
|
732
|
+
const filtered = state.options.filter((option) => {
|
|
733
|
+
const value = option[displayField];
|
|
734
|
+
return value != null && value.toString().toLowerCase().includes(query);
|
|
735
|
+
});
|
|
736
|
+
if (config.maxOptions != null) {
|
|
737
|
+
const limit = Math.max(0, config.maxOptions);
|
|
738
|
+
return filtered.slice(0, limit);
|
|
739
|
+
}
|
|
740
|
+
return filtered;
|
|
741
|
+
}
|
|
742
|
+
function mergeSelectedOptionsByValue(valueField, selectedValues, previous, latestOptions) {
|
|
743
|
+
const byValueFromLatest = {};
|
|
744
|
+
latestOptions.forEach((option) => {
|
|
745
|
+
const value = option[valueField];
|
|
746
|
+
if (typeof value === "string") byValueFromLatest[value] = option;
|
|
747
|
+
});
|
|
748
|
+
const merged = {};
|
|
749
|
+
selectedValues.forEach((value) => {
|
|
750
|
+
const option = byValueFromLatest[value] || previous[value];
|
|
751
|
+
if (option) merged[value] = option;
|
|
752
|
+
});
|
|
753
|
+
return merged;
|
|
754
|
+
}
|
|
755
|
+
//#endregion
|
|
756
|
+
//#region src/core/selection-logic.ts
|
|
757
|
+
function applySelection(config, state, option) {
|
|
758
|
+
const valueField = config.valueField;
|
|
759
|
+
const optionValue = String(option[valueField]);
|
|
760
|
+
const selectedOptionsByValue = { ...state.selectedOptionsByValue };
|
|
761
|
+
if (config.multiple) {
|
|
762
|
+
if (state.selectedValues.includes(optionValue)) {
|
|
763
|
+
const selectedValues = state.selectedValues.filter((v) => v !== optionValue);
|
|
764
|
+
delete selectedOptionsByValue[optionValue];
|
|
765
|
+
return {
|
|
766
|
+
selectedValues,
|
|
767
|
+
selectedOptionsByValue,
|
|
768
|
+
inputValue: "",
|
|
769
|
+
tagEvent: "tagRemoved",
|
|
770
|
+
tagOption: option
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
const selectedValues = [...state.selectedValues, optionValue];
|
|
774
|
+
selectedOptionsByValue[optionValue] = option;
|
|
775
|
+
return {
|
|
776
|
+
selectedValues,
|
|
777
|
+
selectedOptionsByValue,
|
|
778
|
+
inputValue: "",
|
|
779
|
+
tagEvent: "tagAdded",
|
|
780
|
+
tagOption: option
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
return {
|
|
784
|
+
selectedValues: [optionValue],
|
|
785
|
+
selectedOptionsByValue: { [optionValue]: option },
|
|
786
|
+
inputValue: ""
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
function createOptionFromLabel(config, label) {
|
|
790
|
+
const value = label;
|
|
791
|
+
return {
|
|
792
|
+
value,
|
|
793
|
+
label,
|
|
794
|
+
[config.valueField]: value,
|
|
795
|
+
[config.displayField]: label
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
function removeLastSelection(config, state) {
|
|
799
|
+
const valueField = config.valueField;
|
|
800
|
+
const selectedValues = [...state.selectedValues];
|
|
801
|
+
const selectedOptionsByValue = { ...state.selectedOptionsByValue };
|
|
802
|
+
const removedValue = selectedValues.pop();
|
|
803
|
+
if (!removedValue) return {
|
|
804
|
+
selectedValues,
|
|
805
|
+
selectedOptionsByValue
|
|
806
|
+
};
|
|
807
|
+
const removedOption = state.options.find((o) => o[valueField] === removedValue) || selectedOptionsByValue[removedValue];
|
|
808
|
+
delete selectedOptionsByValue[removedValue];
|
|
809
|
+
return {
|
|
810
|
+
selectedValues,
|
|
811
|
+
selectedOptionsByValue,
|
|
812
|
+
removedOption
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
function reorderSelectedValues(state, from, to) {
|
|
816
|
+
if (!Number.isInteger(from) || !Number.isInteger(to)) return [...state.selectedValues];
|
|
817
|
+
if (from < 0 || to < 0 || from >= state.selectedValues.length || to >= state.selectedValues.length || from === to) return [...state.selectedValues];
|
|
818
|
+
const selectedValues = [...state.selectedValues];
|
|
819
|
+
const [movedItem] = selectedValues.splice(from, 1);
|
|
820
|
+
if (typeof movedItem === "undefined") return [...state.selectedValues];
|
|
821
|
+
selectedValues.splice(to, 0, movedItem);
|
|
822
|
+
return selectedValues;
|
|
823
|
+
}
|
|
824
|
+
function resolveSelectedOptions(config, state) {
|
|
825
|
+
const valueField = config.valueField;
|
|
826
|
+
const displayField = config.displayField;
|
|
827
|
+
return state.selectedValues.map((value) => state.options.find((o) => o[valueField] === value) || state.selectedOptionsByValue[value] || {
|
|
828
|
+
value,
|
|
829
|
+
label: value,
|
|
830
|
+
[valueField]: value,
|
|
831
|
+
[displayField]: value
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
function buildSelectedOptionsMapFromValues(config, state, values) {
|
|
835
|
+
const valueField = config.valueField;
|
|
836
|
+
const selectedOptionsByValue = {};
|
|
837
|
+
values.forEach((value) => {
|
|
838
|
+
const option = state.options.find((o) => o[valueField] === value) || state.selectedOptionsByValue[value];
|
|
839
|
+
if (option) selectedOptionsByValue[value] = option;
|
|
840
|
+
});
|
|
841
|
+
return selectedOptionsByValue;
|
|
842
|
+
}
|
|
843
|
+
//#endregion
|
|
844
|
+
//#region src/core/event-emitter.ts
|
|
845
|
+
var ThekSelectEventEmitter = class {
|
|
846
|
+
listeners = /* @__PURE__ */ new Map();
|
|
847
|
+
on(event, callback) {
|
|
848
|
+
if (!this.listeners.has(event)) this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
849
|
+
this.listeners.get(event).add(callback);
|
|
850
|
+
return () => this.off(event, callback);
|
|
851
|
+
}
|
|
852
|
+
off(event, callback) {
|
|
853
|
+
const callbacks = this.listeners.get(event);
|
|
854
|
+
if (!callbacks) return;
|
|
855
|
+
callbacks.delete(callback);
|
|
856
|
+
if (callbacks.size === 0) this.listeners.delete(event);
|
|
857
|
+
}
|
|
858
|
+
emit(event, data) {
|
|
859
|
+
if (!this.listeners.has(event)) return;
|
|
860
|
+
this.listeners.get(event).forEach((listener) => listener(data));
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
var globalEventManager = class GlobalEventManager {
|
|
864
|
+
static instance;
|
|
865
|
+
resizeListeners = /* @__PURE__ */ new Set();
|
|
866
|
+
scrollListeners = /* @__PURE__ */ new Set();
|
|
867
|
+
clickListeners = /* @__PURE__ */ new Set();
|
|
868
|
+
constructor() {
|
|
869
|
+
if (typeof window !== "undefined") {
|
|
870
|
+
window.addEventListener("resize", (e) => this.notify(this.resizeListeners, e));
|
|
871
|
+
window.addEventListener("scroll", (e) => this.notify(this.scrollListeners, e), true);
|
|
872
|
+
document.addEventListener("click", (e) => this.notify(this.clickListeners, e));
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
static getInstance() {
|
|
876
|
+
if (!GlobalEventManager.instance) GlobalEventManager.instance = new GlobalEventManager();
|
|
877
|
+
return GlobalEventManager.instance;
|
|
878
|
+
}
|
|
879
|
+
notify(listeners, event) {
|
|
880
|
+
listeners.forEach((callback) => callback(event));
|
|
881
|
+
}
|
|
882
|
+
onResize(callback) {
|
|
883
|
+
this.resizeListeners.add(callback);
|
|
884
|
+
return () => this.resizeListeners.delete(callback);
|
|
885
|
+
}
|
|
886
|
+
onScroll(callback) {
|
|
887
|
+
this.scrollListeners.add(callback);
|
|
888
|
+
return () => this.scrollListeners.delete(callback);
|
|
889
|
+
}
|
|
890
|
+
onClick(callback) {
|
|
891
|
+
this.clickListeners.add(callback);
|
|
892
|
+
return () => this.clickListeners.delete(callback);
|
|
893
|
+
}
|
|
894
|
+
}.getInstance();
|
|
895
|
+
//#endregion
|
|
896
|
+
//#region src/core/thekselect.ts
|
|
897
|
+
var ThekSelect = class ThekSelect {
|
|
898
|
+
static globalDefaults = {};
|
|
899
|
+
config;
|
|
900
|
+
stateManager;
|
|
901
|
+
renderer;
|
|
902
|
+
id;
|
|
903
|
+
events = new ThekSelectEventEmitter();
|
|
904
|
+
originalElement;
|
|
905
|
+
unsubscribeEvents = [];
|
|
906
|
+
unsubscribeState;
|
|
907
|
+
remoteRequestId = 0;
|
|
908
|
+
isDestroyed = false;
|
|
909
|
+
focusTimeoutId = null;
|
|
910
|
+
constructor(element, config = {}) {
|
|
911
|
+
injectStyles();
|
|
912
|
+
const el = typeof element === "string" ? document.querySelector(element) : element;
|
|
913
|
+
if (!el) throw new Error("Element not found");
|
|
914
|
+
this.originalElement = el;
|
|
915
|
+
this.id = generateId();
|
|
916
|
+
this.config = buildConfig(this.originalElement, config, ThekSelect.globalDefaults);
|
|
917
|
+
this.stateManager = new StateManager(buildInitialState(this.config));
|
|
918
|
+
this.renderer = new DomRenderer(this.config, this.id, {
|
|
919
|
+
onSelect: (option) => this.handleSelect(option),
|
|
920
|
+
onCreate: (label) => this.handleCreate(label),
|
|
921
|
+
onRemove: (option) => this.handleSelect(option),
|
|
922
|
+
onReorder: (from, to) => this.handleReorder(from, to)
|
|
923
|
+
});
|
|
924
|
+
this.setupHandleSearch();
|
|
925
|
+
this.initialize();
|
|
926
|
+
}
|
|
927
|
+
static init(element, config = {}) {
|
|
928
|
+
return new ThekSelect(element, config);
|
|
929
|
+
}
|
|
930
|
+
static setDefaults(defaults) {
|
|
931
|
+
ThekSelect.globalDefaults = {
|
|
932
|
+
...ThekSelect.globalDefaults,
|
|
933
|
+
...defaults
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
static resetDefaults() {
|
|
937
|
+
ThekSelect.globalDefaults = {};
|
|
938
|
+
}
|
|
939
|
+
initialize() {
|
|
940
|
+
this.renderer.createDom();
|
|
941
|
+
this.setupListeners();
|
|
942
|
+
this.unsubscribeState = this.stateManager.subscribe(() => this.render());
|
|
943
|
+
this.render();
|
|
944
|
+
if (this.originalElement.parentNode) {
|
|
945
|
+
this.originalElement.style.display = "none";
|
|
946
|
+
this.originalElement.parentNode.insertBefore(this.renderer.wrapper, this.originalElement.nextSibling);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
setupListeners() {
|
|
950
|
+
this.renderer.control.addEventListener("click", () => {
|
|
951
|
+
if (this.config.disabled) return;
|
|
952
|
+
this.toggleDropdown();
|
|
953
|
+
});
|
|
954
|
+
if (this.config.searchable) this.renderer.input.addEventListener("input", (e) => {
|
|
955
|
+
const value = e.target.value;
|
|
956
|
+
this.stateManager.setState({ inputValue: value });
|
|
957
|
+
this.handleSearch(value);
|
|
958
|
+
});
|
|
959
|
+
this.renderer.input.addEventListener("keydown", (e) => this.handleKeyDown(e));
|
|
960
|
+
this.unsubscribeEvents.push(globalEventManager.onClick((e) => {
|
|
961
|
+
const event = e;
|
|
962
|
+
if (!this.renderer.wrapper.contains(event.target) && !this.renderer.dropdown.contains(event.target)) this.closeDropdown();
|
|
963
|
+
}));
|
|
964
|
+
this.unsubscribeEvents.push(globalEventManager.onResize(() => this.renderer.positionDropdown()));
|
|
965
|
+
this.unsubscribeEvents.push(globalEventManager.onScroll(() => this.renderer.positionDropdown()));
|
|
966
|
+
}
|
|
967
|
+
handleSearch;
|
|
968
|
+
setupHandleSearch() {
|
|
969
|
+
this.handleSearch = debounce(async (query) => {
|
|
970
|
+
this.emit("search", query);
|
|
971
|
+
if (isRemoteMode(this.config)) if (query.length > 0) {
|
|
972
|
+
const requestId = ++this.remoteRequestId;
|
|
973
|
+
this.stateManager.setState({ isLoading: true });
|
|
974
|
+
try {
|
|
975
|
+
const options = await this.config.loadOptions(query);
|
|
976
|
+
if (this.isDestroyed || requestId !== this.remoteRequestId) return;
|
|
977
|
+
const state = this.stateManager.getState();
|
|
978
|
+
this.stateManager.setState({
|
|
979
|
+
options,
|
|
980
|
+
isLoading: false,
|
|
981
|
+
focusedIndex: 0,
|
|
982
|
+
selectedOptionsByValue: mergeSelectedOptionsByValue(this.config.valueField, state.selectedValues, state.selectedOptionsByValue, options)
|
|
983
|
+
});
|
|
984
|
+
} catch {
|
|
985
|
+
if (this.isDestroyed || requestId !== this.remoteRequestId) return;
|
|
986
|
+
this.stateManager.setState({ isLoading: false });
|
|
987
|
+
}
|
|
988
|
+
} else {
|
|
989
|
+
this.remoteRequestId++;
|
|
990
|
+
this.stateManager.setState({
|
|
991
|
+
options: this.config.options,
|
|
992
|
+
focusedIndex: -1,
|
|
993
|
+
isLoading: false
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
else this.stateManager.setState({ focusedIndex: 0 });
|
|
997
|
+
}, this.config.debounce);
|
|
998
|
+
}
|
|
999
|
+
handleKeyDown(e) {
|
|
1000
|
+
const state = this.stateManager.getState();
|
|
1001
|
+
const filteredOptions = getFilteredOptions(this.config, state);
|
|
1002
|
+
const displayField = this.config.displayField;
|
|
1003
|
+
switch (e.key) {
|
|
1004
|
+
case "ArrowDown":
|
|
1005
|
+
e.preventDefault();
|
|
1006
|
+
this.openDropdown();
|
|
1007
|
+
const maxIndex = this.config.canCreate && state.inputValue && !filteredOptions.some((o) => o[displayField].toLowerCase() === state.inputValue.toLowerCase()) ? filteredOptions.length : filteredOptions.length - 1;
|
|
1008
|
+
this.stateManager.setState({ focusedIndex: Math.min(state.focusedIndex + 1, maxIndex) });
|
|
1009
|
+
break;
|
|
1010
|
+
case "ArrowUp":
|
|
1011
|
+
e.preventDefault();
|
|
1012
|
+
this.stateManager.setState({ focusedIndex: Math.max(state.focusedIndex - 1, 0) });
|
|
1013
|
+
break;
|
|
1014
|
+
case "Enter":
|
|
1015
|
+
e.preventDefault();
|
|
1016
|
+
if (state.focusedIndex >= 0 && state.focusedIndex < filteredOptions.length) this.handleSelect(filteredOptions[state.focusedIndex]);
|
|
1017
|
+
else if (this.config.canCreate && state.inputValue && state.focusedIndex === filteredOptions.length) this.handleCreate(state.inputValue);
|
|
1018
|
+
break;
|
|
1019
|
+
case "Escape":
|
|
1020
|
+
this.closeDropdown();
|
|
1021
|
+
break;
|
|
1022
|
+
case "Backspace":
|
|
1023
|
+
if (state.inputValue === "" && this.config.multiple && state.selectedValues.length > 0) this.handleRemoveLastSelection();
|
|
1024
|
+
break;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
toggleDropdown() {
|
|
1028
|
+
if (this.stateManager.getState().isOpen) this.closeDropdown();
|
|
1029
|
+
else this.openDropdown();
|
|
1030
|
+
}
|
|
1031
|
+
openDropdown() {
|
|
1032
|
+
if (this.stateManager.getState().isOpen) return;
|
|
1033
|
+
this.stateManager.setState({
|
|
1034
|
+
isOpen: true,
|
|
1035
|
+
focusedIndex: 0
|
|
1036
|
+
});
|
|
1037
|
+
this.renderer.positionDropdown();
|
|
1038
|
+
if (this.config.searchable) this.focusTimeoutId = setTimeout(() => {
|
|
1039
|
+
if (!this.isDestroyed) this.renderer.input.focus();
|
|
1040
|
+
}, 10);
|
|
1041
|
+
this.emit("open", null);
|
|
1042
|
+
}
|
|
1043
|
+
closeDropdown() {
|
|
1044
|
+
if (!this.stateManager.getState().isOpen) return;
|
|
1045
|
+
this.stateManager.setState({
|
|
1046
|
+
isOpen: false,
|
|
1047
|
+
focusedIndex: -1,
|
|
1048
|
+
inputValue: ""
|
|
1049
|
+
});
|
|
1050
|
+
this.renderer.input.value = "";
|
|
1051
|
+
this.emit("close", null);
|
|
1052
|
+
}
|
|
1053
|
+
handleSelect(option) {
|
|
1054
|
+
if (option.disabled) return;
|
|
1055
|
+
const state = this.stateManager.getState();
|
|
1056
|
+
const update = applySelection(this.config, state, option);
|
|
1057
|
+
if (!this.config.multiple) this.closeDropdown();
|
|
1058
|
+
this.stateManager.setState({
|
|
1059
|
+
selectedValues: update.selectedValues,
|
|
1060
|
+
selectedOptionsByValue: update.selectedOptionsByValue,
|
|
1061
|
+
inputValue: ""
|
|
1062
|
+
});
|
|
1063
|
+
this.renderer.input.value = "";
|
|
1064
|
+
this.syncOriginalElement(update.selectedValues);
|
|
1065
|
+
if (update.tagEvent && update.tagOption) this.emit(update.tagEvent, update.tagOption);
|
|
1066
|
+
this.emit("change", this.getValue());
|
|
1067
|
+
}
|
|
1068
|
+
handleCreate(label) {
|
|
1069
|
+
const newOption = createOptionFromLabel(this.config, label);
|
|
1070
|
+
const state = this.stateManager.getState();
|
|
1071
|
+
this.stateManager.setState({ options: [...state.options, newOption] });
|
|
1072
|
+
this.handleSelect(newOption);
|
|
1073
|
+
}
|
|
1074
|
+
handleRemoveLastSelection() {
|
|
1075
|
+
const state = this.stateManager.getState();
|
|
1076
|
+
const update = removeLastSelection(this.config, state);
|
|
1077
|
+
this.stateManager.setState({
|
|
1078
|
+
selectedValues: update.selectedValues,
|
|
1079
|
+
selectedOptionsByValue: update.selectedOptionsByValue
|
|
1080
|
+
});
|
|
1081
|
+
this.syncOriginalElement(update.selectedValues);
|
|
1082
|
+
if (update.removedOption) this.emit("tagRemoved", update.removedOption);
|
|
1083
|
+
this.emit("change", this.getValue());
|
|
1084
|
+
}
|
|
1085
|
+
handleReorder(from, to) {
|
|
1086
|
+
const selectedValues = reorderSelectedValues(this.stateManager.getState(), from, to);
|
|
1087
|
+
this.stateManager.setState({ selectedValues });
|
|
1088
|
+
this.syncOriginalElement(selectedValues);
|
|
1089
|
+
this.emit("reordered", selectedValues);
|
|
1090
|
+
this.emit("change", this.getValue());
|
|
1091
|
+
}
|
|
1092
|
+
syncOriginalElement(values) {
|
|
1093
|
+
if (this.originalElement instanceof HTMLSelectElement) {
|
|
1094
|
+
const select = this.originalElement;
|
|
1095
|
+
Array.from(select.options).forEach((opt) => {
|
|
1096
|
+
opt.selected = values.includes(opt.value);
|
|
1097
|
+
});
|
|
1098
|
+
values.forEach((val) => {
|
|
1099
|
+
if (!Array.from(select.options).some((opt) => opt.value === val)) {
|
|
1100
|
+
const opt = new Option(val, val, true, true);
|
|
1101
|
+
select.add(opt);
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1104
|
+
select.dispatchEvent(new Event("change", { bubbles: true }));
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
render() {
|
|
1108
|
+
this.renderer.render(this.stateManager.getState(), getFilteredOptions(this.config, this.stateManager.getState()));
|
|
1109
|
+
}
|
|
1110
|
+
on(event, callback) {
|
|
1111
|
+
return this.events.on(event, callback);
|
|
1112
|
+
}
|
|
1113
|
+
emit(event, data) {
|
|
1114
|
+
this.events.emit(event, data);
|
|
1115
|
+
}
|
|
1116
|
+
getValue() {
|
|
1117
|
+
const state = this.stateManager.getState();
|
|
1118
|
+
return this.config.multiple ? state.selectedValues : state.selectedValues[0];
|
|
1119
|
+
}
|
|
1120
|
+
getSelectedOptions() {
|
|
1121
|
+
const selected = resolveSelectedOptions(this.config, this.stateManager.getState());
|
|
1122
|
+
return this.config.multiple ? selected : selected[0];
|
|
1123
|
+
}
|
|
1124
|
+
setValue(value, silent = false) {
|
|
1125
|
+
const state = this.stateManager.getState();
|
|
1126
|
+
const stringValues = (Array.isArray(value) ? value : [value]).filter((entry) => typeof entry === "string");
|
|
1127
|
+
const values = this.config.multiple ? Array.from(new Set(stringValues)) : stringValues.slice(0, 1);
|
|
1128
|
+
const selectedOptionsByValue = buildSelectedOptionsMapFromValues(this.config, state, values);
|
|
1129
|
+
this.stateManager.setState({
|
|
1130
|
+
selectedValues: values,
|
|
1131
|
+
selectedOptionsByValue
|
|
1132
|
+
});
|
|
1133
|
+
this.syncOriginalElement(values);
|
|
1134
|
+
if (!silent) this.emit("change", this.getValue());
|
|
1135
|
+
}
|
|
1136
|
+
setHeight(height) {
|
|
1137
|
+
this.config.height = height;
|
|
1138
|
+
this.renderer.setHeight(height);
|
|
1139
|
+
this.renderer.positionDropdown();
|
|
1140
|
+
}
|
|
1141
|
+
setRenderOption(callback) {
|
|
1142
|
+
this.config.renderOption = callback;
|
|
1143
|
+
this.renderer.updateConfig({ renderOption: callback });
|
|
1144
|
+
this.render();
|
|
1145
|
+
}
|
|
1146
|
+
setMaxOptions(max) {
|
|
1147
|
+
this.config.maxOptions = max;
|
|
1148
|
+
this.render();
|
|
1149
|
+
}
|
|
1150
|
+
destroy() {
|
|
1151
|
+
this.isDestroyed = true;
|
|
1152
|
+
this.remoteRequestId++;
|
|
1153
|
+
this.handleSearch.cancel();
|
|
1154
|
+
if (this.focusTimeoutId !== null) {
|
|
1155
|
+
clearTimeout(this.focusTimeoutId);
|
|
1156
|
+
this.focusTimeoutId = null;
|
|
1157
|
+
}
|
|
1158
|
+
if (this.unsubscribeState) {
|
|
1159
|
+
this.unsubscribeState();
|
|
1160
|
+
this.unsubscribeState = void 0;
|
|
1161
|
+
}
|
|
1162
|
+
this.unsubscribeEvents.forEach((unsub) => unsub());
|
|
1163
|
+
this.unsubscribeEvents = [];
|
|
1164
|
+
this.renderer.destroy();
|
|
1165
|
+
this.originalElement.style.display = "";
|
|
1166
|
+
}
|
|
1167
|
+
};
|
|
1168
|
+
//#endregion
|
|
1169
|
+
export { ThekSelect };
|