@smilodon/core 1.9.2 → 1.10.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/README.md +10 -6
- package/dist/index.cjs +2488 -436
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2473 -437
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.umd.js +2488 -436
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/components/enhanced-select.d.ts +114 -1
- package/dist/types/src/components/native-select.d.ts +2 -1
- package/dist/types/src/config/global-config.d.ts +2 -2
- package/dist/types/src/engine/accessibility.d.ts +109 -0
- package/dist/types/src/engine/contracts.d.ts +4 -0
- package/dist/types/src/engine/index.d.ts +9 -0
- package/dist/types/src/engine/item-adapter.d.ts +23 -0
- package/dist/types/src/engine/item-registry.d.ts +31 -0
- package/dist/types/src/engine/select-effects.d.ts +20 -0
- package/dist/types/src/engine/select-events.d.ts +55 -0
- package/dist/types/src/engine/select-machine.d.ts +9 -0
- package/dist/types/src/engine/select-state.d.ts +17 -0
- package/dist/types/src/engine/selection-policy.d.ts +33 -0
- package/dist/types/src/engine/selectors.d.ts +10 -0
- package/dist/types/src/engine/virtualization.d.ts +56 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/platform/web/accessibility-effects.d.ts +19 -0
- package/dist/types/src/platform/web/index.d.ts +2 -0
- package/dist/types/src/platform/web/select-effect-runner.d.ts +15 -0
- package/dist/types/src/platform/web/select-engine-bridge.d.ts +10 -0
- package/dist/types/src/utils/custom-option-pool.d.ts +3 -4
- package/dist/types/src/utils/data-source.d.ts +7 -2
- package/dist/types/src/utils/dom-pool.d.ts +1 -0
- package/dist/types/src/utils/telemetry.d.ts +2 -2
- package/dist/types/src/utils/virtualizer.d.ts +50 -1
- package/dist/types/tests/accessibility-contract.spec.d.ts +1 -0
- package/dist/types/tests/enhanced-select-accessibility-integration.spec.d.ts +1 -0
- package/dist/types/tests/enhanced-select-active-authority.spec.d.ts +1 -0
- package/dist/types/tests/enhanced-select-active-parity.spec.d.ts +1 -0
- package/dist/types/tests/enhanced-select-authority.spec.d.ts +1 -0
- package/dist/types/tests/enhanced-select-compatibility-projection.spec.d.ts +1 -0
- package/dist/types/tests/enhanced-select-selection-parity.spec.d.ts +1 -0
- package/dist/types/tests/enhanced-select-virtualization-integration.spec.d.ts +1 -0
- package/dist/types/tests/item-adapter.spec.d.ts +1 -0
- package/dist/types/tests/item-registry.spec.d.ts +1 -0
- package/dist/types/tests/memory-lifecycle.spec.d.ts +1 -0
- package/dist/types/tests/select-accessibility.spec.d.ts +1 -0
- package/dist/types/tests/select-selection.spec.d.ts +1 -0
- package/dist/types/tests/virtualization-contract.spec.d.ts +1 -0
- package/dist/types/tests/web-accessibility-effects.spec.d.ts +1 -0
- package/package.json +3 -3
package/dist/index.umd.js
CHANGED
|
@@ -46,6 +46,530 @@
|
|
|
46
46
|
container.replaceChildren(frag);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
const objectKeyCache = new WeakMap();
|
|
50
|
+
let generatedObjectKeyId = 0;
|
|
51
|
+
function isObjectRecord(value) {
|
|
52
|
+
return value !== null && (typeof value === 'object' || typeof value === 'function');
|
|
53
|
+
}
|
|
54
|
+
function readField(item, field) {
|
|
55
|
+
if (!field || !isObjectRecord(item))
|
|
56
|
+
return undefined;
|
|
57
|
+
return item[field];
|
|
58
|
+
}
|
|
59
|
+
function toSelectKey(value) {
|
|
60
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
if (typeof value === 'boolean' || typeof value === 'bigint' || typeof value === 'symbol') {
|
|
64
|
+
return String(value);
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
function objectIdentityKey(item) {
|
|
69
|
+
const cached = objectKeyCache.get(item);
|
|
70
|
+
if (cached)
|
|
71
|
+
return cached;
|
|
72
|
+
generatedObjectKeyId += 1;
|
|
73
|
+
const key = `__smilodon-object-${generatedObjectKeyId}`;
|
|
74
|
+
objectKeyCache.set(item, key);
|
|
75
|
+
return key;
|
|
76
|
+
}
|
|
77
|
+
function indexFallbackKey(index, warn) {
|
|
78
|
+
const env = globalThis.process?.env?.NODE_ENV;
|
|
79
|
+
if (env !== 'production') {
|
|
80
|
+
const message = '[Smilodon Phase2] Using index as key is unstable under filtering/sorting. Provide getKey/valueField for stable selection.';
|
|
81
|
+
if (warn) {
|
|
82
|
+
warn(message);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
console.warn(message);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return `__smilodon-index-${index}`;
|
|
89
|
+
}
|
|
90
|
+
/** Generated reference keys survive reorder, but not replacement with new objects. */
|
|
91
|
+
function createDefaultItemAdapter(options = {}) {
|
|
92
|
+
return {
|
|
93
|
+
getKey(item, index) {
|
|
94
|
+
if (options.getKey) {
|
|
95
|
+
return options.getKey(item, index);
|
|
96
|
+
}
|
|
97
|
+
if (isObjectRecord(item)) {
|
|
98
|
+
const directKey = toSelectKey(item.id)
|
|
99
|
+
?? toSelectKey(item.key)
|
|
100
|
+
?? toSelectKey(item.value)
|
|
101
|
+
?? toSelectKey(readField(item, options.valueField));
|
|
102
|
+
if (directKey !== null) {
|
|
103
|
+
return directKey;
|
|
104
|
+
}
|
|
105
|
+
if (options.getValue) {
|
|
106
|
+
const configuredValueKey = toSelectKey(options.getValue(item, index));
|
|
107
|
+
if (configuredValueKey !== null) {
|
|
108
|
+
return configuredValueKey;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return options.keyFallback === 'index'
|
|
112
|
+
? indexFallbackKey(index, options.onIndexFallback)
|
|
113
|
+
: objectIdentityKey(item);
|
|
114
|
+
}
|
|
115
|
+
return String(item);
|
|
116
|
+
},
|
|
117
|
+
getValue(item, index) {
|
|
118
|
+
if (options.getValue) {
|
|
119
|
+
return options.getValue(item, index);
|
|
120
|
+
}
|
|
121
|
+
const fieldValue = readField(item, options.valueField);
|
|
122
|
+
if (fieldValue !== undefined) {
|
|
123
|
+
return fieldValue;
|
|
124
|
+
}
|
|
125
|
+
return isObjectRecord(item) && item.value !== undefined ? item.value : item;
|
|
126
|
+
},
|
|
127
|
+
getLabel(item, index) {
|
|
128
|
+
if (options.getLabel) {
|
|
129
|
+
return options.getLabel(item, index);
|
|
130
|
+
}
|
|
131
|
+
const fieldLabel = readField(item, options.labelField);
|
|
132
|
+
if (fieldLabel !== undefined) {
|
|
133
|
+
return String(fieldLabel);
|
|
134
|
+
}
|
|
135
|
+
if (isObjectRecord(item) && item.label !== undefined) {
|
|
136
|
+
return String(item.label);
|
|
137
|
+
}
|
|
138
|
+
if (isObjectRecord(item) && item.text !== undefined) {
|
|
139
|
+
return String(item.text);
|
|
140
|
+
}
|
|
141
|
+
return String(item);
|
|
142
|
+
},
|
|
143
|
+
isDisabled(item, index) {
|
|
144
|
+
if (options.isDisabled) {
|
|
145
|
+
return options.isDisabled(item, index);
|
|
146
|
+
}
|
|
147
|
+
return Boolean(isObjectRecord(item) && item.disabled);
|
|
148
|
+
},
|
|
149
|
+
getGroupKey(item, index) {
|
|
150
|
+
if (options.getGroupKey) {
|
|
151
|
+
return options.getGroupKey(item, index);
|
|
152
|
+
}
|
|
153
|
+
if (!isObjectRecord(item) || item.group === undefined) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
return item.group == null ? null : String(item.group);
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function getDefaultDuplicateKeyPolicy() {
|
|
162
|
+
const env = globalThis.process?.env?.NODE_ENV;
|
|
163
|
+
return env === 'production' ? 'warn' : 'throw';
|
|
164
|
+
}
|
|
165
|
+
function createItemRegistry(items, adapter, options = {}) {
|
|
166
|
+
const duplicateKeyPolicy = options.duplicateKeyPolicy ?? getDefaultDuplicateKeyPolicy();
|
|
167
|
+
const records = [];
|
|
168
|
+
const keyToRecord = new Map();
|
|
169
|
+
const indexToKey = new Map();
|
|
170
|
+
const keyToIndex = new Map();
|
|
171
|
+
const firstIndexByKey = new Map();
|
|
172
|
+
const duplicateKeys = [];
|
|
173
|
+
items.forEach((item, index) => {
|
|
174
|
+
const key = adapter.getKey(item, index);
|
|
175
|
+
const firstIndex = firstIndexByKey.get(key);
|
|
176
|
+
const record = {
|
|
177
|
+
key,
|
|
178
|
+
item,
|
|
179
|
+
index,
|
|
180
|
+
label: adapter.getLabel(item, index),
|
|
181
|
+
value: adapter.getValue(item, index),
|
|
182
|
+
disabled: adapter.isDisabled?.(item, index) === true,
|
|
183
|
+
groupKey: adapter.getGroupKey?.(item, index) ?? null,
|
|
184
|
+
};
|
|
185
|
+
records.push(record);
|
|
186
|
+
indexToKey.set(index, key);
|
|
187
|
+
if (firstIndex !== undefined) {
|
|
188
|
+
const diagnostic = {
|
|
189
|
+
key,
|
|
190
|
+
firstIndex,
|
|
191
|
+
duplicateIndex: index,
|
|
192
|
+
};
|
|
193
|
+
duplicateKeys.push(diagnostic);
|
|
194
|
+
if (duplicateKeyPolicy === 'throw') {
|
|
195
|
+
throw new Error(duplicateKeyMessage(diagnostic));
|
|
196
|
+
}
|
|
197
|
+
if (duplicateKeyPolicy === 'warn') {
|
|
198
|
+
if (options.onDuplicateKey) {
|
|
199
|
+
options.onDuplicateKey(diagnostic);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
console.warn(duplicateKeyMessage(diagnostic));
|
|
203
|
+
}
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (duplicateKeyPolicy === 'first-wins') {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
firstIndexByKey.set(key, index);
|
|
212
|
+
}
|
|
213
|
+
keyToRecord.set(key, record);
|
|
214
|
+
keyToIndex.set(key, index);
|
|
215
|
+
});
|
|
216
|
+
return {
|
|
217
|
+
records,
|
|
218
|
+
keyToRecord,
|
|
219
|
+
indexToKey,
|
|
220
|
+
keyToIndex,
|
|
221
|
+
duplicateKeys,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function duplicateKeyMessage(diagnostic) {
|
|
225
|
+
return `[Smilodon Phase2] Duplicate item key "${String(diagnostic.key)}" at indexes ${diagnostic.firstIndex} and ${diagnostic.duplicateIndex}. Provide stable unique keys for selection.`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Pure key policy. Visibility does not constrain selection; disabled keys do. */
|
|
229
|
+
function applySelectionRequest(selectedKeys, request, config) {
|
|
230
|
+
let next;
|
|
231
|
+
switch (request.type) {
|
|
232
|
+
case 'select':
|
|
233
|
+
next = selectKey(selectedKeys, request.key, config);
|
|
234
|
+
break;
|
|
235
|
+
case 'deselect':
|
|
236
|
+
next = deselectKey(selectedKeys, request.key, config);
|
|
237
|
+
break;
|
|
238
|
+
case 'toggle':
|
|
239
|
+
if (!selectedKeys.has(request.key)) {
|
|
240
|
+
next = selectKey(selectedKeys, request.key, config);
|
|
241
|
+
}
|
|
242
|
+
else if (config.multiple || config.allowDeselect) {
|
|
243
|
+
next = deselectKey(selectedKeys, request.key, config);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
next = selectedKeys;
|
|
247
|
+
}
|
|
248
|
+
break;
|
|
249
|
+
case 'clear':
|
|
250
|
+
next = config.required && selectedKeys.size > 0
|
|
251
|
+
? selectedKeys
|
|
252
|
+
: new Set();
|
|
253
|
+
break;
|
|
254
|
+
case 'replace':
|
|
255
|
+
next = replaceKeys(selectedKeys, request.keys, config);
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
// Set membership is authoritative. Reordering the same keys is not a change.
|
|
259
|
+
return next.size === selectedKeys.size && [...next].every((key) => selectedKeys.has(key))
|
|
260
|
+
? selectedKeys
|
|
261
|
+
: next;
|
|
262
|
+
}
|
|
263
|
+
function selectKey(selectedKeys, key, config) {
|
|
264
|
+
if (config.disabledKeys.has(key)) {
|
|
265
|
+
return selectedKeys;
|
|
266
|
+
}
|
|
267
|
+
if (!config.multiple) {
|
|
268
|
+
return new Set([key]);
|
|
269
|
+
}
|
|
270
|
+
if (selectedKeys.has(key)) {
|
|
271
|
+
return selectedKeys;
|
|
272
|
+
}
|
|
273
|
+
const maxSelections = config.maxSelections ?? 0;
|
|
274
|
+
if (maxSelections > 0 && selectedKeys.size >= maxSelections) {
|
|
275
|
+
return selectedKeys;
|
|
276
|
+
}
|
|
277
|
+
return new Set([...selectedKeys, key]);
|
|
278
|
+
}
|
|
279
|
+
function deselectKey(selectedKeys, key, config) {
|
|
280
|
+
if (!selectedKeys.has(key) || config.disabledKeys.has(key)) {
|
|
281
|
+
return selectedKeys;
|
|
282
|
+
}
|
|
283
|
+
if (config.required && selectedKeys.size <= 1) {
|
|
284
|
+
return selectedKeys;
|
|
285
|
+
}
|
|
286
|
+
const next = new Set(selectedKeys);
|
|
287
|
+
next.delete(key);
|
|
288
|
+
return next;
|
|
289
|
+
}
|
|
290
|
+
function replaceKeys(selectedKeys, keys, config) {
|
|
291
|
+
const next = normalizeKeys(keys, config);
|
|
292
|
+
if (config.required && next.size === 0 && selectedKeys.size > 0) {
|
|
293
|
+
// Required preserves a valid selection, but cannot keep newly disabled keys.
|
|
294
|
+
return normalizeKeys(selectedKeys, config);
|
|
295
|
+
}
|
|
296
|
+
return next;
|
|
297
|
+
}
|
|
298
|
+
function normalizeKeys(keys, config) {
|
|
299
|
+
const seen = new Set();
|
|
300
|
+
const maxSelections = config.maxSelections ?? 0;
|
|
301
|
+
for (const key of keys) {
|
|
302
|
+
if (config.disabledKeys.has(key) || seen.has(key)) {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
seen.add(key);
|
|
306
|
+
if (!config.multiple || (maxSelections > 0 && seen.size >= maxSelections)) {
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return seen;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function transition(state, event) {
|
|
314
|
+
switch (event.type) {
|
|
315
|
+
case 'CONNECT':
|
|
316
|
+
return { state: { ...state, lifecycle: 'connected' }, effects: [] };
|
|
317
|
+
case 'DISCONNECT': {
|
|
318
|
+
const effects = state.popup === 'open'
|
|
319
|
+
? [{ type: 'POPUP_CLOSED', reason: 'disconnect' }, { type: 'SYNC_ARIA' }]
|
|
320
|
+
: [];
|
|
321
|
+
return {
|
|
322
|
+
state: { ...state, lifecycle: 'disconnected', popup: 'closed', focused: false },
|
|
323
|
+
effects,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
case 'OPEN_REQUEST':
|
|
327
|
+
if (state.lifecycle !== 'connected' || state.disabled || state.popup === 'open') {
|
|
328
|
+
return { state: enforceInvariants(state), effects: [] };
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
state: { ...state, popup: 'open' },
|
|
332
|
+
effects: [{ type: 'POPUP_OPENED' }, { type: 'FOCUS_TRIGGER' }, { type: 'SYNC_ARIA' }],
|
|
333
|
+
};
|
|
334
|
+
case 'CLOSE_REQUEST':
|
|
335
|
+
if (state.popup === 'closed') {
|
|
336
|
+
return { state, effects: [] };
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
state: { ...state, popup: 'closed' },
|
|
340
|
+
effects: [{ type: 'POPUP_CLOSED', reason: event.reason }, { type: 'SYNC_ARIA' }],
|
|
341
|
+
};
|
|
342
|
+
case 'TOGGLE_REQUEST':
|
|
343
|
+
return transition(state, state.popup === 'open'
|
|
344
|
+
? { type: 'CLOSE_REQUEST', reason: 'programmatic' }
|
|
345
|
+
: { type: 'OPEN_REQUEST' });
|
|
346
|
+
case 'FOCUS_GAINED':
|
|
347
|
+
return state.focused
|
|
348
|
+
? { state, effects: [] }
|
|
349
|
+
: { state: { ...state, focused: true }, effects: [{ type: 'SYNC_ARIA' }] };
|
|
350
|
+
case 'FOCUS_LOST':
|
|
351
|
+
return state.focused
|
|
352
|
+
? { state: { ...state, focused: false }, effects: [{ type: 'SYNC_ARIA' }] }
|
|
353
|
+
: { state, effects: [] };
|
|
354
|
+
case 'DISABLED_CHANGED': {
|
|
355
|
+
if (state.disabled === event.disabled) {
|
|
356
|
+
return { state: enforceInvariants(state), effects: [] };
|
|
357
|
+
}
|
|
358
|
+
const nextState = enforceInvariants({ ...state, disabled: event.disabled });
|
|
359
|
+
const effects = [];
|
|
360
|
+
if (event.disabled && state.popup === 'open') {
|
|
361
|
+
effects.push({ type: 'POPUP_CLOSED', reason: 'disabled' });
|
|
362
|
+
}
|
|
363
|
+
effects.push({ type: 'SYNC_ARIA' });
|
|
364
|
+
return {
|
|
365
|
+
state: nextState,
|
|
366
|
+
effects,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
case 'ITEMS_REGISTERED': {
|
|
370
|
+
const registeredKeys = new Set(event.keys);
|
|
371
|
+
const disabledKeys = new Set(event.disabledKeys ?? []);
|
|
372
|
+
const selection = { ...state.selection, disabledKeys };
|
|
373
|
+
const activeKey = resolveActiveKey(state.activeKey, registeredKeys, disabledKeys);
|
|
374
|
+
const selectedKeys = applySelectionRequest(state.selectedKeys, { type: 'replace', keys: Array.from(state.selectedKeys)}, selection);
|
|
375
|
+
const nextState = {
|
|
376
|
+
...state,
|
|
377
|
+
activeKey,
|
|
378
|
+
selectedKeys,
|
|
379
|
+
registeredKeys,
|
|
380
|
+
selection,
|
|
381
|
+
};
|
|
382
|
+
const effects = [];
|
|
383
|
+
if (!keysEqual(activeKey, state.activeKey)) {
|
|
384
|
+
effects.push({ type: 'ACTIVE_KEY_CHANGED', activeKey, reason: 'programmatic' });
|
|
385
|
+
}
|
|
386
|
+
if (!setsEqual(selectedKeys, state.selectedKeys)) {
|
|
387
|
+
effects.push({ type: 'SELECTION_CHANGED', selectedKeys: Array.from(selectedKeys), reason: 'programmatic' });
|
|
388
|
+
}
|
|
389
|
+
if (effects.length > 0) {
|
|
390
|
+
effects.push({ type: 'SYNC_ARIA' });
|
|
391
|
+
}
|
|
392
|
+
return { state: nextState, effects };
|
|
393
|
+
}
|
|
394
|
+
case 'SELECTION_CONFIG_CHANGED': {
|
|
395
|
+
const selection = {
|
|
396
|
+
...state.selection,
|
|
397
|
+
multiple: event.multiple ?? state.selection.multiple,
|
|
398
|
+
required: event.required ?? state.selection.required,
|
|
399
|
+
allowDeselect: event.allowDeselect ?? state.selection.allowDeselect,
|
|
400
|
+
maxSelections: event.maxSelections ?? state.selection.maxSelections,
|
|
401
|
+
disabledKeys: event.disabledKeys ? new Set(event.disabledKeys) : state.selection.disabledKeys,
|
|
402
|
+
};
|
|
403
|
+
const selectedKeys = applySelectionRequest(state.selectedKeys, { type: 'replace', keys: Array.from(state.selectedKeys)}, selection);
|
|
404
|
+
const activeKey = resolveActiveKey(state.activeKey, state.registeredKeys, selection.disabledKeys);
|
|
405
|
+
const nextState = { ...state, selection, selectedKeys, activeKey };
|
|
406
|
+
const effects = [];
|
|
407
|
+
if (!keysEqual(activeKey, state.activeKey)) {
|
|
408
|
+
effects.push({ type: 'ACTIVE_KEY_CHANGED', activeKey, reason: 'programmatic' });
|
|
409
|
+
}
|
|
410
|
+
if (!setsEqual(selectedKeys, state.selectedKeys)) {
|
|
411
|
+
effects.push({ type: 'SELECTION_CHANGED', selectedKeys: Array.from(selectedKeys), reason: 'programmatic' });
|
|
412
|
+
}
|
|
413
|
+
if (effects.length > 0) {
|
|
414
|
+
effects.push({ type: 'SYNC_ARIA' });
|
|
415
|
+
}
|
|
416
|
+
return { state: nextState, effects };
|
|
417
|
+
}
|
|
418
|
+
case 'ACTIVE_KEY_CHANGED': {
|
|
419
|
+
if (event.key !== null
|
|
420
|
+
&& (!state.registeredKeys.has(event.key) || state.selection.disabledKeys.has(event.key))) {
|
|
421
|
+
return { state, effects: [] };
|
|
422
|
+
}
|
|
423
|
+
if (keysEqual(state.activeKey, event.key)) {
|
|
424
|
+
return { state, effects: [] };
|
|
425
|
+
}
|
|
426
|
+
return {
|
|
427
|
+
state: { ...state, activeKey: event.key },
|
|
428
|
+
effects: [
|
|
429
|
+
{ type: 'ACTIVE_KEY_CHANGED', activeKey: event.key, reason: event.reason },
|
|
430
|
+
{ type: 'SYNC_ARIA' },
|
|
431
|
+
],
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
case 'SELECT_KEY_REQUEST':
|
|
435
|
+
case 'DESELECT_KEY_REQUEST':
|
|
436
|
+
case 'TOGGLE_KEY_REQUEST':
|
|
437
|
+
case 'CLEAR_SELECTION_REQUEST':
|
|
438
|
+
case 'REPLACE_SELECTION_REQUEST': {
|
|
439
|
+
const selectedKeys = applySelectionRequest(state.selectedKeys, selectionRequestFromEvent(event), state.selection);
|
|
440
|
+
if (setsEqual(selectedKeys, state.selectedKeys)) {
|
|
441
|
+
return { state, effects: [] };
|
|
442
|
+
}
|
|
443
|
+
return {
|
|
444
|
+
state: { ...state, selectedKeys },
|
|
445
|
+
effects: [
|
|
446
|
+
{ type: 'SELECTION_CHANGED', selectedKeys: Array.from(selectedKeys), reason: event.reason },
|
|
447
|
+
{ type: 'SYNC_ARIA' },
|
|
448
|
+
],
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
default:
|
|
452
|
+
return { state, effects: [] };
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
function selectionRequestFromEvent(event) {
|
|
456
|
+
switch (event.type) {
|
|
457
|
+
case 'SELECT_KEY_REQUEST':
|
|
458
|
+
return { type: 'select', key: event.key, reason: event.reason };
|
|
459
|
+
case 'DESELECT_KEY_REQUEST':
|
|
460
|
+
return { type: 'deselect', key: event.key, reason: event.reason };
|
|
461
|
+
case 'TOGGLE_KEY_REQUEST':
|
|
462
|
+
return { type: 'toggle', key: event.key, reason: event.reason };
|
|
463
|
+
case 'CLEAR_SELECTION_REQUEST':
|
|
464
|
+
return { type: 'clear', reason: event.reason };
|
|
465
|
+
case 'REPLACE_SELECTION_REQUEST':
|
|
466
|
+
return { type: 'replace', keys: event.keys, reason: event.reason };
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
function enforceInvariants(state) {
|
|
470
|
+
if (state.lifecycle === 'disconnected' || state.disabled) {
|
|
471
|
+
return state.popup === 'closed' ? state : { ...state, popup: 'closed' };
|
|
472
|
+
}
|
|
473
|
+
return state;
|
|
474
|
+
}
|
|
475
|
+
function resolveActiveKey(key, registeredKeys, disabledKeys) {
|
|
476
|
+
return key !== null && registeredKeys.has(key) && !disabledKeys.has(key) ? key : null;
|
|
477
|
+
}
|
|
478
|
+
function setsEqual(left, right) {
|
|
479
|
+
if (left.size !== right.size)
|
|
480
|
+
return false;
|
|
481
|
+
for (const value of left) {
|
|
482
|
+
if (!right.has(value))
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
/** Match Map/Set identity, including NaN and signed zero. */
|
|
488
|
+
function keysEqual(left, right) {
|
|
489
|
+
return left === right || Object.is(left, right);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function createInitialSelectState() {
|
|
493
|
+
return {
|
|
494
|
+
lifecycle: 'disconnected',
|
|
495
|
+
popup: 'closed',
|
|
496
|
+
disabled: false,
|
|
497
|
+
focused: false,
|
|
498
|
+
query: '',
|
|
499
|
+
activeKey: null,
|
|
500
|
+
selectedKeys: new Set(),
|
|
501
|
+
registeredKeys: new Set(),
|
|
502
|
+
selection: {
|
|
503
|
+
multiple: false,
|
|
504
|
+
required: false,
|
|
505
|
+
allowDeselect: false,
|
|
506
|
+
disabledKeys: new Set(),
|
|
507
|
+
maxSelections: 0,
|
|
508
|
+
},
|
|
509
|
+
dataStatus: 'idle',
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function isOpen(state) {
|
|
514
|
+
return state.popup === 'open';
|
|
515
|
+
}
|
|
516
|
+
function isDisabled(state) {
|
|
517
|
+
return state.disabled;
|
|
518
|
+
}
|
|
519
|
+
function isFocused(state) {
|
|
520
|
+
return state.focused;
|
|
521
|
+
}
|
|
522
|
+
function hasSelection(state) {
|
|
523
|
+
return state.selectedKeys.size > 0;
|
|
524
|
+
}
|
|
525
|
+
function isSelected(state, key) {
|
|
526
|
+
return state.selectedKeys.has(key);
|
|
527
|
+
}
|
|
528
|
+
function getSelectedKeys(state) {
|
|
529
|
+
return Array.from(state.selectedKeys);
|
|
530
|
+
}
|
|
531
|
+
function getActiveKey(state) {
|
|
532
|
+
return state.activeKey;
|
|
533
|
+
}
|
|
534
|
+
function isRegisteredKey(state, key) {
|
|
535
|
+
return state.registeredKeys.has(key);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
class SelectEngineBridge {
|
|
539
|
+
constructor(initialState = createInitialSelectState()) {
|
|
540
|
+
this._state = initialState;
|
|
541
|
+
}
|
|
542
|
+
get state() {
|
|
543
|
+
return this._state;
|
|
544
|
+
}
|
|
545
|
+
dispatch(event) {
|
|
546
|
+
const result = transition(this._state, event);
|
|
547
|
+
this._state = result.state;
|
|
548
|
+
return result.effects;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
class DefaultSelectEffectRunner {
|
|
553
|
+
run(effects, context) {
|
|
554
|
+
for (const effect of effects) {
|
|
555
|
+
switch (effect.type) {
|
|
556
|
+
case 'FOCUS_TRIGGER':
|
|
557
|
+
context.focusTrigger?.();
|
|
558
|
+
break;
|
|
559
|
+
case 'SYNC_ARIA':
|
|
560
|
+
context.syncAria?.();
|
|
561
|
+
break;
|
|
562
|
+
case 'POPUP_OPENED':
|
|
563
|
+
context.onPopupOpened?.();
|
|
564
|
+
break;
|
|
565
|
+
case 'POPUP_CLOSED':
|
|
566
|
+
context.onPopupClosed?.(effect.reason);
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
49
573
|
/**
|
|
50
574
|
* Fenwick Tree (Binary Indexed Tree) for O(log n) prefix sum queries and updates.
|
|
51
575
|
* Used for cumulative height calculations in variable-height virtualization.
|
|
@@ -249,6 +773,10 @@
|
|
|
249
773
|
if (poolNode) {
|
|
250
774
|
poolNode.inUse = false;
|
|
251
775
|
poolNode.lastUsed = performance.now();
|
|
776
|
+
if (this.pool.length > this.maxSize) {
|
|
777
|
+
this.removePoolNode(poolNode);
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
252
780
|
// Aggressive cleanup
|
|
253
781
|
this.reset(poolNode.node);
|
|
254
782
|
}
|
|
@@ -269,12 +797,19 @@
|
|
|
269
797
|
}
|
|
270
798
|
}
|
|
271
799
|
if (lruIndex >= 0) {
|
|
272
|
-
|
|
273
|
-
// Full cleanup before GC
|
|
274
|
-
this.deepCleanup(evicted.node);
|
|
800
|
+
this.removePoolNode(this.pool[lruIndex]);
|
|
275
801
|
if (this.telemetry)
|
|
276
802
|
this.evictions++;
|
|
803
|
+
return true;
|
|
277
804
|
}
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
807
|
+
removePoolNode(poolNode) {
|
|
808
|
+
const index = this.pool.indexOf(poolNode);
|
|
809
|
+
if (index < 0)
|
|
810
|
+
return;
|
|
811
|
+
const [removed] = this.pool.splice(index, 1);
|
|
812
|
+
this.deepCleanup(removed.node);
|
|
278
813
|
}
|
|
279
814
|
/**
|
|
280
815
|
* Default reset: clear content, remove inline styles, remove attributes
|
|
@@ -324,7 +859,8 @@
|
|
|
324
859
|
this.maxSize = size;
|
|
325
860
|
// Evict excess nodes
|
|
326
861
|
while (this.pool.length > this.maxSize) {
|
|
327
|
-
this.evictLRU()
|
|
862
|
+
if (!this.evictLRU())
|
|
863
|
+
break;
|
|
328
864
|
}
|
|
329
865
|
}
|
|
330
866
|
/**
|
|
@@ -359,11 +895,17 @@
|
|
|
359
895
|
class Virtualizer {
|
|
360
896
|
constructor(container, itemsLength, itemGetter, options) {
|
|
361
897
|
this.measuredHeights = new Map();
|
|
898
|
+
this.measuredHeightTotal = 0;
|
|
362
899
|
this.activeNodes = new Map(); // index -> node mapping
|
|
900
|
+
this.destroyed = false;
|
|
901
|
+
this.recycleRafId = 0;
|
|
902
|
+
this.intendedScrollTop = 0;
|
|
903
|
+
this.currentWindow = null;
|
|
363
904
|
this.container = container;
|
|
364
905
|
this.itemsLength = itemsLength;
|
|
365
906
|
this.itemGetter = itemGetter;
|
|
366
907
|
this.options = options;
|
|
908
|
+
this.estimatedItemHeight = options.estimatedItemHeight;
|
|
367
909
|
this.averageHeight = options.estimatedItemHeight;
|
|
368
910
|
// Initialize DOM pool with LRU eviction
|
|
369
911
|
const maxPoolSize = Math.ceil((options.buffer * 2 + 10) * 1.5) + (options.maxPoolExtra ?? 32);
|
|
@@ -384,39 +926,121 @@
|
|
|
384
926
|
},
|
|
385
927
|
telemetry: options.enableTelemetry || false,
|
|
386
928
|
});
|
|
387
|
-
//
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
// Initialize with estimated heights
|
|
391
|
-
for (let i = 0; i < itemsLength; i++) {
|
|
392
|
-
this.fenwick.add(i, options.estimatedItemHeight);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
929
|
+
// One prefix index keeps offset, window, and scroll calculations on the
|
|
930
|
+
// same measured-height policy at every list size.
|
|
931
|
+
this.fenwick = this.createFenwickTree(itemsLength);
|
|
395
932
|
this.container.style.willChange = 'transform';
|
|
396
933
|
}
|
|
397
934
|
computeWindow(scrollTop, viewportHeight) {
|
|
398
935
|
const { buffer } = this.options;
|
|
399
|
-
const
|
|
400
|
-
const
|
|
936
|
+
const visibleStartIndex = this.indexAtOffset(scrollTop);
|
|
937
|
+
const calculatedStartIndex = visibleStartIndex - buffer;
|
|
938
|
+
const startIndex = this.itemsLength > 0
|
|
939
|
+
? Math.max(0, Math.min(this.itemsLength - 1, calculatedStartIndex))
|
|
940
|
+
: 0;
|
|
941
|
+
const viewportEnd = Math.max(0, scrollTop + viewportHeight);
|
|
942
|
+
let visibleEndIndex = this.indexAtOffset(viewportEnd);
|
|
943
|
+
// The item beginning exactly at the viewport's lower edge is not visible.
|
|
944
|
+
// indexAtOffset deliberately advances across exact boundaries for the
|
|
945
|
+
// viewport start, so reverse that choice here when the viewport has height.
|
|
946
|
+
if (viewportHeight > 0
|
|
947
|
+
&& visibleEndIndex > visibleStartIndex
|
|
948
|
+
&& this.cumulativeOffset(visibleEndIndex) === viewportEnd) {
|
|
949
|
+
visibleEndIndex -= 1;
|
|
950
|
+
}
|
|
951
|
+
const calculatedEndIndex = Math.max(visibleStartIndex, visibleEndIndex) + buffer;
|
|
952
|
+
const endIndex = this.itemsLength > 0
|
|
953
|
+
? Math.max(0, Math.min(this.itemsLength - 1, calculatedEndIndex))
|
|
954
|
+
: -1;
|
|
401
955
|
const windowSize = Math.max(0, endIndex - startIndex + 1);
|
|
402
956
|
// Adjust pool size dynamically based on window
|
|
403
957
|
const newPoolSize = windowSize + (this.options.maxPoolExtra ?? 32);
|
|
404
958
|
this.pool.setMaxSize(newPoolSize);
|
|
405
959
|
return { startIndex, endIndex, windowSize };
|
|
406
960
|
}
|
|
407
|
-
|
|
408
|
-
|
|
961
|
+
/** Computes and records a scroll-derived window without installing listeners. */
|
|
962
|
+
updateWindow(scrollTop, viewportHeight) {
|
|
963
|
+
if (this.destroyed)
|
|
964
|
+
return null;
|
|
965
|
+
const safeScrollTop = Number.isFinite(scrollTop) ? Math.max(0, scrollTop) : 0;
|
|
966
|
+
const safeViewportHeight = Number.isFinite(viewportHeight) && viewportHeight >= 0
|
|
967
|
+
? viewportHeight
|
|
968
|
+
: 0;
|
|
969
|
+
const nextWindow = this.computeWindow(safeScrollTop, safeViewportHeight);
|
|
970
|
+
const changed = this.currentWindow === null
|
|
971
|
+
|| this.currentWindow.startIndex !== nextWindow.startIndex
|
|
972
|
+
|| this.currentWindow.endIndex !== nextWindow.endIndex
|
|
973
|
+
|| this.currentWindow.windowSize !== nextWindow.windowSize;
|
|
974
|
+
this.currentWindow = { ...nextWindow };
|
|
975
|
+
return {
|
|
976
|
+
window: { ...nextWindow },
|
|
977
|
+
changed,
|
|
978
|
+
scrollTop: safeScrollTop,
|
|
979
|
+
viewportHeight: safeViewportHeight,
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
/** Returns a copy of the last recorded window, or null before the first update. */
|
|
983
|
+
getCurrentWindow() {
|
|
984
|
+
return this.currentWindow ? { ...this.currentWindow } : null;
|
|
985
|
+
}
|
|
986
|
+
/** Finds the item containing an offset, advancing past exact item boundaries. */
|
|
987
|
+
indexAtOffset(offset) {
|
|
988
|
+
if (this.itemsLength <= 0)
|
|
409
989
|
return 0;
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
990
|
+
const target = Math.max(0, offset);
|
|
991
|
+
let index = Math.max(0, Math.min(this.itemsLength - 1, this.fenwick.lowerBound(target)));
|
|
992
|
+
// FenwickTree.lowerBound returns the preceding item when the target lands
|
|
993
|
+
// exactly on an item boundary. For the viewport start, select the next item.
|
|
994
|
+
while (index < this.itemsLength - 1 && this.cumulativeOffset(index + 1) <= target) {
|
|
995
|
+
index += 1;
|
|
413
996
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
997
|
+
return index;
|
|
998
|
+
}
|
|
999
|
+
cumulativeOffset(index) {
|
|
1000
|
+
if (!Number.isFinite(index)) {
|
|
1001
|
+
return index === Number.POSITIVE_INFINITY && this.itemsLength > 0
|
|
1002
|
+
? this.fenwick.sum(this.itemsLength - 1)
|
|
1003
|
+
: 0;
|
|
418
1004
|
}
|
|
419
|
-
|
|
1005
|
+
const boundedIndex = Math.max(0, Math.min(this.itemsLength, Math.trunc(index)));
|
|
1006
|
+
if (boundedIndex === 0)
|
|
1007
|
+
return 0;
|
|
1008
|
+
return this.fenwick.sum(boundedIndex - 1);
|
|
1009
|
+
}
|
|
1010
|
+
/**
|
|
1011
|
+
* Calculates and records the intended scroll position for an item without
|
|
1012
|
+
* mutating the container. The caller may apply the returned scrollTop.
|
|
1013
|
+
* Returns null after destruction or when there are no items to target.
|
|
1014
|
+
*/
|
|
1015
|
+
scrollToIndex(index) {
|
|
1016
|
+
if (this.destroyed || this.itemsLength <= 0)
|
|
1017
|
+
return null;
|
|
1018
|
+
const requestedIndex = Number.isFinite(index) ? Math.trunc(index) : 0;
|
|
1019
|
+
const targetIndex = Math.max(0, Math.min(this.itemsLength - 1, requestedIndex));
|
|
1020
|
+
const offset = this.cumulativeOffset(targetIndex);
|
|
1021
|
+
this.intendedScrollTop = offset;
|
|
1022
|
+
return { index: targetIndex, offset, scrollTop: offset };
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Resolves a stable key against the caller's current data identity, then
|
|
1026
|
+
* delegates to scrollToIndex(). Invalid or out-of-range resolutions are
|
|
1027
|
+
* rejected rather than clamped to a different item's position.
|
|
1028
|
+
*/
|
|
1029
|
+
scrollToKey(key, resolveIndex) {
|
|
1030
|
+
if (this.destroyed || this.itemsLength <= 0)
|
|
1031
|
+
return null;
|
|
1032
|
+
const index = resolveIndex(key);
|
|
1033
|
+
if (typeof index !== 'number'
|
|
1034
|
+
|| !Number.isInteger(index)
|
|
1035
|
+
|| index < 0
|
|
1036
|
+
|| index >= this.itemsLength) {
|
|
1037
|
+
return null;
|
|
1038
|
+
}
|
|
1039
|
+
return this.scrollToIndex(index);
|
|
1040
|
+
}
|
|
1041
|
+
/** Returns the last scroll position requested through scrollToIndex(). */
|
|
1042
|
+
getIntendedScrollTop() {
|
|
1043
|
+
return this.intendedScrollTop;
|
|
420
1044
|
}
|
|
421
1045
|
acquireNode(index) {
|
|
422
1046
|
// Check if we already have this node active (reuse)
|
|
@@ -445,6 +1069,8 @@
|
|
|
445
1069
|
}
|
|
446
1070
|
}
|
|
447
1071
|
render(startIndex, endIndex, updateNode) {
|
|
1072
|
+
if (this.destroyed)
|
|
1073
|
+
return;
|
|
448
1074
|
const frag = document.createDocumentFragment();
|
|
449
1075
|
const activeIndices = new Set();
|
|
450
1076
|
for (let i = startIndex; i <= endIndex; i++) {
|
|
@@ -453,31 +1079,51 @@
|
|
|
453
1079
|
frag.appendChild(node);
|
|
454
1080
|
activeIndices.add(i);
|
|
455
1081
|
// Measure on appear (deferred)
|
|
456
|
-
queueMicrotask(() =>
|
|
1082
|
+
queueMicrotask(() => {
|
|
1083
|
+
if (!this.destroyed) {
|
|
1084
|
+
this.measureOnAppear(node, i);
|
|
1085
|
+
}
|
|
1086
|
+
});
|
|
457
1087
|
}
|
|
458
1088
|
// Translate container by cumulative offset of startIndex
|
|
459
1089
|
const topOffset = this.cumulativeOffset(startIndex);
|
|
460
1090
|
this.container.style.transform = `translate3d(0, ${Math.round(topOffset * 100) / 100}px, 0)`;
|
|
461
1091
|
this.container.replaceChildren(frag);
|
|
462
1092
|
// Recycle nodes not in use
|
|
463
|
-
|
|
1093
|
+
if (this.recycleRafId) {
|
|
1094
|
+
cancelAnimationFrame(this.recycleRafId);
|
|
1095
|
+
}
|
|
1096
|
+
this.recycleRafId = requestAnimationFrame(() => {
|
|
1097
|
+
this.recycleRafId = 0;
|
|
1098
|
+
if (!this.destroyed) {
|
|
1099
|
+
this.releaseExcess(activeIndices);
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
464
1102
|
}
|
|
465
1103
|
measureOnAppear(node, index) {
|
|
466
|
-
|
|
1104
|
+
this.measureItem(index, node.offsetHeight);
|
|
1105
|
+
}
|
|
1106
|
+
/** Records a validated measured height and updates the virtual offset index. */
|
|
1107
|
+
measureItem(index, height) {
|
|
1108
|
+
if (this.destroyed
|
|
1109
|
+
|| !Number.isInteger(index)
|
|
1110
|
+
|| index < 0
|
|
1111
|
+
|| index >= this.itemsLength
|
|
1112
|
+
|| !Number.isFinite(height)
|
|
1113
|
+
|| height <= 0)
|
|
1114
|
+
return false;
|
|
467
1115
|
const prev = this.measuredHeights.get(index);
|
|
468
1116
|
const threshold = this.options.measurementThreshold ?? 5;
|
|
469
|
-
if (prev === undefined || Math.abs(prev -
|
|
470
|
-
const oldValue = prev ?? this.
|
|
471
|
-
this.measuredHeights.set(index,
|
|
1117
|
+
if (prev === undefined || Math.abs(prev - height) > threshold) {
|
|
1118
|
+
const oldValue = prev ?? this.estimatedItemHeight;
|
|
1119
|
+
this.measuredHeights.set(index, height);
|
|
1120
|
+
this.measuredHeightTotal += height - (prev ?? 0);
|
|
472
1121
|
// Update Fenwick tree with new measurement
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
// Update running average (weighted by measured count)
|
|
477
|
-
const count = this.measuredHeights.size;
|
|
478
|
-
const total = Array.from(this.measuredHeights.values()).reduce((a, b) => a + b, 0);
|
|
479
|
-
this.averageHeight = Math.round((total / count) * 100) / 100;
|
|
1122
|
+
this.fenwick.update(index, oldValue, height);
|
|
1123
|
+
this.updateAverageHeight();
|
|
1124
|
+
return true;
|
|
480
1125
|
}
|
|
1126
|
+
return false;
|
|
481
1127
|
}
|
|
482
1128
|
/**
|
|
483
1129
|
* Get pool statistics (if telemetry enabled)
|
|
@@ -489,29 +1135,63 @@
|
|
|
489
1135
|
* Update items length and resize Fenwick tree if needed
|
|
490
1136
|
*/
|
|
491
1137
|
setItemsLength(newLength) {
|
|
1138
|
+
const previousLength = this.itemsLength;
|
|
492
1139
|
this.itemsLength = newLength;
|
|
493
|
-
if (
|
|
494
|
-
this.
|
|
1140
|
+
if (newLength !== previousLength)
|
|
1141
|
+
this.currentWindow = null;
|
|
1142
|
+
let removedMeasurement = false;
|
|
1143
|
+
for (const [index, height] of this.measuredHeights) {
|
|
1144
|
+
if (index >= newLength) {
|
|
1145
|
+
this.measuredHeightTotal -= height;
|
|
1146
|
+
this.measuredHeights.delete(index);
|
|
1147
|
+
removedMeasurement = true;
|
|
1148
|
+
}
|
|
495
1149
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
this.fenwick.update(index, this.averageHeight, height);
|
|
1150
|
+
if (removedMeasurement)
|
|
1151
|
+
this.updateAverageHeight();
|
|
1152
|
+
for (const index of Array.from(this.activeNodes.keys())) {
|
|
1153
|
+
if (index >= newLength) {
|
|
1154
|
+
this.releaseNode(index);
|
|
502
1155
|
}
|
|
503
1156
|
}
|
|
1157
|
+
if (newLength !== previousLength) {
|
|
1158
|
+
this.fenwick = this.createFenwickTree(newLength);
|
|
1159
|
+
}
|
|
504
1160
|
}
|
|
505
1161
|
/**
|
|
506
1162
|
* Cleanup resources
|
|
507
1163
|
*/
|
|
508
1164
|
destroy() {
|
|
1165
|
+
this.destroyed = true;
|
|
1166
|
+
if (this.recycleRafId) {
|
|
1167
|
+
cancelAnimationFrame(this.recycleRafId);
|
|
1168
|
+
this.recycleRafId = 0;
|
|
1169
|
+
}
|
|
509
1170
|
this.pool.clear();
|
|
510
1171
|
this.activeNodes.clear();
|
|
511
1172
|
this.measuredHeights.clear();
|
|
512
|
-
|
|
513
|
-
|
|
1173
|
+
this.measuredHeightTotal = 0;
|
|
1174
|
+
this.averageHeight = this.estimatedItemHeight;
|
|
1175
|
+
this.currentWindow = null;
|
|
1176
|
+
this.fenwick.reset();
|
|
1177
|
+
}
|
|
1178
|
+
updateAverageHeight() {
|
|
1179
|
+
const count = this.measuredHeights.size;
|
|
1180
|
+
this.averageHeight = count > 0
|
|
1181
|
+
? Math.round((this.measuredHeightTotal / count) * 100) / 100
|
|
1182
|
+
: this.estimatedItemHeight;
|
|
1183
|
+
}
|
|
1184
|
+
createFenwickTree(size) {
|
|
1185
|
+
const tree = new FenwickTree(size);
|
|
1186
|
+
for (let index = 0; index < size; index++) {
|
|
1187
|
+
tree.add(index, this.estimatedItemHeight);
|
|
1188
|
+
}
|
|
1189
|
+
for (const [index, height] of this.measuredHeights) {
|
|
1190
|
+
if (index < size) {
|
|
1191
|
+
tree.update(index, this.estimatedItemHeight, height);
|
|
1192
|
+
}
|
|
514
1193
|
}
|
|
1194
|
+
return tree;
|
|
515
1195
|
}
|
|
516
1196
|
}
|
|
517
1197
|
|
|
@@ -541,30 +1221,18 @@
|
|
|
541
1221
|
* @returns Component instance
|
|
542
1222
|
*/
|
|
543
1223
|
acquire(factory, item, index, context, container) {
|
|
544
|
-
|
|
1224
|
+
this.release(index);
|
|
545
1225
|
// Try to find an available component in the pool
|
|
546
|
-
const pooled = this._findAvailableComponent(
|
|
1226
|
+
const pooled = this._findAvailableComponent(factory);
|
|
547
1227
|
let component;
|
|
548
1228
|
if (pooled) {
|
|
549
1229
|
// Reuse pooled component
|
|
550
1230
|
component = pooled.instance;
|
|
551
|
-
pooled.inUse = true;
|
|
552
|
-
pooled.lastUsedIndex = index;
|
|
553
1231
|
}
|
|
554
1232
|
else {
|
|
555
1233
|
// Create new component
|
|
556
1234
|
try {
|
|
557
1235
|
component = factory(item, index);
|
|
558
|
-
// Add to pool if under limit
|
|
559
|
-
const pool = this._pool.get(factoryKey) || [];
|
|
560
|
-
if (pool.length < this._maxPoolSize) {
|
|
561
|
-
pool.push({
|
|
562
|
-
instance: component,
|
|
563
|
-
inUse: true,
|
|
564
|
-
lastUsedIndex: index
|
|
565
|
-
});
|
|
566
|
-
this._pool.set(factoryKey, pool);
|
|
567
|
-
}
|
|
568
1236
|
}
|
|
569
1237
|
catch (error) {
|
|
570
1238
|
console.error(`[CustomOptionPool] Failed to create component:`, error);
|
|
@@ -574,9 +1242,25 @@
|
|
|
574
1242
|
// Mount the component
|
|
575
1243
|
try {
|
|
576
1244
|
component.mountOption(container, context);
|
|
1245
|
+
if (pooled) {
|
|
1246
|
+
pooled.inUse = true;
|
|
1247
|
+
pooled.lastUsedIndex = index;
|
|
1248
|
+
}
|
|
1249
|
+
else {
|
|
1250
|
+
this._addToPool(factory, component, index);
|
|
1251
|
+
}
|
|
577
1252
|
this._activeComponents.set(index, component);
|
|
578
1253
|
}
|
|
579
1254
|
catch (error) {
|
|
1255
|
+
if (pooled) {
|
|
1256
|
+
pooled.inUse = false;
|
|
1257
|
+
}
|
|
1258
|
+
try {
|
|
1259
|
+
component.unmountOption();
|
|
1260
|
+
}
|
|
1261
|
+
catch {
|
|
1262
|
+
// ignore cleanup failures after a mount failure
|
|
1263
|
+
}
|
|
580
1264
|
console.error(`[CustomOptionPool] Failed to mount component at index ${index}:`, error);
|
|
581
1265
|
throw error;
|
|
582
1266
|
}
|
|
@@ -673,18 +1357,44 @@
|
|
|
673
1357
|
/**
|
|
674
1358
|
* Find an available component in the pool
|
|
675
1359
|
*/
|
|
676
|
-
_findAvailableComponent(
|
|
677
|
-
const pool = this._pool.get(
|
|
1360
|
+
_findAvailableComponent(factory) {
|
|
1361
|
+
const pool = this._pool.get(factory);
|
|
678
1362
|
if (!pool)
|
|
679
1363
|
return undefined;
|
|
680
1364
|
return pool.find(p => !p.inUse);
|
|
681
1365
|
}
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
1366
|
+
_addToPool(factory, component, index) {
|
|
1367
|
+
while (this._getTotalPooled() >= this._maxPoolSize) {
|
|
1368
|
+
if (!this._evictAvailableComponent())
|
|
1369
|
+
return;
|
|
1370
|
+
}
|
|
1371
|
+
const pool = this._pool.get(factory) || [];
|
|
1372
|
+
pool.push({
|
|
1373
|
+
instance: component,
|
|
1374
|
+
inUse: true,
|
|
1375
|
+
lastUsedIndex: index
|
|
1376
|
+
});
|
|
1377
|
+
this._pool.set(factory, pool);
|
|
1378
|
+
}
|
|
1379
|
+
_evictAvailableComponent() {
|
|
1380
|
+
for (const [factory, pool] of this._pool.entries()) {
|
|
1381
|
+
const index = pool.findIndex(p => !p.inUse);
|
|
1382
|
+
if (index < 0)
|
|
1383
|
+
continue;
|
|
1384
|
+
pool.splice(index, 1);
|
|
1385
|
+
if (pool.length === 0) {
|
|
1386
|
+
this._pool.delete(factory);
|
|
1387
|
+
}
|
|
1388
|
+
return true;
|
|
1389
|
+
}
|
|
1390
|
+
return false;
|
|
1391
|
+
}
|
|
1392
|
+
_getTotalPooled() {
|
|
1393
|
+
let total = 0;
|
|
1394
|
+
for (const pool of this._pool.values()) {
|
|
1395
|
+
total += pool.length;
|
|
1396
|
+
}
|
|
1397
|
+
return total;
|
|
688
1398
|
}
|
|
689
1399
|
}
|
|
690
1400
|
|
|
@@ -718,6 +1428,7 @@
|
|
|
718
1428
|
const value = this._config.getValue(item);
|
|
719
1429
|
const label = this._config.getLabel(item);
|
|
720
1430
|
const isDisabled = this._config.getDisabled ? this._config.getDisabled(item) : false;
|
|
1431
|
+
this.unmount(index);
|
|
721
1432
|
// Determine if this is a custom component or lightweight option
|
|
722
1433
|
const hasCustomComponent = extendedItem.optionComponent && typeof extendedItem.optionComponent === 'function';
|
|
723
1434
|
if (hasCustomComponent) {
|
|
@@ -908,7 +1619,7 @@
|
|
|
908
1619
|
|
|
909
1620
|
class NativeSelectElement extends HTMLElement {
|
|
910
1621
|
static get observedAttributes() {
|
|
911
|
-
return [
|
|
1622
|
+
return ["placement", "strategy", "portal"];
|
|
912
1623
|
}
|
|
913
1624
|
constructor() {
|
|
914
1625
|
super();
|
|
@@ -919,22 +1630,23 @@
|
|
|
919
1630
|
this._selectedItems = new Map(); // index -> item
|
|
920
1631
|
this._activeIndex = -1;
|
|
921
1632
|
this._multi = false;
|
|
922
|
-
this._typeBuffer =
|
|
923
|
-
this._shadow = this.attachShadow({ mode:
|
|
924
|
-
this._listRoot = document.createElement(
|
|
925
|
-
this._listRoot.setAttribute(
|
|
926
|
-
this._listRoot.setAttribute(
|
|
1633
|
+
this._typeBuffer = "";
|
|
1634
|
+
this._shadow = this.attachShadow({ mode: "open" });
|
|
1635
|
+
this._listRoot = document.createElement("div");
|
|
1636
|
+
this._listRoot.setAttribute("role", "listbox");
|
|
1637
|
+
this._listRoot.setAttribute("tabindex", "0");
|
|
927
1638
|
this._shadow.appendChild(this._listRoot);
|
|
928
1639
|
// Live region for screen reader announcements
|
|
929
|
-
this._liveRegion = document.createElement(
|
|
930
|
-
this._liveRegion.setAttribute(
|
|
931
|
-
this._liveRegion.setAttribute(
|
|
932
|
-
this._liveRegion.style.cssText =
|
|
1640
|
+
this._liveRegion = document.createElement("div");
|
|
1641
|
+
this._liveRegion.setAttribute("role", "status");
|
|
1642
|
+
this._liveRegion.setAttribute("aria-live", "polite");
|
|
1643
|
+
this._liveRegion.style.cssText =
|
|
1644
|
+
"position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden;";
|
|
933
1645
|
this._shadow.appendChild(this._liveRegion);
|
|
934
1646
|
this._helpers = createRendererHelpers((item, index) => this._onSelect(item, index));
|
|
935
1647
|
// Delegated click
|
|
936
|
-
this._listRoot.addEventListener(
|
|
937
|
-
const el = e.target.closest(
|
|
1648
|
+
this._listRoot.addEventListener("click", (e) => {
|
|
1649
|
+
const el = e.target.closest("[data-selectable]");
|
|
938
1650
|
if (!el)
|
|
939
1651
|
return;
|
|
940
1652
|
const idx = Number(el.dataset.index);
|
|
@@ -942,28 +1654,33 @@
|
|
|
942
1654
|
this._onSelect(item, idx);
|
|
943
1655
|
});
|
|
944
1656
|
// Keyboard navigation
|
|
945
|
-
this._listRoot.addEventListener(
|
|
1657
|
+
this._listRoot.addEventListener("keydown", (e) => this._onKeydown(e));
|
|
946
1658
|
}
|
|
947
1659
|
connectedCallback() {
|
|
948
1660
|
// Initialize ARIA roles and open event
|
|
949
|
-
this._listRoot.setAttribute(
|
|
950
|
-
this._listRoot.setAttribute(
|
|
1661
|
+
this._listRoot.setAttribute("role", "listbox");
|
|
1662
|
+
this._listRoot.setAttribute("aria-label", "Options list");
|
|
951
1663
|
if (this._multi)
|
|
952
|
-
this._listRoot.setAttribute(
|
|
1664
|
+
this._listRoot.setAttribute("aria-multiselectable", "true");
|
|
953
1665
|
this._initializeOptionRenderer();
|
|
954
|
-
this._emit(
|
|
1666
|
+
this._emit("open", {});
|
|
955
1667
|
}
|
|
956
1668
|
disconnectedCallback() {
|
|
957
|
-
this._emit(
|
|
1669
|
+
this._emit("close", {});
|
|
958
1670
|
// Cleanup unified renderer
|
|
959
1671
|
if (this._unifiedRenderer) {
|
|
960
1672
|
this._unifiedRenderer.unmountAll();
|
|
961
1673
|
}
|
|
1674
|
+
this._virtualizer?.destroy();
|
|
1675
|
+
this._virtualizer = undefined;
|
|
962
1676
|
// Cleanup: remove listeners if any were added outside constructor
|
|
963
1677
|
if (this._typeTimeout)
|
|
964
1678
|
window.clearTimeout(this._typeTimeout);
|
|
1679
|
+
if (this._announceTimeout)
|
|
1680
|
+
window.clearTimeout(this._announceTimeout);
|
|
965
1681
|
}
|
|
966
1682
|
_initializeOptionRenderer() {
|
|
1683
|
+
this._unifiedRenderer?.unmountAll();
|
|
967
1684
|
const getValue = (item) => item?.value ?? item;
|
|
968
1685
|
const getLabel = (item) => item?.label ?? String(item);
|
|
969
1686
|
const getDisabled = (item) => item?.disabled ?? false;
|
|
@@ -979,39 +1696,43 @@
|
|
|
979
1696
|
},
|
|
980
1697
|
onCustomEvent: (index, eventName, data) => {
|
|
981
1698
|
// Emit custom events from option components
|
|
982
|
-
this.dispatchEvent(new CustomEvent(
|
|
1699
|
+
this.dispatchEvent(new CustomEvent("option:custom-event", {
|
|
983
1700
|
detail: { index, eventName, data },
|
|
984
1701
|
bubbles: true,
|
|
985
|
-
composed: true
|
|
1702
|
+
composed: true,
|
|
986
1703
|
}));
|
|
987
1704
|
},
|
|
988
1705
|
onError: (index, error) => {
|
|
989
1706
|
console.error(`[NativeSelect] Error in option ${index}:`, error);
|
|
990
|
-
this.dispatchEvent(new CustomEvent(
|
|
1707
|
+
this.dispatchEvent(new CustomEvent("option:mount-error", {
|
|
991
1708
|
detail: { index, error },
|
|
992
1709
|
bubbles: true,
|
|
993
|
-
composed: true
|
|
1710
|
+
composed: true,
|
|
994
1711
|
}));
|
|
995
|
-
}
|
|
1712
|
+
},
|
|
996
1713
|
};
|
|
997
1714
|
this._unifiedRenderer = new OptionRenderer(rendererConfig);
|
|
998
1715
|
}
|
|
999
1716
|
attributeChangedCallback(name, _oldValue, newValue) {
|
|
1000
1717
|
switch (name) {
|
|
1001
|
-
case
|
|
1002
|
-
this._options.placement = (newValue ??
|
|
1718
|
+
case "placement":
|
|
1719
|
+
this._options.placement = (newValue ??
|
|
1720
|
+
undefined);
|
|
1003
1721
|
break;
|
|
1004
|
-
case
|
|
1005
|
-
this._options.strategy = (newValue ??
|
|
1722
|
+
case "strategy":
|
|
1723
|
+
this._options.strategy = (newValue ??
|
|
1724
|
+
undefined);
|
|
1006
1725
|
break;
|
|
1007
|
-
case
|
|
1008
|
-
this._options.portal =
|
|
1726
|
+
case "portal":
|
|
1727
|
+
this._options.portal =
|
|
1728
|
+
newValue === "true" ? true : newValue === "false" ? false : undefined;
|
|
1009
1729
|
break;
|
|
1010
1730
|
}
|
|
1011
1731
|
}
|
|
1012
1732
|
set items(items) {
|
|
1013
1733
|
this._items = items ?? [];
|
|
1014
1734
|
// initialize virtualizer with estimated height (default 48) and buffer
|
|
1735
|
+
this._virtualizer?.destroy();
|
|
1015
1736
|
this._virtualizer = new Virtualizer(this._listRoot, this._items.length, (i) => this._items[i], { estimatedItemHeight: 48, buffer: 5 });
|
|
1016
1737
|
this.render();
|
|
1017
1738
|
}
|
|
@@ -1021,10 +1742,10 @@
|
|
|
1021
1742
|
set multi(value) {
|
|
1022
1743
|
this._multi = value;
|
|
1023
1744
|
if (value) {
|
|
1024
|
-
this._listRoot.setAttribute(
|
|
1745
|
+
this._listRoot.setAttribute("aria-multiselectable", "true");
|
|
1025
1746
|
}
|
|
1026
1747
|
else {
|
|
1027
|
-
this._listRoot.removeAttribute(
|
|
1748
|
+
this._listRoot.removeAttribute("aria-multiselectable");
|
|
1028
1749
|
}
|
|
1029
1750
|
}
|
|
1030
1751
|
get multi() {
|
|
@@ -1060,7 +1781,7 @@
|
|
|
1060
1781
|
* For multi-select: adds to selection if not already selected
|
|
1061
1782
|
*/
|
|
1062
1783
|
setValue(value) {
|
|
1063
|
-
if (value === null || value === undefined || value ===
|
|
1784
|
+
if (value === null || value === undefined || value === "") {
|
|
1064
1785
|
// Clear selection
|
|
1065
1786
|
this._selectedSet.clear();
|
|
1066
1787
|
this._selectedItems.clear();
|
|
@@ -1069,8 +1790,8 @@
|
|
|
1069
1790
|
return;
|
|
1070
1791
|
}
|
|
1071
1792
|
// Find item by value
|
|
1072
|
-
const index = this._items.findIndex(item => {
|
|
1073
|
-
if (typeof item ===
|
|
1793
|
+
const index = this._items.findIndex((item) => {
|
|
1794
|
+
if (typeof item === "object" && item !== null && "value" in item) {
|
|
1074
1795
|
return item.value === value;
|
|
1075
1796
|
}
|
|
1076
1797
|
return item === value;
|
|
@@ -1096,7 +1817,7 @@
|
|
|
1096
1817
|
// Optimized: single-pass iteration, no intermediate arrays
|
|
1097
1818
|
const values = [];
|
|
1098
1819
|
for (const item of this._selectedItems.values()) {
|
|
1099
|
-
if (typeof item ===
|
|
1820
|
+
if (typeof item === "object" && item !== null && "value" in item) {
|
|
1100
1821
|
values.push(item.value);
|
|
1101
1822
|
}
|
|
1102
1823
|
else {
|
|
@@ -1111,16 +1832,16 @@
|
|
|
1111
1832
|
const scrollTop = this.scrollTop || 0;
|
|
1112
1833
|
// Update aria-activedescendant
|
|
1113
1834
|
if (this._activeIndex >= 0) {
|
|
1114
|
-
this._listRoot.setAttribute(
|
|
1835
|
+
this._listRoot.setAttribute("aria-activedescendant", `option-${this._activeIndex}`);
|
|
1115
1836
|
}
|
|
1116
1837
|
else {
|
|
1117
|
-
this._listRoot.removeAttribute(
|
|
1838
|
+
this._listRoot.removeAttribute("aria-activedescendant");
|
|
1118
1839
|
}
|
|
1119
1840
|
// Check if any items have custom components
|
|
1120
|
-
const hasCustomComponents = this._items.some(item => typeof item ===
|
|
1841
|
+
const hasCustomComponents = this._items.some((item) => typeof item === "object" &&
|
|
1121
1842
|
item !== null &&
|
|
1122
|
-
Object.prototype.hasOwnProperty.call(item,
|
|
1123
|
-
typeof item.optionComponent ===
|
|
1843
|
+
Object.prototype.hasOwnProperty.call(item, "optionComponent") &&
|
|
1844
|
+
typeof item.optionComponent === "function");
|
|
1124
1845
|
// Use unified renderer if we have custom components
|
|
1125
1846
|
if (hasCustomComponents && this._unifiedRenderer) {
|
|
1126
1847
|
this._listRoot.replaceChildren(); // Clear existing content
|
|
@@ -1129,7 +1850,7 @@
|
|
|
1129
1850
|
const item = this._items[i];
|
|
1130
1851
|
const isSelected = this._selectedSet.has(i);
|
|
1131
1852
|
const isFocused = this._activeIndex === i;
|
|
1132
|
-
const optionElement = this._unifiedRenderer.render(item, i, isSelected, isFocused, `native-${this.getAttribute(
|
|
1853
|
+
const optionElement = this._unifiedRenderer.render(item, i, isSelected, isFocused, `native-${this.getAttribute("id") || "default"}`);
|
|
1133
1854
|
frag.appendChild(optionElement);
|
|
1134
1855
|
}
|
|
1135
1856
|
this._listRoot.appendChild(frag);
|
|
@@ -1146,14 +1867,14 @@
|
|
|
1146
1867
|
node.replaceChildren(el);
|
|
1147
1868
|
}
|
|
1148
1869
|
else if (optionTemplate) {
|
|
1149
|
-
const wrapper = document.createElement(
|
|
1870
|
+
const wrapper = document.createElement("div");
|
|
1150
1871
|
wrapper.innerHTML = optionTemplate(item, i);
|
|
1151
1872
|
const el = wrapper.firstElementChild;
|
|
1152
1873
|
node.replaceChildren(el ?? document.createTextNode(String(item)));
|
|
1153
1874
|
}
|
|
1154
1875
|
else {
|
|
1155
1876
|
// Handle {label, value} objects or primitives
|
|
1156
|
-
const displayText =
|
|
1877
|
+
const displayText = typeof item === "object" && item !== null && "label" in item
|
|
1157
1878
|
? String(item.label)
|
|
1158
1879
|
: String(item);
|
|
1159
1880
|
node.textContent = displayText;
|
|
@@ -1166,9 +1887,9 @@
|
|
|
1166
1887
|
const item = this._items[i];
|
|
1167
1888
|
if (optionRenderer) {
|
|
1168
1889
|
const el = optionRenderer(item, i, this._helpers);
|
|
1169
|
-
if (!el.hasAttribute(
|
|
1170
|
-
el.setAttribute(
|
|
1171
|
-
el.setAttribute(
|
|
1890
|
+
if (!el.hasAttribute("data-selectable")) {
|
|
1891
|
+
el.setAttribute("data-selectable", "");
|
|
1892
|
+
el.setAttribute("data-index", String(i));
|
|
1172
1893
|
}
|
|
1173
1894
|
this._applyOptionAttrs(el, i);
|
|
1174
1895
|
frag.appendChild(el);
|
|
@@ -1181,14 +1902,14 @@
|
|
|
1181
1902
|
return; // rendering complete
|
|
1182
1903
|
}
|
|
1183
1904
|
else {
|
|
1184
|
-
const el = document.createElement(
|
|
1905
|
+
const el = document.createElement("div");
|
|
1185
1906
|
// Handle {label, value} objects or primitives
|
|
1186
|
-
const displayText =
|
|
1907
|
+
const displayText = typeof item === "object" && item !== null && "label" in item
|
|
1187
1908
|
? String(item.label)
|
|
1188
1909
|
: String(item);
|
|
1189
1910
|
el.textContent = displayText;
|
|
1190
|
-
el.setAttribute(
|
|
1191
|
-
el.setAttribute(
|
|
1911
|
+
el.setAttribute("data-selectable", "");
|
|
1912
|
+
el.setAttribute("data-index", String(i));
|
|
1192
1913
|
this._applyOptionAttrs(el, i);
|
|
1193
1914
|
frag.appendChild(el);
|
|
1194
1915
|
}
|
|
@@ -1196,13 +1917,13 @@
|
|
|
1196
1917
|
this._listRoot.replaceChildren(frag);
|
|
1197
1918
|
}
|
|
1198
1919
|
_applyOptionAttrs(node, index) {
|
|
1199
|
-
node.setAttribute(
|
|
1920
|
+
node.setAttribute("role", "option");
|
|
1200
1921
|
node.id = `option-${index}`;
|
|
1201
1922
|
if (this._selectedSet.has(index)) {
|
|
1202
|
-
node.setAttribute(
|
|
1923
|
+
node.setAttribute("aria-selected", "true");
|
|
1203
1924
|
}
|
|
1204
1925
|
else {
|
|
1205
|
-
node.setAttribute(
|
|
1926
|
+
node.setAttribute("aria-selected", "false");
|
|
1206
1927
|
}
|
|
1207
1928
|
}
|
|
1208
1929
|
_applyAriaToAll() {
|
|
@@ -1240,13 +1961,13 @@
|
|
|
1240
1961
|
const selected = this._selectedSet.has(index);
|
|
1241
1962
|
const value = item?.value ?? item;
|
|
1242
1963
|
const label = item?.label ?? String(item);
|
|
1243
|
-
this._emit(
|
|
1964
|
+
this._emit("select", {
|
|
1244
1965
|
item,
|
|
1245
1966
|
index,
|
|
1246
1967
|
value,
|
|
1247
1968
|
label,
|
|
1248
1969
|
selected,
|
|
1249
|
-
multi: this._multi
|
|
1970
|
+
multi: this._multi,
|
|
1250
1971
|
});
|
|
1251
1972
|
// Optimized: single-pass iteration for change event data
|
|
1252
1973
|
const selectedItems = [];
|
|
@@ -1256,50 +1977,50 @@
|
|
|
1256
1977
|
selectedValues.push(item?.value ?? item);
|
|
1257
1978
|
}
|
|
1258
1979
|
// Emit 'change' event for better React compatibility
|
|
1259
|
-
this._emit(
|
|
1980
|
+
this._emit("change", {
|
|
1260
1981
|
selectedItems,
|
|
1261
1982
|
selectedValues,
|
|
1262
|
-
selectedIndices: Array.from(this._selectedSet)
|
|
1983
|
+
selectedIndices: Array.from(this._selectedSet),
|
|
1263
1984
|
});
|
|
1264
1985
|
this._announce(`Selected ${label}`);
|
|
1265
1986
|
}
|
|
1266
1987
|
_onKeydown(e) {
|
|
1267
1988
|
switch (e.key) {
|
|
1268
|
-
case
|
|
1989
|
+
case "ArrowDown":
|
|
1269
1990
|
e.preventDefault();
|
|
1270
1991
|
this._moveActive(1);
|
|
1271
1992
|
break;
|
|
1272
|
-
case
|
|
1993
|
+
case "ArrowUp":
|
|
1273
1994
|
e.preventDefault();
|
|
1274
1995
|
this._moveActive(-1);
|
|
1275
1996
|
break;
|
|
1276
|
-
case
|
|
1997
|
+
case "Home":
|
|
1277
1998
|
e.preventDefault();
|
|
1278
1999
|
this._setActive(0);
|
|
1279
2000
|
break;
|
|
1280
|
-
case
|
|
2001
|
+
case "End":
|
|
1281
2002
|
e.preventDefault();
|
|
1282
2003
|
this._setActive(this._items.length - 1);
|
|
1283
2004
|
break;
|
|
1284
|
-
case
|
|
2005
|
+
case "PageDown":
|
|
1285
2006
|
e.preventDefault();
|
|
1286
2007
|
this._moveActive(10);
|
|
1287
2008
|
break;
|
|
1288
|
-
case
|
|
2009
|
+
case "PageUp":
|
|
1289
2010
|
e.preventDefault();
|
|
1290
2011
|
this._moveActive(-10);
|
|
1291
2012
|
break;
|
|
1292
|
-
case
|
|
1293
|
-
case
|
|
2013
|
+
case "Enter":
|
|
2014
|
+
case " ":
|
|
1294
2015
|
e.preventDefault();
|
|
1295
2016
|
if (this._activeIndex >= 0) {
|
|
1296
2017
|
const item = this._items[this._activeIndex];
|
|
1297
2018
|
this._onSelect(item, this._activeIndex);
|
|
1298
2019
|
}
|
|
1299
2020
|
break;
|
|
1300
|
-
case
|
|
2021
|
+
case "Escape":
|
|
1301
2022
|
e.preventDefault();
|
|
1302
|
-
this._emit(
|
|
2023
|
+
this._emit("close", {});
|
|
1303
2024
|
break;
|
|
1304
2025
|
default:
|
|
1305
2026
|
// Type-ahead buffer
|
|
@@ -1321,14 +2042,14 @@
|
|
|
1321
2042
|
}
|
|
1322
2043
|
_scrollToActive() {
|
|
1323
2044
|
const el = this._shadow.getElementById(`option-${this._activeIndex}`);
|
|
1324
|
-
el?.scrollIntoView({ block:
|
|
2045
|
+
el?.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
1325
2046
|
}
|
|
1326
2047
|
_onType(char) {
|
|
1327
2048
|
if (this._typeTimeout)
|
|
1328
2049
|
window.clearTimeout(this._typeTimeout);
|
|
1329
2050
|
this._typeBuffer += char.toLowerCase();
|
|
1330
2051
|
this._typeTimeout = window.setTimeout(() => {
|
|
1331
|
-
this._typeBuffer =
|
|
2052
|
+
this._typeBuffer = "";
|
|
1332
2053
|
}, 400);
|
|
1333
2054
|
// Find first matching item
|
|
1334
2055
|
const match = this._items.findIndex((item) => String(item).toLowerCase().startsWith(this._typeBuffer));
|
|
@@ -1339,9 +2060,12 @@
|
|
|
1339
2060
|
_announce(msg) {
|
|
1340
2061
|
if (this._liveRegion) {
|
|
1341
2062
|
this._liveRegion.textContent = msg;
|
|
1342
|
-
|
|
2063
|
+
if (this._announceTimeout)
|
|
2064
|
+
window.clearTimeout(this._announceTimeout);
|
|
2065
|
+
this._announceTimeout = window.setTimeout(() => {
|
|
2066
|
+
this._announceTimeout = undefined;
|
|
1343
2067
|
if (this._liveRegion)
|
|
1344
|
-
this._liveRegion.textContent =
|
|
2068
|
+
this._liveRegion.textContent = "";
|
|
1345
2069
|
}, 1000);
|
|
1346
2070
|
}
|
|
1347
2071
|
}
|
|
@@ -1350,7 +2074,9 @@
|
|
|
1350
2074
|
this._listRoot.focus();
|
|
1351
2075
|
}
|
|
1352
2076
|
}
|
|
1353
|
-
customElements.
|
|
2077
|
+
if (!customElements.get("smilodon-select")) {
|
|
2078
|
+
customElements.define("smilodon-select", NativeSelectElement);
|
|
2079
|
+
}
|
|
1354
2080
|
|
|
1355
2081
|
/**
|
|
1356
2082
|
* Global Configuration System for Select Components
|
|
@@ -1527,6 +2253,222 @@
|
|
|
1527
2253
|
selectConfig.resetConfig();
|
|
1528
2254
|
}
|
|
1529
2255
|
|
|
2256
|
+
/** Returns whether a selected key remains in the filtered result set. */
|
|
2257
|
+
/**
|
|
2258
|
+
* Returns keys in the half-open index range [startIndex, endIndex), clamped to
|
|
2259
|
+
* the source. Bounds are truncated to integers; an end before start is empty.
|
|
2260
|
+
*/
|
|
2261
|
+
function computeWindowKeys(filteredKeys, startIndex, endIndex) {
|
|
2262
|
+
const start = Math.max(0, Math.min(filteredKeys.length, Math.trunc(startIndex)));
|
|
2263
|
+
const end = Math.max(start, Math.min(filteredKeys.length, Math.trunc(endIndex)));
|
|
2264
|
+
return filteredKeys.slice(start, end);
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
/** Projects existing engine state into accessibility semantics without DOM data. */
|
|
2268
|
+
function deriveAccessibilityState(state, context = {}) {
|
|
2269
|
+
const required = state.selection.required;
|
|
2270
|
+
const invalid = context.invalid ?? false;
|
|
2271
|
+
return {
|
|
2272
|
+
expanded: state.popup === 'open',
|
|
2273
|
+
disabled: state.disabled,
|
|
2274
|
+
readonly: context.readonly ?? false,
|
|
2275
|
+
searchable: context.searchable ?? false,
|
|
2276
|
+
multiple: state.selection.multiple,
|
|
2277
|
+
required,
|
|
2278
|
+
invalid,
|
|
2279
|
+
busy: state.dataStatus === 'loading',
|
|
2280
|
+
activeKey: state.activeKey,
|
|
2281
|
+
selectedKeys: state.selectedKeys,
|
|
2282
|
+
disabledKeys: state.selection.disabledKeys,
|
|
2283
|
+
popupRole: 'listbox',
|
|
2284
|
+
label: context.label ?? {},
|
|
2285
|
+
validation: {
|
|
2286
|
+
required,
|
|
2287
|
+
invalid,
|
|
2288
|
+
errorMessage: context.errorMessage ?? null,
|
|
2289
|
+
errorDescriptionId: context.errorDescriptionId ?? null,
|
|
2290
|
+
},
|
|
2291
|
+
};
|
|
2292
|
+
}
|
|
2293
|
+
function uniqueIds(ids) {
|
|
2294
|
+
return [...new Set(ids.filter((id) => Boolean(id)))];
|
|
2295
|
+
}
|
|
2296
|
+
function getLabelledBy(label) {
|
|
2297
|
+
return uniqueIds([label.labelId, ...(label.labelledBy ?? [])]);
|
|
2298
|
+
}
|
|
2299
|
+
function getDescribedBy(state) {
|
|
2300
|
+
return uniqueIds([
|
|
2301
|
+
...(state.label.describedBy ?? []),
|
|
2302
|
+
state.validation.errorDescriptionId,
|
|
2303
|
+
]);
|
|
2304
|
+
}
|
|
2305
|
+
/** Computes combobox semantics without reading from or writing to the DOM. */
|
|
2306
|
+
function computeComboboxAccessibility(state, controlsId = null) {
|
|
2307
|
+
return {
|
|
2308
|
+
role: 'combobox',
|
|
2309
|
+
expanded: state.expanded,
|
|
2310
|
+
disabled: state.disabled,
|
|
2311
|
+
readonly: state.readonly,
|
|
2312
|
+
autocomplete: state.searchable ? 'list' : 'none',
|
|
2313
|
+
multiple: state.multiple,
|
|
2314
|
+
required: state.required,
|
|
2315
|
+
invalid: state.invalid,
|
|
2316
|
+
activeKey: state.activeKey,
|
|
2317
|
+
controlsId,
|
|
2318
|
+
labelledBy: getLabelledBy(state.label),
|
|
2319
|
+
describedBy: getDescribedBy(state),
|
|
2320
|
+
ariaLabel: state.label.ariaLabel ?? null,
|
|
2321
|
+
ariaDescription: state.label.ariaDescription ?? null,
|
|
2322
|
+
};
|
|
2323
|
+
}
|
|
2324
|
+
/** Computes listbox semantics; the supplied label IDs remain caller-owned data. */
|
|
2325
|
+
function computeListboxAccessibility(state) {
|
|
2326
|
+
return {
|
|
2327
|
+
role: state.popupRole,
|
|
2328
|
+
multiple: state.multiple,
|
|
2329
|
+
busy: state.busy,
|
|
2330
|
+
labelledBy: getLabelledBy(state.label),
|
|
2331
|
+
ariaLabel: state.label.ariaLabel ?? null,
|
|
2332
|
+
};
|
|
2333
|
+
}
|
|
2334
|
+
/** Computes one option's semantic state from stable identity, never its index. */
|
|
2335
|
+
function computeOptionAccessibility(state, key) {
|
|
2336
|
+
const disabled = state.disabled || state.disabledKeys.has(key);
|
|
2337
|
+
return {
|
|
2338
|
+
key,
|
|
2339
|
+
selected: state.selectedKeys.has(key),
|
|
2340
|
+
disabled,
|
|
2341
|
+
active: !disabled && state.activeKey === key,
|
|
2342
|
+
};
|
|
2343
|
+
}
|
|
2344
|
+
/**
|
|
2345
|
+
* Produces semantic accessibility effects. IDs and rendered option keys are
|
|
2346
|
+
* caller inputs; this function does not inspect the DOM or perform side effects.
|
|
2347
|
+
*/
|
|
2348
|
+
function computeAccessibilityEffects(state, options) {
|
|
2349
|
+
return [
|
|
2350
|
+
{
|
|
2351
|
+
type: 'SYNC_COMBOBOX_ARIA',
|
|
2352
|
+
...computeComboboxAccessibility(state, options.controlsId ?? null),
|
|
2353
|
+
},
|
|
2354
|
+
{
|
|
2355
|
+
type: 'SYNC_LISTBOX_ARIA',
|
|
2356
|
+
...computeListboxAccessibility(state),
|
|
2357
|
+
},
|
|
2358
|
+
...options.optionKeys.map((key) => ({
|
|
2359
|
+
type: 'SYNC_OPTION_ARIA',
|
|
2360
|
+
...computeOptionAccessibility(state, key),
|
|
2361
|
+
})),
|
|
2362
|
+
...(options.announcements ?? []).map((announcement) => ({
|
|
2363
|
+
type: 'ANNOUNCE',
|
|
2364
|
+
...announcement,
|
|
2365
|
+
})),
|
|
2366
|
+
...(options.focusIntents ?? []).map((focusIntent) => ({
|
|
2367
|
+
type: 'FOCUS_INTENT',
|
|
2368
|
+
...focusIntent,
|
|
2369
|
+
})),
|
|
2370
|
+
];
|
|
2371
|
+
}
|
|
2372
|
+
|
|
2373
|
+
function setBooleanAttribute(element, name, value) {
|
|
2374
|
+
element.setAttribute(name, String(value));
|
|
2375
|
+
}
|
|
2376
|
+
function setOptionalAttribute(element, name, value) {
|
|
2377
|
+
if (value) {
|
|
2378
|
+
element.setAttribute(name, value);
|
|
2379
|
+
}
|
|
2380
|
+
else {
|
|
2381
|
+
element.removeAttribute(name);
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
function setIdListAttribute(element, name, ids) {
|
|
2385
|
+
setOptionalAttribute(element, name, ids.length > 0 ? ids.join(' ') : null);
|
|
2386
|
+
}
|
|
2387
|
+
function applyCombobox(effect, targets) {
|
|
2388
|
+
const combobox = targets.combobox;
|
|
2389
|
+
if (!combobox)
|
|
2390
|
+
return;
|
|
2391
|
+
combobox.setAttribute('role', effect.role);
|
|
2392
|
+
setBooleanAttribute(combobox, 'aria-expanded', effect.expanded);
|
|
2393
|
+
setBooleanAttribute(combobox, 'aria-disabled', effect.disabled);
|
|
2394
|
+
setBooleanAttribute(combobox, 'aria-readonly', effect.readonly);
|
|
2395
|
+
setBooleanAttribute(combobox, 'aria-required', effect.required);
|
|
2396
|
+
setBooleanAttribute(combobox, 'aria-invalid', effect.invalid);
|
|
2397
|
+
combobox.setAttribute('aria-autocomplete', effect.autocomplete);
|
|
2398
|
+
setOptionalAttribute(combobox, 'aria-controls', effect.controlsId);
|
|
2399
|
+
setIdListAttribute(combobox, 'aria-labelledby', effect.labelledBy);
|
|
2400
|
+
setIdListAttribute(combobox, 'aria-describedby', effect.describedBy);
|
|
2401
|
+
setOptionalAttribute(combobox, 'aria-label', effect.ariaLabel);
|
|
2402
|
+
setOptionalAttribute(combobox, 'aria-description', effect.ariaDescription);
|
|
2403
|
+
const activeOption = effect.activeKey == null
|
|
2404
|
+
? null
|
|
2405
|
+
: targets.getOptionByKey?.(effect.activeKey) ?? null;
|
|
2406
|
+
setOptionalAttribute(combobox, 'aria-activedescendant', activeOption?.id || null);
|
|
2407
|
+
}
|
|
2408
|
+
function applyListbox(effect, targets) {
|
|
2409
|
+
const listbox = targets.listbox;
|
|
2410
|
+
if (!listbox)
|
|
2411
|
+
return;
|
|
2412
|
+
listbox.setAttribute('role', effect.role);
|
|
2413
|
+
if (effect.multiple)
|
|
2414
|
+
listbox.setAttribute('aria-multiselectable', 'true');
|
|
2415
|
+
else
|
|
2416
|
+
listbox.removeAttribute('aria-multiselectable');
|
|
2417
|
+
if (effect.busy)
|
|
2418
|
+
listbox.setAttribute('aria-busy', 'true');
|
|
2419
|
+
else
|
|
2420
|
+
listbox.removeAttribute('aria-busy');
|
|
2421
|
+
setIdListAttribute(listbox, 'aria-labelledby', effect.labelledBy);
|
|
2422
|
+
setOptionalAttribute(listbox, 'aria-label', effect.ariaLabel);
|
|
2423
|
+
}
|
|
2424
|
+
function applyOption(effect, targets) {
|
|
2425
|
+
const option = targets.getOptionByKey?.(effect.key);
|
|
2426
|
+
if (!option)
|
|
2427
|
+
return;
|
|
2428
|
+
option.setAttribute('role', 'option');
|
|
2429
|
+
setBooleanAttribute(option, 'aria-selected', effect.selected);
|
|
2430
|
+
if (effect.disabled)
|
|
2431
|
+
option.setAttribute('aria-disabled', 'true');
|
|
2432
|
+
else
|
|
2433
|
+
option.removeAttribute('aria-disabled');
|
|
2434
|
+
}
|
|
2435
|
+
function applyFocusIntent(target, targets, options) {
|
|
2436
|
+
if (!options.applyFocus || target === 'none')
|
|
2437
|
+
return;
|
|
2438
|
+
const focusTarget = target === 'input'
|
|
2439
|
+
? targets.focusTargets?.input
|
|
2440
|
+
: target === 'clear-button'
|
|
2441
|
+
? targets.focusTargets?.clearButton
|
|
2442
|
+
: targets.focusTargets?.getActiveOption?.();
|
|
2443
|
+
focusTarget?.focus();
|
|
2444
|
+
}
|
|
2445
|
+
/** Applies engine accessibility effects to caller-owned web DOM targets. */
|
|
2446
|
+
function applyAccessibilityEffects(effects, targets, options = {}) {
|
|
2447
|
+
for (const effect of effects) {
|
|
2448
|
+
switch (effect.type) {
|
|
2449
|
+
case 'SYNC_COMBOBOX_ARIA':
|
|
2450
|
+
applyCombobox(effect, targets);
|
|
2451
|
+
break;
|
|
2452
|
+
case 'SYNC_LISTBOX_ARIA':
|
|
2453
|
+
applyListbox(effect, targets);
|
|
2454
|
+
break;
|
|
2455
|
+
case 'SYNC_OPTION_ARIA':
|
|
2456
|
+
applyOption(effect, targets);
|
|
2457
|
+
break;
|
|
2458
|
+
case 'ANNOUNCE':
|
|
2459
|
+
if (targets.liveRegion) {
|
|
2460
|
+
targets.liveRegion.setAttribute('aria-live', effect.politeness);
|
|
2461
|
+
targets.liveRegion.setAttribute('aria-atomic', 'true');
|
|
2462
|
+
targets.liveRegion.textContent = effect.message;
|
|
2463
|
+
}
|
|
2464
|
+
break;
|
|
2465
|
+
case 'FOCUS_INTENT':
|
|
2466
|
+
applyFocusIntent(effect.target, targets, options);
|
|
2467
|
+
break;
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
|
|
1530
2472
|
/**
|
|
1531
2473
|
* Independent Option Component
|
|
1532
2474
|
* High cohesion, low coupling - handles its own selection state and events
|
|
@@ -2117,13 +3059,19 @@
|
|
|
2117
3059
|
* Implements all advanced features: infinite scroll, load more, busy state,
|
|
2118
3060
|
* server-side selection, and full customization
|
|
2119
3061
|
*
|
|
2120
|
-
*
|
|
3062
|
+
* Redesigned with formal elegance, refined microinteractions, and polished UX
|
|
2121
3063
|
*/
|
|
2122
3064
|
class EnhancedSelect extends HTMLElement {
|
|
3065
|
+
static get observedAttributes() {
|
|
3066
|
+
return ['aria-label', 'aria-description'];
|
|
3067
|
+
}
|
|
2123
3068
|
get classMap() {
|
|
2124
3069
|
return this._classMap;
|
|
2125
3070
|
}
|
|
2126
3071
|
set classMap(map) {
|
|
3072
|
+
if (this._areClassMapsEqual(this._classMap, map))
|
|
3073
|
+
return;
|
|
3074
|
+
this._invalidateGuardedMeasurements();
|
|
2127
3075
|
this._classMap = map;
|
|
2128
3076
|
this._setGlobalStylesMirroring(Boolean(this._optionRenderer || map || this._groupHeaderRenderer));
|
|
2129
3077
|
this._track('style', 'classMapChanged', {
|
|
@@ -2143,6 +3091,8 @@
|
|
|
2143
3091
|
return this._groupHeaderRenderer;
|
|
2144
3092
|
}
|
|
2145
3093
|
set groupHeaderRenderer(renderer) {
|
|
3094
|
+
if (this._groupHeaderRenderer === renderer)
|
|
3095
|
+
return;
|
|
2146
3096
|
this._groupHeaderRenderer = renderer;
|
|
2147
3097
|
this._setGlobalStylesMirroring(Boolean(this._optionRenderer || this._classMap || renderer));
|
|
2148
3098
|
this._track('style', 'groupHeaderRendererChanged', { enabled: Boolean(renderer) });
|
|
@@ -2152,6 +3102,48 @@
|
|
|
2152
3102
|
}
|
|
2153
3103
|
constructor() {
|
|
2154
3104
|
super();
|
|
3105
|
+
this._engine = new SelectEngineBridge();
|
|
3106
|
+
this._virtualizationScope = {
|
|
3107
|
+
dataKeys: [],
|
|
3108
|
+
filteredKeys: [],
|
|
3109
|
+
registeredKeys: [],
|
|
3110
|
+
windowKeys: [],
|
|
3111
|
+
mountedKeys: [],
|
|
3112
|
+
selectedKeys: new Set(),
|
|
3113
|
+
activeKey: null,
|
|
3114
|
+
};
|
|
3115
|
+
this._renderPlan = {
|
|
3116
|
+
mode: 'legacy-full',
|
|
3117
|
+
filteredIndices: [],
|
|
3118
|
+
filteredKeys: [],
|
|
3119
|
+
intendedRenderKeys: [],
|
|
3120
|
+
filteredIndexByKey: new Map(),
|
|
3121
|
+
};
|
|
3122
|
+
// Null keeps the public/default renderer on its legacy full path. This
|
|
3123
|
+
// private state is only used by guarded virtualization integration.
|
|
3124
|
+
this._guardedVirtualWindow = null;
|
|
3125
|
+
this._guardedVirtualizer = null;
|
|
3126
|
+
this._guardedVirtualizationRequested = false;
|
|
3127
|
+
this._guardedVirtualizerKeys = [];
|
|
3128
|
+
this._guardedMeasurementQuery = null;
|
|
3129
|
+
this._guardedOptionResizeObserver = null;
|
|
3130
|
+
this._guardedObservedOptions = new Set();
|
|
3131
|
+
this._guardedObserverSeenOptions = new WeakSet();
|
|
3132
|
+
this._guardedOptionMeasurements = new WeakMap();
|
|
3133
|
+
this._guardedMeasurementGeneration = 0;
|
|
3134
|
+
this._guardedVirtualScrollTop = 0;
|
|
3135
|
+
this._guardedVirtualViewportHeight = 0;
|
|
3136
|
+
this._guardedVirtualOverscan = 0;
|
|
3137
|
+
this._guardedVirtualWindowRafId = null;
|
|
3138
|
+
this._guardedScrollListenerAttached = false;
|
|
3139
|
+
this._onGuardedVirtualScroll = () => this._scheduleGuardedVirtualWindowUpdate();
|
|
3140
|
+
this._effectRunner = new DefaultSelectEffectRunner();
|
|
3141
|
+
this._itemAdapter = this._createEngineItemAdapter();
|
|
3142
|
+
this._itemRegistry = createItemRegistry([], this._itemAdapter);
|
|
3143
|
+
// Engine selectedKeys owns identity. The registry resolves current records;
|
|
3144
|
+
// this cache keeps the last known record available while a selected key is detached.
|
|
3145
|
+
this._selectedRecordsByKey = new Map();
|
|
3146
|
+
this._registryItemsRef = [];
|
|
2155
3147
|
this._pageCache = {};
|
|
2156
3148
|
this._typeBuffer = '';
|
|
2157
3149
|
this._hasError = false;
|
|
@@ -2161,11 +3153,17 @@
|
|
|
2161
3153
|
this._pendingSearchRenderMark = false;
|
|
2162
3154
|
this._rangeAnchorIndex = null;
|
|
2163
3155
|
this._customOptionBoundElements = new WeakSet();
|
|
3156
|
+
this._customOptionNormalizedIds = new WeakMap();
|
|
3157
|
+
this._customOptionNormalizedLabels = new WeakMap();
|
|
2164
3158
|
this._mirrorGlobalStylesForCustomOptions = false;
|
|
2165
3159
|
this._globalStylesObserver = null;
|
|
2166
3160
|
this._globalStylesContainer = null;
|
|
2167
3161
|
this._themeContextObserver = null;
|
|
2168
3162
|
this._globalStylesSyncToken = 0;
|
|
3163
|
+
this._asyncGeneration = 0;
|
|
3164
|
+
this._lifecycleTimers = new Set();
|
|
3165
|
+
this._lifecycleRafs = new Set();
|
|
3166
|
+
this._keyboardActiveChangeGeneration = 0;
|
|
2169
3167
|
this._tracking = { events: [], styles: [], limitations: [] };
|
|
2170
3168
|
this._suppressBlurClose = false;
|
|
2171
3169
|
this._renderCycleId = 0;
|
|
@@ -2179,14 +3177,54 @@
|
|
|
2179
3177
|
startScrollLeft: 0,
|
|
2180
3178
|
};
|
|
2181
3179
|
this._liftedAncestors = [];
|
|
3180
|
+
this._onDocumentPointerDown = (e) => {
|
|
3181
|
+
const path = (e.composedPath && e.composedPath()) || [];
|
|
3182
|
+
let clickedInside = false;
|
|
3183
|
+
for (const node of path) {
|
|
3184
|
+
if (node === this || node === this._container) {
|
|
3185
|
+
clickedInside = true;
|
|
3186
|
+
break;
|
|
3187
|
+
}
|
|
3188
|
+
if (node instanceof Node) {
|
|
3189
|
+
try {
|
|
3190
|
+
if (this._shadow && this._shadow.contains(node)) {
|
|
3191
|
+
clickedInside = true;
|
|
3192
|
+
break;
|
|
3193
|
+
}
|
|
3194
|
+
}
|
|
3195
|
+
catch (err) {
|
|
3196
|
+
// ignore
|
|
3197
|
+
}
|
|
3198
|
+
}
|
|
3199
|
+
if (node instanceof Element) {
|
|
3200
|
+
try {
|
|
3201
|
+
if (node.matches('[data-sm-selectable], [data-selectable], [data-sm-state], .input-container, .select-container, .dropdown-arrow-container, .clear-control-button')) {
|
|
3202
|
+
clickedInside = true;
|
|
3203
|
+
break;
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
catch (err) {
|
|
3207
|
+
// ignore
|
|
3208
|
+
}
|
|
3209
|
+
}
|
|
3210
|
+
}
|
|
3211
|
+
if (!clickedInside) {
|
|
3212
|
+
this._handleClose('outside-pointer');
|
|
3213
|
+
}
|
|
3214
|
+
};
|
|
2182
3215
|
this._shadow = this.attachShadow({ mode: 'open' });
|
|
2183
3216
|
this._uniqueId = `enhanced-select-${Math.random().toString(36).substr(2, 9)}`;
|
|
2184
3217
|
this._rendererHelpers = this._buildRendererHelpers();
|
|
2185
3218
|
// Merge global config with component-level config
|
|
2186
3219
|
this._config = selectConfig.getConfig();
|
|
3220
|
+
this._engine.dispatch({ type: 'DISABLED_CHANGED', disabled: !this._config.enabled });
|
|
3221
|
+
this._syncEngineSelectionConfig();
|
|
2187
3222
|
// Initialize state
|
|
3223
|
+
const host = this;
|
|
2188
3224
|
this._state = {
|
|
2189
|
-
isOpen
|
|
3225
|
+
get isOpen() {
|
|
3226
|
+
return host._engine.state.popup === 'open';
|
|
3227
|
+
},
|
|
2190
3228
|
isBusy: false,
|
|
2191
3229
|
isSearching: false,
|
|
2192
3230
|
currentPage: this._config.infiniteScroll.initialPage || 1,
|
|
@@ -2213,6 +3251,7 @@
|
|
|
2213
3251
|
this._dropdown = this._createDropdown();
|
|
2214
3252
|
this._optionsContainer = this._createOptionsContainer();
|
|
2215
3253
|
this._liveRegion = this._createLiveRegion();
|
|
3254
|
+
this._errorDescription = this._createErrorDescription();
|
|
2216
3255
|
// Initialize styles BEFORE assembling DOM (order matters in shadow DOM)
|
|
2217
3256
|
this._initializeStyles();
|
|
2218
3257
|
this._syncStyleConfigVariables();
|
|
@@ -2221,17 +3260,27 @@
|
|
|
2221
3260
|
this._assembleDOM();
|
|
2222
3261
|
this._attachEventListeners();
|
|
2223
3262
|
this._initializeObservers();
|
|
3263
|
+
this._syncComboboxAccessibility();
|
|
3264
|
+
this._syncListboxAccessibility();
|
|
2224
3265
|
}
|
|
2225
3266
|
connectedCallback() {
|
|
2226
3267
|
// register instance
|
|
2227
3268
|
EnhancedSelect._instances.add(this);
|
|
3269
|
+
this._runEngineEffects(this._engine.dispatch({ type: 'CONNECT' }));
|
|
3270
|
+
this._runEngineEffects(this._engine.dispatch({ type: 'DISABLED_CHANGED', disabled: !this._config.enabled }));
|
|
3271
|
+
this._connectionController?.abort();
|
|
3272
|
+
this._connectionController = new AbortController();
|
|
3273
|
+
document.addEventListener('pointerdown', this._onDocumentPointerDown, {
|
|
3274
|
+
signal: this._connectionController.signal,
|
|
3275
|
+
});
|
|
2228
3276
|
// WORKAROUND: Force display style on host element for Angular compatibility
|
|
2229
3277
|
// Angular's rendering seems to not apply :host styles correctly in some cases
|
|
2230
3278
|
// Must be done in connectedCallback when element is attached to DOM
|
|
2231
3279
|
this.style.display = 'block';
|
|
2232
3280
|
// Sync direction attribute (must be in connectedCallback, not constructor)
|
|
2233
3281
|
this._syncDirectionConfig();
|
|
2234
|
-
|
|
3282
|
+
this._syncComboboxAccessibility();
|
|
3283
|
+
if (this._optionRenderer || this._classMap || this._groupHeaderRenderer) {
|
|
2235
3284
|
this._setGlobalStylesMirroring(true);
|
|
2236
3285
|
}
|
|
2237
3286
|
// Load initial data if server-side is enabled
|
|
@@ -2249,9 +3298,33 @@
|
|
|
2249
3298
|
this._config.callbacks.onOpen();
|
|
2250
3299
|
}
|
|
2251
3300
|
}
|
|
3301
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
3302
|
+
if (oldValue === newValue || (name !== 'aria-label' && name !== 'aria-description'))
|
|
3303
|
+
return;
|
|
3304
|
+
this._syncComboboxAccessibility();
|
|
3305
|
+
if (name === 'aria-label')
|
|
3306
|
+
this._syncListboxAccessibility();
|
|
3307
|
+
}
|
|
2252
3308
|
disconnectedCallback() {
|
|
2253
3309
|
// unregister instance
|
|
2254
3310
|
EnhancedSelect._instances.delete(this);
|
|
3311
|
+
if (this._state.isOpen) {
|
|
3312
|
+
this._handleClose('disconnect');
|
|
3313
|
+
}
|
|
3314
|
+
this._runEngineEffects(this._engine.dispatch({ type: 'DISCONNECT' }));
|
|
3315
|
+
this._connectionController?.abort();
|
|
3316
|
+
this._connectionController = undefined;
|
|
3317
|
+
this._asyncGeneration += 1;
|
|
3318
|
+
this._cancelGuardedVirtualWindowUpdate();
|
|
3319
|
+
this._clearLifecycleTasks();
|
|
3320
|
+
this._destroyGuardedVirtualizer(true);
|
|
3321
|
+
this._virtualizationScope = {
|
|
3322
|
+
...this._virtualizationScope,
|
|
3323
|
+
mountedKeys: [],
|
|
3324
|
+
};
|
|
3325
|
+
Array.from(this._optionsContainer.children)
|
|
3326
|
+
.filter((element) => element.hasAttribute('data-index') || element.hasAttribute('data-virtual-spacer'))
|
|
3327
|
+
.forEach((element) => element.remove());
|
|
2255
3328
|
// Cleanup observers
|
|
2256
3329
|
this._resizeObserver?.disconnect();
|
|
2257
3330
|
this._intersectionObserver?.disconnect();
|
|
@@ -2271,8 +3344,8 @@
|
|
|
2271
3344
|
}
|
|
2272
3345
|
_setGlobalStylesMirroring(enabled) {
|
|
2273
3346
|
if (this._mirrorGlobalStylesForCustomOptions === enabled) {
|
|
2274
|
-
if (enabled) {
|
|
2275
|
-
this.
|
|
3347
|
+
if (enabled && this.isConnected && !this._globalStylesObserver) {
|
|
3348
|
+
this._setupGlobalStylesMirroring();
|
|
2276
3349
|
}
|
|
2277
3350
|
return;
|
|
2278
3351
|
}
|
|
@@ -2285,6 +3358,36 @@
|
|
|
2285
3358
|
this._teardownGlobalStylesMirroring();
|
|
2286
3359
|
}
|
|
2287
3360
|
}
|
|
3361
|
+
_setLifecycleTimeout(callback, delay = 0) {
|
|
3362
|
+
const id = window.setTimeout(() => {
|
|
3363
|
+
this._lifecycleTimers.delete(id);
|
|
3364
|
+
if (!this.isConnected)
|
|
3365
|
+
return;
|
|
3366
|
+
callback();
|
|
3367
|
+
}, delay);
|
|
3368
|
+
this._lifecycleTimers.add(id);
|
|
3369
|
+
return id;
|
|
3370
|
+
}
|
|
3371
|
+
_requestLifecycleFrame(callback) {
|
|
3372
|
+
const id = requestAnimationFrame(() => {
|
|
3373
|
+
this._lifecycleRafs.delete(id);
|
|
3374
|
+
if (!this.isConnected)
|
|
3375
|
+
return;
|
|
3376
|
+
callback();
|
|
3377
|
+
});
|
|
3378
|
+
this._lifecycleRafs.add(id);
|
|
3379
|
+
return id;
|
|
3380
|
+
}
|
|
3381
|
+
_clearLifecycleTasks() {
|
|
3382
|
+
for (const id of this._lifecycleTimers) {
|
|
3383
|
+
clearTimeout(id);
|
|
3384
|
+
}
|
|
3385
|
+
this._lifecycleTimers.clear();
|
|
3386
|
+
for (const id of this._lifecycleRafs) {
|
|
3387
|
+
cancelAnimationFrame(id);
|
|
3388
|
+
}
|
|
3389
|
+
this._lifecycleRafs.clear();
|
|
3390
|
+
}
|
|
2288
3391
|
_setupGlobalStylesMirroring() {
|
|
2289
3392
|
if (typeof document === 'undefined')
|
|
2290
3393
|
return;
|
|
@@ -2396,23 +3499,64 @@
|
|
|
2396
3499
|
return this._buildScopedStyleElement(node.textContent || '', String(index));
|
|
2397
3500
|
}
|
|
2398
3501
|
if (node instanceof HTMLLinkElement && node.href) {
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
3502
|
+
const credentials = node.crossOrigin === 'use-credentials' ? 'include' : 'same-origin';
|
|
3503
|
+
const cacheKey = `${node.href}|${credentials}`;
|
|
3504
|
+
let cacheEntry = EnhancedSelect._mirroredStylesheetTextCache.get(cacheKey);
|
|
3505
|
+
if (!cacheEntry) {
|
|
3506
|
+
cacheEntry = {
|
|
3507
|
+
promise: fetch(node.href, {
|
|
3508
|
+
credentials,
|
|
3509
|
+
mode: 'cors',
|
|
3510
|
+
})
|
|
3511
|
+
.then((response) => {
|
|
3512
|
+
if (!response.ok)
|
|
3513
|
+
return null;
|
|
3514
|
+
return response.text();
|
|
3515
|
+
})
|
|
3516
|
+
.catch(() => null),
|
|
3517
|
+
lastUsed: Date.now(),
|
|
3518
|
+
};
|
|
3519
|
+
EnhancedSelect._mirroredStylesheetTextCache.set(cacheKey, cacheEntry);
|
|
3520
|
+
EnhancedSelect._pruneMirroredStylesheetTextCache();
|
|
3521
|
+
}
|
|
3522
|
+
else {
|
|
3523
|
+
cacheEntry.lastUsed = Date.now();
|
|
2409
3524
|
}
|
|
2410
|
-
|
|
3525
|
+
const cssText = await cacheEntry.promise;
|
|
3526
|
+
if (cssText === null) {
|
|
3527
|
+
EnhancedSelect._mirroredStylesheetTextCache.delete(cacheKey);
|
|
2411
3528
|
return null;
|
|
2412
3529
|
}
|
|
3530
|
+
return this._buildScopedStyleElement(cssText, String(index));
|
|
2413
3531
|
}
|
|
2414
3532
|
return null;
|
|
2415
3533
|
}
|
|
3534
|
+
static _pruneMirroredStylesheetTextCache() {
|
|
3535
|
+
while (EnhancedSelect._mirroredStylesheetTextCache.size > EnhancedSelect._MIRRORED_STYLESHEET_CACHE_LIMIT) {
|
|
3536
|
+
let lruKey;
|
|
3537
|
+
let oldest = Infinity;
|
|
3538
|
+
for (const [key, entry] of EnhancedSelect._mirroredStylesheetTextCache.entries()) {
|
|
3539
|
+
if (entry.lastUsed < oldest) {
|
|
3540
|
+
oldest = entry.lastUsed;
|
|
3541
|
+
lruKey = key;
|
|
3542
|
+
}
|
|
3543
|
+
}
|
|
3544
|
+
if (!lruKey)
|
|
3545
|
+
return;
|
|
3546
|
+
EnhancedSelect._mirroredStylesheetTextCache.delete(lruKey);
|
|
3547
|
+
}
|
|
3548
|
+
}
|
|
3549
|
+
_areClassMapsEqual(left, right) {
|
|
3550
|
+
if (left === right)
|
|
3551
|
+
return true;
|
|
3552
|
+
if (!left || !right)
|
|
3553
|
+
return false;
|
|
3554
|
+
const leftKeys = Object.keys(left);
|
|
3555
|
+
const rightKeys = Object.keys(right);
|
|
3556
|
+
if (leftKeys.length !== rightKeys.length)
|
|
3557
|
+
return false;
|
|
3558
|
+
return leftKeys.every((key) => left[key] === right[key]);
|
|
3559
|
+
}
|
|
2416
3560
|
_buildScopedStyleElement(cssText, id) {
|
|
2417
3561
|
const scopedCss = this._scopeMirroredCssText(cssText);
|
|
2418
3562
|
if (!scopedCss.trim())
|
|
@@ -2557,8 +3701,16 @@
|
|
|
2557
3701
|
return scopedSelector;
|
|
2558
3702
|
}
|
|
2559
3703
|
_normalizeCustomOptionAccessibility(optionEl, meta) {
|
|
2560
|
-
|
|
3704
|
+
const previousNormalizedLabel = this._customOptionNormalizedLabels.get(optionEl);
|
|
3705
|
+
const currentAriaLabel = optionEl.getAttribute('aria-label');
|
|
3706
|
+
const canRefreshNormalizedLabel = previousNormalizedLabel !== undefined
|
|
3707
|
+
&& currentAriaLabel === previousNormalizedLabel;
|
|
3708
|
+
if (!optionEl.hasAttribute('aria-labelledby') && (!currentAriaLabel || canRefreshNormalizedLabel)) {
|
|
2561
3709
|
optionEl.setAttribute('aria-label', meta.label);
|
|
3710
|
+
this._customOptionNormalizedLabels.set(optionEl, meta.label);
|
|
3711
|
+
}
|
|
3712
|
+
else {
|
|
3713
|
+
this._customOptionNormalizedLabels.delete(optionEl);
|
|
2562
3714
|
}
|
|
2563
3715
|
const nestedFocusableSelectors = [
|
|
2564
3716
|
'a[href]',
|
|
@@ -2660,8 +3812,241 @@
|
|
|
2660
3812
|
_isOptionSelectionBlocked(item) {
|
|
2661
3813
|
return Boolean(item?.disabled) && !this._isDisabledOptionSelectable(item);
|
|
2662
3814
|
}
|
|
3815
|
+
_isPhase2DevRuntime() {
|
|
3816
|
+
const env = globalThis.process?.env?.NODE_ENV;
|
|
3817
|
+
return env !== 'production';
|
|
3818
|
+
}
|
|
3819
|
+
_getDuplicateKeyPolicy() {
|
|
3820
|
+
return this._isPhase2DevRuntime() ? 'throw' : 'warn';
|
|
3821
|
+
}
|
|
3822
|
+
_warnPhase2(message) {
|
|
3823
|
+
if (this._isPhase2DevRuntime() && typeof console !== 'undefined') {
|
|
3824
|
+
console.warn(message);
|
|
3825
|
+
}
|
|
3826
|
+
}
|
|
3827
|
+
_getItemValue(item) {
|
|
3828
|
+
return this._config.serverSide.getValueFromItem?.(item) ?? item?.value ?? item;
|
|
3829
|
+
}
|
|
3830
|
+
_getItemLabel(item) {
|
|
3831
|
+
return this._config.serverSide.getLabelFromItem?.(item) ?? item?.label ?? String(item);
|
|
3832
|
+
}
|
|
3833
|
+
_createEngineItemAdapter() {
|
|
3834
|
+
return createDefaultItemAdapter({
|
|
3835
|
+
getValue: (item) => this._getItemValue(item),
|
|
3836
|
+
getLabel: (item) => this._getItemLabel(item),
|
|
3837
|
+
isDisabled: (item) => Boolean(item?.disabled),
|
|
3838
|
+
getGroupKey: (item) => {
|
|
3839
|
+
const group = item?.group;
|
|
3840
|
+
return group == null ? null : String(group);
|
|
3841
|
+
},
|
|
3842
|
+
onIndexFallback: (message) => this._warnPhase2(message),
|
|
3843
|
+
});
|
|
3844
|
+
}
|
|
3845
|
+
_getBlockedDisabledKeys() {
|
|
3846
|
+
const keys = this._itemRegistry.records
|
|
3847
|
+
.filter((record) => record.disabled && this._isOptionSelectionBlocked(record.item))
|
|
3848
|
+
.map((record) => record.key);
|
|
3849
|
+
for (const entry of this._getSelectedEntries()) {
|
|
3850
|
+
if (this._isOptionSelectionBlocked(entry.item))
|
|
3851
|
+
keys.push(entry.key);
|
|
3852
|
+
}
|
|
3853
|
+
return keys;
|
|
3854
|
+
}
|
|
3855
|
+
_syncEngineSelectionConfig() {
|
|
3856
|
+
const required = Boolean(this._input?.hasAttribute('required'));
|
|
3857
|
+
const disabledKeys = this._getBlockedDisabledKeys();
|
|
3858
|
+
this._engine.dispatch({
|
|
3859
|
+
type: 'SELECTION_CONFIG_CHANGED',
|
|
3860
|
+
multiple: this._config.selection.mode === 'multi',
|
|
3861
|
+
required,
|
|
3862
|
+
allowDeselect: this._config.selection.allowDeselect === true,
|
|
3863
|
+
maxSelections: this._config.selection.maxSelections ?? 0,
|
|
3864
|
+
disabledKeys,
|
|
3865
|
+
});
|
|
3866
|
+
if (this._state) {
|
|
3867
|
+
this._syncLegacySelectionFromEngine();
|
|
3868
|
+
this._syncLegacyActiveFromEngine();
|
|
3869
|
+
this._assertSelectionProjection('selection config');
|
|
3870
|
+
this._assertActiveProjection('selection config');
|
|
3871
|
+
}
|
|
3872
|
+
}
|
|
3873
|
+
_rebuildItemRegistry(items = this._state.loadedItems) {
|
|
3874
|
+
// Build first so duplicate-key rejection leaves the previous runtime intact.
|
|
3875
|
+
const registry = createItemRegistry(items, this._itemAdapter, {
|
|
3876
|
+
duplicateKeyPolicy: this._getDuplicateKeyPolicy(),
|
|
3877
|
+
});
|
|
3878
|
+
const activeKey = this._engine.state.activeKey;
|
|
3879
|
+
const anchorKey = this._rangeAnchorIndex === null ? undefined : this._itemRegistry.indexToKey.get(this._rangeAnchorIndex);
|
|
3880
|
+
this._itemRegistry = registry;
|
|
3881
|
+
this._state.loadedItems = items;
|
|
3882
|
+
this._registryItemsRef = items;
|
|
3883
|
+
const disabledKeys = this._getBlockedDisabledKeys();
|
|
3884
|
+
this._rangeAnchorIndex = anchorKey === undefined ? null : registry.keyToIndex.get(anchorKey) ?? null;
|
|
3885
|
+
this._engine.dispatch({
|
|
3886
|
+
type: 'ITEMS_REGISTERED',
|
|
3887
|
+
keys: registry.records.map((record) => record.key),
|
|
3888
|
+
disabledKeys,
|
|
3889
|
+
});
|
|
3890
|
+
if (activeKey !== null && registry.keyToRecord.get(activeKey)?.disabled) {
|
|
3891
|
+
this._dispatchActiveKey(null, 'programmatic', 'items registered with disabled active key');
|
|
3892
|
+
}
|
|
3893
|
+
else {
|
|
3894
|
+
this._syncLegacyActiveFromEngine();
|
|
3895
|
+
}
|
|
3896
|
+
this._syncLegacySelectionFromEngine();
|
|
3897
|
+
this._syncEngineSelectionConfig();
|
|
3898
|
+
this._assertSelectionProjection('items registered');
|
|
3899
|
+
}
|
|
3900
|
+
_ensureRegistryCurrent() {
|
|
3901
|
+
if (this._registryItemsRef !== this._state.loadedItems
|
|
3902
|
+
|| this._itemRegistry.records.length !== this._state.loadedItems.length
|
|
3903
|
+
|| this._itemRegistry.records.some((record, index) => record.item !== this._state.loadedItems[index])) {
|
|
3904
|
+
this._rebuildItemRegistry();
|
|
3905
|
+
return;
|
|
3906
|
+
}
|
|
3907
|
+
this._syncLegacySelectionFromEngine();
|
|
3908
|
+
this._syncLegacyActiveFromEngine();
|
|
3909
|
+
this._assertSelectionProjection('registry read');
|
|
3910
|
+
this._assertActiveProjection('registry read');
|
|
3911
|
+
}
|
|
3912
|
+
_getRecordByIndex(index) {
|
|
3913
|
+
const key = this._itemRegistry.indexToKey.get(index);
|
|
3914
|
+
return key === undefined ? null : this._itemRegistry.keyToRecord.get(key) ?? null;
|
|
3915
|
+
}
|
|
3916
|
+
_getSelectedEntries() {
|
|
3917
|
+
const entries = [];
|
|
3918
|
+
if (!this._state)
|
|
3919
|
+
return entries;
|
|
3920
|
+
for (const key of this._engine.state.selectedKeys) {
|
|
3921
|
+
const record = this._itemRegistry.keyToRecord.get(key) ?? this._selectedRecordsByKey.get(key);
|
|
3922
|
+
if (record)
|
|
3923
|
+
entries.push({ key, index: record.index, item: record.item });
|
|
3924
|
+
}
|
|
3925
|
+
return entries;
|
|
3926
|
+
}
|
|
3927
|
+
_syncLegacySelectionFromEngine() {
|
|
3928
|
+
const selectedIndices = new Set();
|
|
3929
|
+
const selectedItems = new Map();
|
|
3930
|
+
const selectedKeys = this._engine.state.selectedKeys;
|
|
3931
|
+
for (const key of [...this._selectedRecordsByKey.keys()]) {
|
|
3932
|
+
if (!selectedKeys.has(key))
|
|
3933
|
+
this._selectedRecordsByKey.delete(key);
|
|
3934
|
+
}
|
|
3935
|
+
for (const key of selectedKeys) {
|
|
3936
|
+
const record = this._itemRegistry.keyToRecord.get(key);
|
|
3937
|
+
if (record)
|
|
3938
|
+
this._selectedRecordsByKey.set(key, record);
|
|
3939
|
+
if (!record)
|
|
3940
|
+
continue;
|
|
3941
|
+
selectedIndices.add(record.index);
|
|
3942
|
+
selectedItems.set(record.index, record.item);
|
|
3943
|
+
}
|
|
3944
|
+
this._state.selectedIndices = selectedIndices;
|
|
3945
|
+
this._state.selectedItems = selectedItems;
|
|
3946
|
+
}
|
|
3947
|
+
_projectedSelectedKeysSnapshot() {
|
|
3948
|
+
return new Set([...this._state.selectedIndices]
|
|
3949
|
+
.map((index) => this._itemRegistry.indexToKey.get(index))
|
|
3950
|
+
.filter((key) => key !== undefined));
|
|
3951
|
+
}
|
|
3952
|
+
_assertSelectionProjection(context) {
|
|
3953
|
+
if (!this._isPhase2DevRuntime())
|
|
3954
|
+
return;
|
|
3955
|
+
const legacyKeys = this._projectedSelectedKeysSnapshot();
|
|
3956
|
+
const engineKeys = this._engine.state.selectedKeys;
|
|
3957
|
+
if (this._state.selectedItems.size !== this._state.selectedIndices.size
|
|
3958
|
+
|| [...this._state.selectedIndices].some((index) => !this._state.selectedItems.has(index))
|
|
3959
|
+
|| [...engineKeys].some((key) => this._itemRegistry.keyToRecord.has(key) && !legacyKeys.has(key))
|
|
3960
|
+
|| [...legacyKeys].some((key) => !engineKeys.has(key))) {
|
|
3961
|
+
throw new Error(`[Smilodon Phase2] Selection projection failed in ${context}: compatibility=${JSON.stringify([...legacyKeys])}, engine=${JSON.stringify([...engineKeys])}`);
|
|
3962
|
+
}
|
|
3963
|
+
}
|
|
3964
|
+
_projectedActiveKeySnapshot() {
|
|
3965
|
+
return this._state.activeIndex < 0
|
|
3966
|
+
? null
|
|
3967
|
+
: this._itemRegistry.indexToKey.get(this._state.activeIndex) ?? null;
|
|
3968
|
+
}
|
|
3969
|
+
_syncLegacyActiveFromEngine() {
|
|
3970
|
+
const activeKey = this._engine.state.activeKey;
|
|
3971
|
+
this._state.activeIndex = activeKey === null
|
|
3972
|
+
? -1
|
|
3973
|
+
: this._itemRegistry.keyToIndex.get(activeKey) ?? -1;
|
|
3974
|
+
this._syncLegacyActiveAria();
|
|
3975
|
+
}
|
|
3976
|
+
_assertActiveProjection(context) {
|
|
3977
|
+
if (!this._isPhase2DevRuntime())
|
|
3978
|
+
return;
|
|
3979
|
+
const legacyKey = this._projectedActiveKeySnapshot();
|
|
3980
|
+
const engineKey = this._engine.state.activeKey;
|
|
3981
|
+
if (legacyKey !== engineKey && !Object.is(legacyKey, engineKey)) {
|
|
3982
|
+
throw new Error(`[Smilodon Phase2] Active projection failed in ${context}: compatibility=${String(legacyKey)}, engine=${String(engineKey)}`);
|
|
3983
|
+
}
|
|
3984
|
+
}
|
|
3985
|
+
_dispatchActiveKey(key, reason, context) {
|
|
3986
|
+
const effects = this._engine.dispatch({ type: 'ACTIVE_KEY_CHANGED', key, reason });
|
|
3987
|
+
this._runEngineEffects(effects);
|
|
3988
|
+
this._syncLegacyActiveFromEngine();
|
|
3989
|
+
this._assertActiveProjection(context);
|
|
3990
|
+
}
|
|
3991
|
+
_clearDisabledEngineActive(context) {
|
|
3992
|
+
const activeKey = this._engine.state.activeKey;
|
|
3993
|
+
if (activeKey !== null && this._itemRegistry.keyToRecord.get(activeKey)?.disabled) {
|
|
3994
|
+
this._dispatchActiveKey(null, 'programmatic', context);
|
|
3995
|
+
}
|
|
3996
|
+
else {
|
|
3997
|
+
this._syncLegacyActiveFromEngine();
|
|
3998
|
+
this._assertActiveProjection(context);
|
|
3999
|
+
}
|
|
4000
|
+
}
|
|
4001
|
+
/**
|
|
4002
|
+
* @deprecated Test/backward-compatibility shim. Production active-state authority is engine.activeKey.
|
|
4003
|
+
* Kept for existing renderer tests; component mutation paths dispatch by key.
|
|
4004
|
+
*/
|
|
4005
|
+
_mirrorLegacyActive(reason, context) {
|
|
4006
|
+
this._dispatchActiveKey(this._projectedActiveKeySnapshot(), reason, context);
|
|
4007
|
+
}
|
|
4008
|
+
_dispatchSelection(event, context) {
|
|
4009
|
+
this._engine.dispatch(event);
|
|
4010
|
+
this._syncLegacySelectionFromEngine();
|
|
4011
|
+
this._assertSelectionProjection(context);
|
|
4012
|
+
}
|
|
4013
|
+
/**
|
|
4014
|
+
* @deprecated Test/backward-compatibility shim. Production selection authority is engine.selectedKeys.
|
|
4015
|
+
* Kept for existing styling tests; dispatches to engine authority.
|
|
4016
|
+
*/
|
|
4017
|
+
_mirrorLegacySelection(event, context) {
|
|
4018
|
+
this._dispatchSelection(event, context);
|
|
4019
|
+
}
|
|
4020
|
+
_requestSelectionReplacement(entries, reason, context) {
|
|
4021
|
+
for (const entry of entries) {
|
|
4022
|
+
const record = this._itemRegistry.keyToRecord.get(entry.key) ?? this._selectedRecordsByKey.get(entry.key);
|
|
4023
|
+
this._selectedRecordsByKey.set(entry.key, record ?? {
|
|
4024
|
+
key: entry.key,
|
|
4025
|
+
item: entry.item,
|
|
4026
|
+
index: entry.index,
|
|
4027
|
+
label: this._getItemLabel(entry.item),
|
|
4028
|
+
value: this._getItemValue(entry.item),
|
|
4029
|
+
disabled: Boolean(entry.item?.disabled),
|
|
4030
|
+
groupKey: entry.item?.group == null
|
|
4031
|
+
? null
|
|
4032
|
+
: String(entry.item.group),
|
|
4033
|
+
});
|
|
4034
|
+
}
|
|
4035
|
+
this._dispatchSelection({ type: 'REPLACE_SELECTION_REQUEST', keys: entries.map(({ key }) => key), reason }, context);
|
|
4036
|
+
}
|
|
4037
|
+
_registerShadowItems(indices) {
|
|
4038
|
+
const keys = indices.map((index) => this._itemRegistry.indexToKey.get(index)).filter((key) => key !== undefined);
|
|
4039
|
+
this._engine.dispatch({ type: 'ITEMS_REGISTERED', keys, disabledKeys: this._getBlockedDisabledKeys() });
|
|
4040
|
+
this._clearDisabledEngineActive('visible items');
|
|
4041
|
+
this._assertSelectionProjection('visible items');
|
|
4042
|
+
}
|
|
4043
|
+
_syncLegacyActiveAria() {
|
|
4044
|
+
this._syncComboboxAccessibility();
|
|
4045
|
+
}
|
|
2663
4046
|
_syncStyleConfigVariables() {
|
|
2664
4047
|
const styles = this._config.styles;
|
|
4048
|
+
if (!styles)
|
|
4049
|
+
return;
|
|
2665
4050
|
this._applyStyleVariableMap(styles.option, {
|
|
2666
4051
|
background: '--select-option-bg',
|
|
2667
4052
|
backgroundColor: '--select-option-bg',
|
|
@@ -3076,10 +4461,7 @@
|
|
|
3076
4461
|
delete inputStyles.maxHeight;
|
|
3077
4462
|
Object.assign(input.style, inputStyles);
|
|
3078
4463
|
}
|
|
3079
|
-
input.setAttribute('role', 'combobox');
|
|
3080
|
-
input.setAttribute('aria-expanded', 'false');
|
|
3081
4464
|
input.setAttribute('aria-haspopup', 'listbox');
|
|
3082
|
-
input.setAttribute('aria-autocomplete', this._config.searchable ? 'list' : 'none');
|
|
3083
4465
|
return input;
|
|
3084
4466
|
}
|
|
3085
4467
|
_createDropdown() {
|
|
@@ -3093,11 +4475,6 @@
|
|
|
3093
4475
|
if (this._config.styles.dropdown) {
|
|
3094
4476
|
Object.assign(dropdown.style, this._config.styles.dropdown);
|
|
3095
4477
|
}
|
|
3096
|
-
dropdown.setAttribute('role', 'listbox');
|
|
3097
|
-
dropdown.setAttribute('aria-labelledby', `${this._uniqueId}-input`);
|
|
3098
|
-
if (this._config.selection.mode === 'multi') {
|
|
3099
|
-
dropdown.setAttribute('aria-multiselectable', 'true');
|
|
3100
|
-
}
|
|
3101
4478
|
return dropdown;
|
|
3102
4479
|
}
|
|
3103
4480
|
_createOptionsContainer() {
|
|
@@ -3113,6 +4490,12 @@
|
|
|
3113
4490
|
liveRegion.style.cssText = 'position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0;';
|
|
3114
4491
|
return liveRegion;
|
|
3115
4492
|
}
|
|
4493
|
+
_createErrorDescription() {
|
|
4494
|
+
const description = document.createElement('div');
|
|
4495
|
+
description.id = `${this._uniqueId}-error`;
|
|
4496
|
+
description.style.cssText = 'position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0;';
|
|
4497
|
+
return description;
|
|
4498
|
+
}
|
|
3116
4499
|
_createArrowContainer() {
|
|
3117
4500
|
const container = document.createElement('div');
|
|
3118
4501
|
container.className = 'dropdown-arrow-container';
|
|
@@ -3159,10 +4542,12 @@
|
|
|
3159
4542
|
if (this._liveRegion) {
|
|
3160
4543
|
this._shadow.appendChild(this._liveRegion);
|
|
3161
4544
|
}
|
|
4545
|
+
if (this._errorDescription) {
|
|
4546
|
+
this._shadow.appendChild(this._errorDescription);
|
|
4547
|
+
}
|
|
3162
4548
|
// Set ARIA relationships
|
|
3163
4549
|
const listboxId = `${this._uniqueId}-listbox`;
|
|
3164
4550
|
this._dropdown.id = listboxId;
|
|
3165
|
-
this._input.setAttribute('aria-controls', listboxId);
|
|
3166
4551
|
this._input.setAttribute('aria-owns', listboxId);
|
|
3167
4552
|
this._syncInputContainerMode();
|
|
3168
4553
|
this._syncClearControlState();
|
|
@@ -4012,7 +5397,7 @@
|
|
|
4012
5397
|
margin: var(--select-group-header-margin, 0 calc(var(--select-options-padding, 6px) * -1));
|
|
4013
5398
|
font-weight: var(--select-group-header-weight, 600);
|
|
4014
5399
|
color: var(--select-group-header-color, var(--select-text-muted));
|
|
4015
|
-
background: var(--select-group-header-bg, #
|
|
5400
|
+
background: var(--select-group-header-bg, #f3f4f6);
|
|
4016
5401
|
text-align: var(--select-group-header-text-align, left);
|
|
4017
5402
|
font-size: var(--select-group-header-font-size, 0.7333em);
|
|
4018
5403
|
text-transform: var(--select-group-header-text-transform, uppercase);
|
|
@@ -4519,6 +5904,25 @@
|
|
|
4519
5904
|
box-shadow: var(--select-group-header-shadow, 0 1px 2px rgba(0, 0, 0, 0.3));
|
|
4520
5905
|
}
|
|
4521
5906
|
|
|
5907
|
+
:host(.dark-mode) .group-header,
|
|
5908
|
+
:host([dark-mode]) .group-header,
|
|
5909
|
+
:host([darkmode]) .group-header,
|
|
5910
|
+
:host([data-theme="dark"]) .group-header,
|
|
5911
|
+
:host([theme="dark"]) .group-header {
|
|
5912
|
+
color: var(--select-dark-group-header-color, var(--select-group-header-dark-color, #9ca3af));
|
|
5913
|
+
background: var(--select-dark-group-header-bg, var(--select-group-header-dark-bg, #1a1a2e));
|
|
5914
|
+
border-bottom: var(--select-group-header-border-bottom, 1px solid rgba(255, 255, 255, 0.08));
|
|
5915
|
+
box-shadow: var(--select-group-header-shadow, 0 1px 2px rgba(0, 0, 0, 0.3));
|
|
5916
|
+
}
|
|
5917
|
+
|
|
5918
|
+
:host(.dark-mode) .group-header:not(:first-child),
|
|
5919
|
+
:host([dark-mode]) .group-header:not(:first-child),
|
|
5920
|
+
:host([darkmode]) .group-header:not(:first-child),
|
|
5921
|
+
:host([data-theme="dark"]) .group-header:not(:first-child),
|
|
5922
|
+
:host([theme="dark"]) .group-header:not(:first-child) {
|
|
5923
|
+
border-top-color: var(--select-border);
|
|
5924
|
+
}
|
|
5925
|
+
|
|
4522
5926
|
:host(.dark-mode) .option.selected::before,
|
|
4523
5927
|
:host([dark-mode]) .option.selected::before,
|
|
4524
5928
|
:host([darkmode]) .option.selected::before,
|
|
@@ -4616,7 +6020,7 @@
|
|
|
4616
6020
|
this._handleOpen();
|
|
4617
6021
|
}
|
|
4618
6022
|
// Focus the input (do not prevent default behavior for input itself)
|
|
4619
|
-
this.
|
|
6023
|
+
this._applyFocusIntent('input', 'input-container-trigger');
|
|
4620
6024
|
// If we just opened the dropdown, transfer pointer capture to the
|
|
4621
6025
|
// options container so the subsequent pointerup lands there instead of
|
|
4622
6026
|
// staying with the input container (which would swallow the event).
|
|
@@ -4653,7 +6057,7 @@
|
|
|
4653
6057
|
if (!this._state.isOpen) {
|
|
4654
6058
|
this._handleOpen();
|
|
4655
6059
|
}
|
|
4656
|
-
this.
|
|
6060
|
+
this._applyFocusIntent('input', 'input-container-activation');
|
|
4657
6061
|
});
|
|
4658
6062
|
this._inputContent.addEventListener('pointermove', (e) => {
|
|
4659
6063
|
this._updateMultiScrollDrag(e);
|
|
@@ -4679,7 +6083,10 @@
|
|
|
4679
6083
|
e.stopPropagation();
|
|
4680
6084
|
});
|
|
4681
6085
|
// Input focus/blur
|
|
4682
|
-
this._input.addEventListener('focus', () =>
|
|
6086
|
+
this._input.addEventListener('focus', () => {
|
|
6087
|
+
this._runEngineEffects(this._engine.dispatch({ type: 'FOCUS_GAINED' }));
|
|
6088
|
+
this._handleOpen();
|
|
6089
|
+
});
|
|
4683
6090
|
// When the input loses focus we normally close the dropdown, but
|
|
4684
6091
|
// clicking an option will blur the input before the option's click
|
|
4685
6092
|
// handler executes. To avoid the blur timer closing the dropdown
|
|
@@ -4687,12 +6094,13 @@
|
|
|
4687
6094
|
// interacting with the options container. The close callback checks this
|
|
4688
6095
|
// flag and skips closing if the user is about to click an option.
|
|
4689
6096
|
this._input.addEventListener('blur', (e) => {
|
|
6097
|
+
this._runEngineEffects(this._engine.dispatch({ type: 'FOCUS_LOST' }));
|
|
4690
6098
|
const related = e.relatedTarget;
|
|
4691
6099
|
if (related && (this._shadow.contains(related) || this._container.contains(related))) {
|
|
4692
6100
|
return;
|
|
4693
6101
|
}
|
|
4694
6102
|
// Delay to allow option click/focus transitions
|
|
4695
|
-
|
|
6103
|
+
this._setLifecycleTimeout(() => {
|
|
4696
6104
|
if (this._suppressBlurClose) {
|
|
4697
6105
|
// another pointerdown inside options is in progress; keep open
|
|
4698
6106
|
return;
|
|
@@ -4701,7 +6109,7 @@
|
|
|
4701
6109
|
if (active && (this._shadow.contains(active) || this._container.contains(active))) {
|
|
4702
6110
|
return;
|
|
4703
6111
|
}
|
|
4704
|
-
this._handleClose();
|
|
6112
|
+
this._handleClose('blur');
|
|
4705
6113
|
}, 150);
|
|
4706
6114
|
});
|
|
4707
6115
|
// Input search
|
|
@@ -4717,7 +6125,7 @@
|
|
|
4717
6125
|
// sufficient.
|
|
4718
6126
|
this._optionsContainer.addEventListener('pointerdown', () => {
|
|
4719
6127
|
this._suppressBlurClose = true;
|
|
4720
|
-
|
|
6128
|
+
this._setLifecycleTimeout(() => {
|
|
4721
6129
|
this._suppressBlurClose = false;
|
|
4722
6130
|
}, 150); // Increased timeout to ensure click finishes before blur checks
|
|
4723
6131
|
});
|
|
@@ -4752,42 +6160,6 @@
|
|
|
4752
6160
|
this._optionsContainer.addEventListener('click', handleOptionEvent);
|
|
4753
6161
|
// Keyboard navigation
|
|
4754
6162
|
this._input.addEventListener('keydown', (e) => this._handleKeydown(e));
|
|
4755
|
-
// Click outside to close — robust detection across shadow DOM and custom renderers
|
|
4756
|
-
document.addEventListener('pointerdown', (e) => {
|
|
4757
|
-
const path = (e.composedPath && e.composedPath()) || [];
|
|
4758
|
-
let clickedInside = false;
|
|
4759
|
-
for (const node of path) {
|
|
4760
|
-
if (node === this || node === this._container) {
|
|
4761
|
-
clickedInside = true;
|
|
4762
|
-
break;
|
|
4763
|
-
}
|
|
4764
|
-
if (node instanceof Node) {
|
|
4765
|
-
try {
|
|
4766
|
-
if (this._shadow && this._shadow.contains(node)) {
|
|
4767
|
-
clickedInside = true;
|
|
4768
|
-
break;
|
|
4769
|
-
}
|
|
4770
|
-
}
|
|
4771
|
-
catch (err) {
|
|
4772
|
-
// ignore
|
|
4773
|
-
}
|
|
4774
|
-
}
|
|
4775
|
-
if (node instanceof Element) {
|
|
4776
|
-
try {
|
|
4777
|
-
if (node.matches('[data-sm-selectable], [data-selectable], [data-sm-state], .input-container, .select-container, .dropdown-arrow-container, .clear-control-button')) {
|
|
4778
|
-
clickedInside = true;
|
|
4779
|
-
break;
|
|
4780
|
-
}
|
|
4781
|
-
}
|
|
4782
|
-
catch (err) {
|
|
4783
|
-
// ignore
|
|
4784
|
-
}
|
|
4785
|
-
}
|
|
4786
|
-
}
|
|
4787
|
-
if (!clickedInside) {
|
|
4788
|
-
this._handleClose();
|
|
4789
|
-
}
|
|
4790
|
-
});
|
|
4791
6163
|
}
|
|
4792
6164
|
_initializeObservers() {
|
|
4793
6165
|
// Disconnect existing observer if any
|
|
@@ -4813,41 +6185,143 @@
|
|
|
4813
6185
|
return;
|
|
4814
6186
|
}
|
|
4815
6187
|
this._setBusy(true);
|
|
6188
|
+
const generation = ++this._asyncGeneration;
|
|
4816
6189
|
try {
|
|
4817
6190
|
const items = await this._config.serverSide.fetchSelectedItems(this._config.serverSide.initialSelectedValues);
|
|
4818
|
-
|
|
4819
|
-
|
|
4820
|
-
|
|
4821
|
-
this.
|
|
6191
|
+
if (generation !== this._asyncGeneration || !this.isConnected)
|
|
6192
|
+
return;
|
|
6193
|
+
const fetchedRegistry = createItemRegistry(items, this._itemAdapter, {
|
|
6194
|
+
duplicateKeyPolicy: this._getDuplicateKeyPolicy(),
|
|
4822
6195
|
});
|
|
6196
|
+
this._requestSelectionReplacement(fetchedRegistry.records, 'external-value-sync', 'initial selected items');
|
|
4823
6197
|
this._updateInputDisplay();
|
|
4824
6198
|
}
|
|
4825
6199
|
catch (error) {
|
|
6200
|
+
if (generation !== this._asyncGeneration || !this.isConnected)
|
|
6201
|
+
return;
|
|
4826
6202
|
this._handleError(error);
|
|
4827
6203
|
}
|
|
4828
6204
|
finally {
|
|
4829
|
-
this.
|
|
6205
|
+
if (generation === this._asyncGeneration && this.isConnected) {
|
|
6206
|
+
this._setBusy(false);
|
|
6207
|
+
}
|
|
4830
6208
|
}
|
|
4831
6209
|
}
|
|
6210
|
+
_runEngineEffects(effects) {
|
|
6211
|
+
this._effectRunner.run(effects, {
|
|
6212
|
+
focusTrigger: () => {
|
|
6213
|
+
this._applyFocusIntent('input', 'engine-focus-trigger');
|
|
6214
|
+
},
|
|
6215
|
+
syncAria: () => {
|
|
6216
|
+
this._syncPopupAria();
|
|
6217
|
+
},
|
|
6218
|
+
});
|
|
6219
|
+
}
|
|
6220
|
+
_syncPopupAria() {
|
|
6221
|
+
if (!this._input)
|
|
6222
|
+
return;
|
|
6223
|
+
this._syncComboboxAccessibility();
|
|
6224
|
+
}
|
|
6225
|
+
_syncComboboxAccessibility() {
|
|
6226
|
+
if (!this._input || !this._engine || !this._state)
|
|
6227
|
+
return;
|
|
6228
|
+
const accessibilityState = deriveAccessibilityState(this._engine.state, {
|
|
6229
|
+
searchable: this._config.searchable,
|
|
6230
|
+
readonly: !this._config.searchable,
|
|
6231
|
+
invalid: this._hasError,
|
|
6232
|
+
label: {
|
|
6233
|
+
ariaLabel: this.getAttribute('aria-label')?.trim() || null,
|
|
6234
|
+
ariaDescription: this.getAttribute('aria-description')?.trim() || null,
|
|
6235
|
+
},
|
|
6236
|
+
errorMessage: this._errorMessage,
|
|
6237
|
+
errorDescriptionId: this._hasError && this._errorMessage ? this._errorDescription?.id ?? null : null,
|
|
6238
|
+
});
|
|
6239
|
+
const effects = computeAccessibilityEffects(accessibilityState, {
|
|
6240
|
+
controlsId: this._dropdown?.id || null,
|
|
6241
|
+
optionKeys: [],
|
|
6242
|
+
}).filter((effect) => effect.type === 'SYNC_COMBOBOX_ARIA');
|
|
6243
|
+
applyAccessibilityEffects(effects, {
|
|
6244
|
+
combobox: this._input,
|
|
6245
|
+
getOptionByKey: (key) => {
|
|
6246
|
+
if (!this._state.isOpen)
|
|
6247
|
+
return null;
|
|
6248
|
+
const index = this._itemRegistry.keyToIndex.get(key);
|
|
6249
|
+
return index === undefined ? null : this._getOptionElementByIndex(index);
|
|
6250
|
+
},
|
|
6251
|
+
});
|
|
6252
|
+
}
|
|
6253
|
+
_syncListboxAccessibility() {
|
|
6254
|
+
if (!this._dropdown || !this._engine || !this._state)
|
|
6255
|
+
return;
|
|
6256
|
+
const derivedState = deriveAccessibilityState(this._engine.state, {
|
|
6257
|
+
label: { ariaLabel: this.getAttribute('aria-label')?.trim() || null },
|
|
6258
|
+
});
|
|
6259
|
+
const accessibilityState = {
|
|
6260
|
+
...derivedState,
|
|
6261
|
+
// The component's existing busy lifecycle is not yet mirrored to engine dataStatus.
|
|
6262
|
+
busy: this._state.isBusy,
|
|
6263
|
+
};
|
|
6264
|
+
const effects = computeAccessibilityEffects(accessibilityState, {
|
|
6265
|
+
controlsId: this._dropdown.id || null,
|
|
6266
|
+
optionKeys: [],
|
|
6267
|
+
}).filter((effect) => effect.type === 'SYNC_LISTBOX_ARIA');
|
|
6268
|
+
applyAccessibilityEffects(effects, { listbox: this._dropdown });
|
|
6269
|
+
}
|
|
6270
|
+
_syncRenderedOptionAccessibility() {
|
|
6271
|
+
if (!this._optionsContainer || !this._engine || !this._state)
|
|
6272
|
+
return;
|
|
6273
|
+
const optionTargets = new Map();
|
|
6274
|
+
this._optionsContainer.querySelectorAll('[data-index]').forEach((option) => {
|
|
6275
|
+
const index = Number(option.getAttribute('data-index'));
|
|
6276
|
+
if (!Number.isFinite(index))
|
|
6277
|
+
return;
|
|
6278
|
+
const key = this._itemRegistry.indexToKey.get(index);
|
|
6279
|
+
if (key !== undefined)
|
|
6280
|
+
optionTargets.set(key, option);
|
|
6281
|
+
});
|
|
6282
|
+
if (optionTargets.size === 0)
|
|
6283
|
+
return;
|
|
6284
|
+
const effects = computeAccessibilityEffects(deriveAccessibilityState(this._engine.state), {
|
|
6285
|
+
controlsId: this._dropdown?.id || null,
|
|
6286
|
+
optionKeys: [...optionTargets.keys()],
|
|
6287
|
+
}).filter((effect) => effect.type === 'SYNC_OPTION_ARIA');
|
|
6288
|
+
applyAccessibilityEffects(effects, {
|
|
6289
|
+
getOptionByKey: (key) => optionTargets.get(key) ?? null,
|
|
6290
|
+
});
|
|
6291
|
+
}
|
|
6292
|
+
_applyFocusIntent(target, reason, focusTarget) {
|
|
6293
|
+
const effects = computeAccessibilityEffects(deriveAccessibilityState(this._engine.state), {
|
|
6294
|
+
optionKeys: [],
|
|
6295
|
+
focusIntents: [{ target, reason }],
|
|
6296
|
+
}).filter((effect) => effect.type === 'FOCUS_INTENT');
|
|
6297
|
+
applyAccessibilityEffects(effects, {
|
|
6298
|
+
focusTargets: {
|
|
6299
|
+
input: target === 'input' ? this._input : null,
|
|
6300
|
+
clearButton: target === 'clear-button' ? this._clearControl : null,
|
|
6301
|
+
getActiveOption: () => target === 'active-option' ? focusTarget ?? null : null,
|
|
6302
|
+
},
|
|
6303
|
+
}, { applyFocus: true });
|
|
6304
|
+
}
|
|
4832
6305
|
_handleOpen() {
|
|
4833
|
-
if (!this._config.enabled
|
|
6306
|
+
if (!this._config.enabled) {
|
|
6307
|
+
this._runEngineEffects(this._engine.dispatch({ type: 'DISABLED_CHANGED', disabled: true }));
|
|
6308
|
+
return;
|
|
6309
|
+
}
|
|
6310
|
+
if (this._state.isOpen)
|
|
4834
6311
|
return;
|
|
4835
6312
|
// close any other open selects before proceeding
|
|
4836
6313
|
EnhancedSelect._instances.forEach(inst => {
|
|
4837
6314
|
if (inst !== this)
|
|
4838
6315
|
inst._handleClose();
|
|
4839
6316
|
});
|
|
4840
|
-
|
|
4841
|
-
|
|
4842
|
-
|
|
4843
|
-
|
|
4844
|
-
this._input.focus();
|
|
6317
|
+
const effects = this._engine.dispatch({ type: 'OPEN_REQUEST' });
|
|
6318
|
+
if (!effects.some((effect) => effect.type === 'POPUP_OPENED'))
|
|
6319
|
+
return;
|
|
6320
|
+
this._runEngineEffects(effects);
|
|
4845
6321
|
this._markOpenStart();
|
|
4846
|
-
this._state.isOpen = true;
|
|
4847
6322
|
this.setAttribute('data-open', 'true');
|
|
4848
6323
|
this._liftStackingAncestors();
|
|
4849
6324
|
this._dropdown.style.display = 'block';
|
|
4850
|
-
this._input.setAttribute('aria-expanded', 'true');
|
|
4851
6325
|
this._updateArrowRotation();
|
|
4852
6326
|
// Render options when opening
|
|
4853
6327
|
this._renderOptions();
|
|
@@ -4858,26 +6332,32 @@
|
|
|
4858
6332
|
this._announce('Options expanded');
|
|
4859
6333
|
// Scroll to selected if configured
|
|
4860
6334
|
if (this._config.scrollToSelected.enabled) {
|
|
6335
|
+
const keyboardActiveChangeGeneration = this._keyboardActiveChangeGeneration;
|
|
6336
|
+
const canRestoreOpenSelection = () => this._state.isOpen
|
|
6337
|
+
&& this._keyboardActiveChangeGeneration === keyboardActiveChangeGeneration;
|
|
4861
6338
|
// Use requestAnimationFrame for better timing after render
|
|
4862
|
-
|
|
6339
|
+
this._requestLifecycleFrame(() => {
|
|
6340
|
+
if (!canRestoreOpenSelection())
|
|
6341
|
+
return;
|
|
4863
6342
|
// Double RAF to ensure layout is complete
|
|
4864
|
-
|
|
6343
|
+
this._requestLifecycleFrame(() => {
|
|
6344
|
+
if (!canRestoreOpenSelection())
|
|
6345
|
+
return;
|
|
4865
6346
|
this._scrollToSelected();
|
|
4866
6347
|
});
|
|
4867
6348
|
});
|
|
4868
6349
|
}
|
|
4869
6350
|
}
|
|
4870
|
-
_handleClose() {
|
|
4871
|
-
|
|
6351
|
+
_handleClose(reason = 'programmatic') {
|
|
6352
|
+
const effects = this._engine.dispatch({ type: 'CLOSE_REQUEST', reason });
|
|
6353
|
+
if (!effects.some((effect) => effect.type === 'POPUP_CLOSED'))
|
|
4872
6354
|
return;
|
|
4873
|
-
this._state.isOpen = false;
|
|
4874
6355
|
this.removeAttribute('data-open');
|
|
4875
6356
|
this._restoreLiftedAncestors();
|
|
4876
6357
|
this._dropdown.style.display = 'none';
|
|
4877
6358
|
this._dropdown.setAttribute('data-placement', this._resolvedDropdownPlacement);
|
|
4878
|
-
this._input.setAttribute('aria-expanded', 'false');
|
|
4879
|
-
this._input.removeAttribute('aria-activedescendant');
|
|
4880
6359
|
this._updateArrowRotation();
|
|
6360
|
+
this._runEngineEffects(effects);
|
|
4881
6361
|
this._emit('close', {});
|
|
4882
6362
|
this._config.callbacks.onClose?.();
|
|
4883
6363
|
this._announce('Options collapsed');
|
|
@@ -4888,14 +6368,13 @@
|
|
|
4888
6368
|
this._liftStackingAncestors();
|
|
4889
6369
|
this._dropdown.style.display = 'block';
|
|
4890
6370
|
this._syncDropdownPlacement();
|
|
4891
|
-
this._input.setAttribute('aria-expanded', 'true');
|
|
4892
6371
|
}
|
|
4893
6372
|
else {
|
|
4894
6373
|
this.removeAttribute('data-open');
|
|
4895
6374
|
this._restoreLiftedAncestors();
|
|
4896
6375
|
this._dropdown.style.display = 'none';
|
|
4897
|
-
this._input.setAttribute('aria-expanded', 'false');
|
|
4898
6376
|
}
|
|
6377
|
+
this._syncComboboxAccessibility();
|
|
4899
6378
|
}
|
|
4900
6379
|
_liftStackingAncestors() {
|
|
4901
6380
|
this._restoreLiftedAncestors();
|
|
@@ -4983,6 +6462,9 @@
|
|
|
4983
6462
|
if (!this._isPerfEnabled())
|
|
4984
6463
|
return;
|
|
4985
6464
|
performance.measure(name, start, end);
|
|
6465
|
+
performance.clearMarks(start);
|
|
6466
|
+
performance.clearMarks(end);
|
|
6467
|
+
performance.clearMeasures(name);
|
|
4986
6468
|
}
|
|
4987
6469
|
_markOpenStart() {
|
|
4988
6470
|
if (!this._isPerfEnabled())
|
|
@@ -5080,7 +6562,7 @@
|
|
|
5080
6562
|
this._state.lastNotifiedQuery = query;
|
|
5081
6563
|
this._state.lastNotifiedResultCount = count;
|
|
5082
6564
|
// Use setTimeout to avoid synchronous state updates during render
|
|
5083
|
-
|
|
6565
|
+
this._setLifecycleTimeout(() => {
|
|
5084
6566
|
this._emit('search', { query, results: filteredItems, count });
|
|
5085
6567
|
this._config.callbacks.onSearch?.(query);
|
|
5086
6568
|
}, 0);
|
|
@@ -5152,13 +6634,17 @@
|
|
|
5152
6634
|
if (!this._state.isOpen) {
|
|
5153
6635
|
this._handleOpen();
|
|
5154
6636
|
}
|
|
5155
|
-
else
|
|
5156
|
-
|
|
6637
|
+
else {
|
|
6638
|
+
const activeKey = this._engine.state.activeKey;
|
|
6639
|
+
const currentIndex = activeKey === null ? -1 : this._itemRegistry.keyToIndex.get(activeKey) ?? -1;
|
|
6640
|
+
if (currentIndex >= 0) {
|
|
6641
|
+
this._selectOption(currentIndex, { shiftKey: e.shiftKey, toggleKey: e.ctrlKey || e.metaKey, reason: 'keyboard' });
|
|
6642
|
+
}
|
|
5157
6643
|
}
|
|
5158
6644
|
break;
|
|
5159
6645
|
case 'Escape':
|
|
5160
6646
|
e.preventDefault();
|
|
5161
|
-
this._handleClose();
|
|
6647
|
+
this._handleClose('escape');
|
|
5162
6648
|
break;
|
|
5163
6649
|
case 'Tab':
|
|
5164
6650
|
if (this._state.isOpen) {
|
|
@@ -5184,7 +6670,9 @@
|
|
|
5184
6670
|
const selectableIndices = this._getSelectableOptionIndices();
|
|
5185
6671
|
if (selectableIndices.length === 0)
|
|
5186
6672
|
return;
|
|
5187
|
-
const
|
|
6673
|
+
const activeKey = this._engine.state.activeKey;
|
|
6674
|
+
const currentIndex = activeKey === null ? -1 : this._itemRegistry.keyToIndex.get(activeKey) ?? -1;
|
|
6675
|
+
const currentPosition = selectableIndices.indexOf(currentIndex);
|
|
5188
6676
|
const basePosition = currentPosition >= 0
|
|
5189
6677
|
? currentPosition
|
|
5190
6678
|
: delta >= 0
|
|
@@ -5194,7 +6682,7 @@
|
|
|
5194
6682
|
const next = selectableIndices[nextPosition];
|
|
5195
6683
|
this._setActive(next);
|
|
5196
6684
|
if (this._config.selection.mode === 'multi' && opts?.shiftKey) {
|
|
5197
|
-
const anchor = this._rangeAnchorIndex ??
|
|
6685
|
+
const anchor = this._rangeAnchorIndex ?? currentIndex;
|
|
5198
6686
|
const anchorIndex = anchor >= 0 ? anchor : next;
|
|
5199
6687
|
if (this._rangeAnchorIndex === null) {
|
|
5200
6688
|
this._rangeAnchorIndex = anchorIndex;
|
|
@@ -5202,14 +6690,26 @@
|
|
|
5202
6690
|
this._selectRange(anchorIndex, next, { clear: !opts?.toggleKey });
|
|
5203
6691
|
}
|
|
5204
6692
|
}
|
|
5205
|
-
_setActive(index) {
|
|
5206
|
-
|
|
5207
|
-
|
|
6693
|
+
_setActive(index, reason = 'keyboard') {
|
|
6694
|
+
this._ensureRegistryCurrent();
|
|
6695
|
+
const record = this._getRecordByIndex(index);
|
|
6696
|
+
let option = this._getOptionElementByIndex(index);
|
|
6697
|
+
if (!option && record && this._guardedVirtualizer) {
|
|
6698
|
+
this._materializeGuardedVirtualKey(record.key);
|
|
6699
|
+
option = this._getOptionElementByIndex(index);
|
|
6700
|
+
}
|
|
6701
|
+
if (!record || record.disabled || !option || option.getAttribute('aria-disabled') === 'true') {
|
|
5208
6702
|
return;
|
|
5209
6703
|
}
|
|
6704
|
+
if (reason === 'keyboard')
|
|
6705
|
+
this._keyboardActiveChangeGeneration += 1;
|
|
6706
|
+
const previousIndex = this._state.activeIndex;
|
|
6707
|
+
this._dispatchActiveKey(record.key, reason, 'set active');
|
|
6708
|
+
if (this._state.activeIndex !== index)
|
|
6709
|
+
return;
|
|
5210
6710
|
// Clear previous active state
|
|
5211
|
-
if (
|
|
5212
|
-
const prevOption = this._getOptionElementByIndex(
|
|
6711
|
+
if (previousIndex >= 0 && previousIndex !== index) {
|
|
6712
|
+
const prevOption = this._getOptionElementByIndex(previousIndex);
|
|
5213
6713
|
if (prevOption) {
|
|
5214
6714
|
// Check if it's a custom SelectOption or a lightweight DOM element
|
|
5215
6715
|
if ('setActive' in prevOption && typeof prevOption.setActive === 'function') {
|
|
@@ -5217,11 +6717,10 @@
|
|
|
5217
6717
|
}
|
|
5218
6718
|
else {
|
|
5219
6719
|
// Lightweight option - remove active class
|
|
5220
|
-
prevOption.classList.remove('smilodon-option--active');
|
|
6720
|
+
prevOption.classList.remove('active', 'smilodon-option--active');
|
|
5221
6721
|
}
|
|
5222
6722
|
}
|
|
5223
6723
|
}
|
|
5224
|
-
this._state.activeIndex = index;
|
|
5225
6724
|
// Set new active state
|
|
5226
6725
|
if (option) {
|
|
5227
6726
|
// Check if it's a custom SelectOption or a lightweight DOM element
|
|
@@ -5230,7 +6729,7 @@
|
|
|
5230
6729
|
}
|
|
5231
6730
|
else {
|
|
5232
6731
|
// Lightweight option - add active class
|
|
5233
|
-
option.classList.add('smilodon-option--active');
|
|
6732
|
+
option.classList.add('active', 'smilodon-option--active');
|
|
5234
6733
|
}
|
|
5235
6734
|
if (typeof option.scrollIntoView === 'function') {
|
|
5236
6735
|
// Don't scroll wildly when just opening with pre-selections
|
|
@@ -5240,8 +6739,7 @@
|
|
|
5240
6739
|
const total = this._state.loadedItems.length;
|
|
5241
6740
|
this._announce(`Item ${index + 1} of ${total}`);
|
|
5242
6741
|
// Update aria-activedescendant using the actual option id when available
|
|
5243
|
-
|
|
5244
|
-
this._input.setAttribute('aria-activedescendant', optionId);
|
|
6742
|
+
this._syncLegacyActiveAria();
|
|
5245
6743
|
}
|
|
5246
6744
|
}
|
|
5247
6745
|
_getOptionElementByIndex(index) {
|
|
@@ -5252,10 +6750,16 @@
|
|
|
5252
6750
|
// Only return elements with data-index or data-sm-selectable attributes
|
|
5253
6751
|
return Array.from(this._optionsContainer.children).filter((child) => {
|
|
5254
6752
|
return (child.hasAttribute('data-index') || child.hasAttribute('data-sm-selectable'))
|
|
5255
|
-
&&
|
|
6753
|
+
&& child.getAttribute('aria-disabled') !== 'true';
|
|
5256
6754
|
});
|
|
5257
6755
|
}
|
|
5258
6756
|
_getSelectableOptionIndices() {
|
|
6757
|
+
if (this._guardedVirtualizer) {
|
|
6758
|
+
return this._renderPlan.filteredKeys.flatMap((key) => {
|
|
6759
|
+
const record = this._itemRegistry.keyToRecord.get(key);
|
|
6760
|
+
return record && !record.disabled ? [record.index] : [];
|
|
6761
|
+
});
|
|
6762
|
+
}
|
|
5259
6763
|
return this._getSelectableOptions()
|
|
5260
6764
|
.map((option) => Number(option.getAttribute('data-sm-index') ?? option.getAttribute('data-index')))
|
|
5261
6765
|
.filter((index) => Number.isFinite(index));
|
|
@@ -5273,7 +6777,7 @@
|
|
|
5273
6777
|
keyboardFocus: (index) => {
|
|
5274
6778
|
this._setActive(index);
|
|
5275
6779
|
const el = this._getOptionElementByIndex(index);
|
|
5276
|
-
el
|
|
6780
|
+
this._applyFocusIntent('active-option', 'renderer-keyboard-focus', el);
|
|
5277
6781
|
},
|
|
5278
6782
|
};
|
|
5279
6783
|
}
|
|
@@ -5288,43 +6792,25 @@
|
|
|
5288
6792
|
const getValue = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
5289
6793
|
const matchIndex = this._state.loadedItems.findIndex((item) => getValue(item).toLowerCase().startsWith(this._typeBuffer));
|
|
5290
6794
|
if (matchIndex >= 0) {
|
|
5291
|
-
this._setActive(matchIndex);
|
|
6795
|
+
this._setActive(matchIndex, 'search');
|
|
5292
6796
|
}
|
|
5293
6797
|
}
|
|
5294
6798
|
_selectAll() {
|
|
5295
6799
|
if (this._config.selection.mode !== 'multi')
|
|
5296
6800
|
return;
|
|
6801
|
+
this._ensureRegistryCurrent();
|
|
5297
6802
|
const options = this._getSelectableOptions();
|
|
5298
|
-
const
|
|
6803
|
+
const entries = this._getSelectedEntries();
|
|
5299
6804
|
options.forEach((option) => {
|
|
5300
6805
|
const optionIndex = Number(option.getAttribute('data-sm-index') ?? option.getAttribute('data-index'));
|
|
5301
6806
|
if (!Number.isFinite(optionIndex))
|
|
5302
6807
|
return;
|
|
5303
|
-
|
|
5304
|
-
|
|
5305
|
-
|
|
5306
|
-
if (!this._state.selectedIndices.has(optionIndex)) {
|
|
5307
|
-
// Check if it's a custom SelectOption or a lightweight DOM element
|
|
5308
|
-
if ('getConfig' in option && typeof option.getConfig === 'function') {
|
|
5309
|
-
const config = option.getConfig();
|
|
5310
|
-
if (this._isOptionSelectionBlocked(config.item))
|
|
5311
|
-
return;
|
|
5312
|
-
this._state.selectedIndices.add(optionIndex);
|
|
5313
|
-
this._state.selectedItems.set(optionIndex, config.item);
|
|
5314
|
-
option.setSelected(true);
|
|
5315
|
-
}
|
|
5316
|
-
else {
|
|
5317
|
-
// Lightweight option - get item from data attribute or state
|
|
5318
|
-
const item = this._state.loadedItems[optionIndex];
|
|
5319
|
-
if (item && !this._isOptionSelectionBlocked(item)) {
|
|
5320
|
-
this._state.selectedIndices.add(optionIndex);
|
|
5321
|
-
this._state.selectedItems.set(optionIndex, item);
|
|
5322
|
-
option.classList.add('smilodon-option--selected');
|
|
5323
|
-
option.setAttribute('aria-selected', 'true');
|
|
5324
|
-
}
|
|
5325
|
-
}
|
|
5326
|
-
}
|
|
6808
|
+
const record = this._getRecordByIndex(optionIndex);
|
|
6809
|
+
if (record && !this._isOptionSelectionBlocked(record.item))
|
|
6810
|
+
entries.push(record);
|
|
5327
6811
|
});
|
|
6812
|
+
this._requestSelectionReplacement(entries, 'keyboard', 'select all');
|
|
6813
|
+
this._renderOptions();
|
|
5328
6814
|
this._updateInputDisplay();
|
|
5329
6815
|
this._emitChange();
|
|
5330
6816
|
this._announce(`Selected all ${options.length} items`);
|
|
@@ -5332,82 +6818,101 @@
|
|
|
5332
6818
|
_selectRange(start, end, opts) {
|
|
5333
6819
|
if (this._config.selection.mode !== 'multi')
|
|
5334
6820
|
return;
|
|
5335
|
-
|
|
6821
|
+
this._ensureRegistryCurrent();
|
|
5336
6822
|
const [min, max] = start < end ? [start, end] : [end, start];
|
|
5337
|
-
|
|
5338
|
-
|
|
5339
|
-
this.
|
|
5340
|
-
|
|
6823
|
+
const selectedEntries = this._getSelectedEntries();
|
|
6824
|
+
const entries = opts?.clear
|
|
6825
|
+
? selectedEntries.filter((entry) => !this._itemRegistry.keyToRecord.has(entry.key))
|
|
6826
|
+
: selectedEntries;
|
|
5341
6827
|
for (let i = min; i <= max; i += 1) {
|
|
5342
|
-
|
|
5343
|
-
break;
|
|
5344
|
-
const item = this._state.loadedItems[i];
|
|
6828
|
+
const record = this._getRecordByIndex(i);
|
|
5345
6829
|
const optionEl = this._getOptionElementByIndex(i);
|
|
5346
6830
|
if (optionEl?.getAttribute('aria-disabled') === 'true' && !optionEl.classList.contains('smilodon-option--disabled-selectable'))
|
|
5347
6831
|
continue;
|
|
5348
|
-
if (!item)
|
|
5349
|
-
continue;
|
|
5350
|
-
if (this._isOptionSelectionBlocked(item))
|
|
6832
|
+
if (!record || this._isOptionSelectionBlocked(record.item))
|
|
5351
6833
|
continue;
|
|
5352
|
-
|
|
5353
|
-
this._state.selectedItems.set(i, item);
|
|
6834
|
+
entries.push(record);
|
|
5354
6835
|
}
|
|
6836
|
+
this._requestSelectionReplacement(entries, opts?.reason ?? 'keyboard', 'select range');
|
|
5355
6837
|
this._renderOptions();
|
|
5356
6838
|
this._updateInputDisplay();
|
|
6839
|
+
const target = this._getRecordByIndex(end);
|
|
6840
|
+
const selectDetail = target ? this._getSelectEventDetail(target) : null;
|
|
6841
|
+
if (selectDetail)
|
|
6842
|
+
this._emit('select', selectDetail);
|
|
5357
6843
|
this._emitChange();
|
|
5358
6844
|
this._announce(`${this._state.selectedIndices.size} items selected`);
|
|
6845
|
+
if (selectDetail)
|
|
6846
|
+
this._config.callbacks.onSelect?.(selectDetail);
|
|
6847
|
+
}
|
|
6848
|
+
_getSelectEventDetail(record) {
|
|
6849
|
+
const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
|
|
6850
|
+
const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
6851
|
+
return {
|
|
6852
|
+
item: record.item,
|
|
6853
|
+
index: record.index,
|
|
6854
|
+
value: getValue(record.item),
|
|
6855
|
+
label: getLabel(record.item),
|
|
6856
|
+
selected: this._engine.state.selectedKeys.has(record.key),
|
|
6857
|
+
multi: this._config.selection.mode === 'multi',
|
|
6858
|
+
};
|
|
5359
6859
|
}
|
|
5360
6860
|
_setInitialActiveOption() {
|
|
5361
6861
|
const selectableIndices = this._getSelectableOptionIndices();
|
|
5362
|
-
if (selectableIndices.length === 0)
|
|
5363
|
-
return;
|
|
5364
6862
|
const selected = Array.from(this._state.selectedIndices).sort((a, b) => a - b);
|
|
5365
|
-
if (this._config.selection.mode === 'multi' && selected.length === 0) {
|
|
5366
|
-
this.
|
|
5367
|
-
this._input.removeAttribute('aria-activedescendant');
|
|
6863
|
+
if (selectableIndices.length === 0 || (this._config.selection.mode === 'multi' && selected.length === 0)) {
|
|
6864
|
+
this._dispatchActiveKey(null, 'open', 'initial active option');
|
|
5368
6865
|
return;
|
|
5369
6866
|
}
|
|
5370
6867
|
const target = selected.find((index) => selectableIndices.includes(index)) ?? selectableIndices[0];
|
|
5371
|
-
this._setActive(target);
|
|
6868
|
+
this._setActive(target, 'open');
|
|
5372
6869
|
}
|
|
5373
6870
|
_announce(message) {
|
|
5374
|
-
|
|
5375
|
-
|
|
5376
|
-
|
|
5377
|
-
|
|
5378
|
-
|
|
5379
|
-
|
|
5380
|
-
|
|
6871
|
+
const effects = computeAccessibilityEffects(deriveAccessibilityState(this._engine.state), {
|
|
6872
|
+
optionKeys: [],
|
|
6873
|
+
announcements: [{ politeness: 'polite', message }],
|
|
6874
|
+
}).filter((effect) => effect.type === 'ANNOUNCE');
|
|
6875
|
+
applyAccessibilityEffects(effects, { liveRegion: this._liveRegion });
|
|
6876
|
+
if (!this._liveRegion)
|
|
6877
|
+
return;
|
|
6878
|
+
this._setLifecycleTimeout(() => {
|
|
6879
|
+
const clearEffects = computeAccessibilityEffects(deriveAccessibilityState(this._engine.state), {
|
|
6880
|
+
optionKeys: [],
|
|
6881
|
+
announcements: [{ politeness: 'polite', message: '' }],
|
|
6882
|
+
}).filter((effect) => effect.type === 'ANNOUNCE');
|
|
6883
|
+
applyAccessibilityEffects(clearEffects, { liveRegion: this._liveRegion });
|
|
6884
|
+
}, 1000);
|
|
5381
6885
|
}
|
|
5382
6886
|
_selectOption(index, opts) {
|
|
5383
6887
|
// FIX: Do not rely on this._optionsContainer.children[index] because filtering changes the children
|
|
5384
6888
|
// Instead, use the index to update state directly
|
|
5385
|
-
|
|
5386
|
-
|
|
6889
|
+
this._ensureRegistryCurrent();
|
|
6890
|
+
const record = this._getRecordByIndex(index);
|
|
6891
|
+
if (!record)
|
|
5387
6892
|
return;
|
|
5388
|
-
|
|
6893
|
+
index = record.index;
|
|
6894
|
+
const item = record.item;
|
|
5389
6895
|
if (this._isOptionSelectionBlocked(item)) {
|
|
5390
6896
|
return;
|
|
5391
6897
|
}
|
|
5392
|
-
const isCurrentlySelected = this.
|
|
6898
|
+
const isCurrentlySelected = this._engine.state.selectedKeys.has(record.key);
|
|
6899
|
+
const reason = opts?.reason ?? 'pointer';
|
|
5393
6900
|
// Keep active/focus styling aligned with the most recently interacted option.
|
|
5394
6901
|
// Without this, a previously selected item may retain active classes/styles
|
|
5395
6902
|
// after selecting a different option.
|
|
5396
|
-
|
|
6903
|
+
if (!record.disabled) {
|
|
6904
|
+
this._dispatchActiveKey(record.key, reason === 'keyboard' ? 'keyboard' : 'pointer', 'option selection');
|
|
6905
|
+
}
|
|
5397
6906
|
if (this._config.selection.mode === 'single') {
|
|
5398
|
-
|
|
5399
|
-
|
|
5400
|
-
|
|
5401
|
-
|
|
5402
|
-
|
|
5403
|
-
// Select this option
|
|
5404
|
-
this._state.selectedIndices.add(index);
|
|
5405
|
-
this._state.selectedItems.set(index, item);
|
|
5406
|
-
}
|
|
6907
|
+
this._dispatchSelection({
|
|
6908
|
+
type: isCurrentlySelected && this._config.selection.allowDeselect ? 'TOGGLE_KEY_REQUEST' : 'SELECT_KEY_REQUEST',
|
|
6909
|
+
key: record.key,
|
|
6910
|
+
reason,
|
|
6911
|
+
}, 'single option selection');
|
|
5407
6912
|
// Re-render to update all option styles
|
|
5408
6913
|
this._renderOptions();
|
|
5409
6914
|
if (this._config.selection.closeOnSelect) {
|
|
5410
|
-
this._handleClose();
|
|
6915
|
+
this._handleClose('selection');
|
|
5411
6916
|
}
|
|
5412
6917
|
}
|
|
5413
6918
|
else {
|
|
@@ -5415,35 +6920,27 @@
|
|
|
5415
6920
|
const shiftKey = Boolean(opts?.shiftKey);
|
|
5416
6921
|
if (shiftKey) {
|
|
5417
6922
|
const anchor = this._rangeAnchorIndex ?? index;
|
|
5418
|
-
this._selectRange(anchor, index, { clear: !toggleKey });
|
|
6923
|
+
this._selectRange(anchor, index, { clear: !toggleKey, reason });
|
|
5419
6924
|
this._rangeAnchorIndex = anchor;
|
|
5420
6925
|
return;
|
|
5421
6926
|
}
|
|
5422
6927
|
// Multi select with toggle
|
|
5423
6928
|
const maxSelections = this._config.selection.maxSelections || 0;
|
|
5424
|
-
if (isCurrentlySelected) {
|
|
5425
|
-
|
|
5426
|
-
this.
|
|
5427
|
-
|
|
5428
|
-
}
|
|
5429
|
-
else {
|
|
5430
|
-
// Select (toggle on)
|
|
5431
|
-
if (maxSelections > 0 && this._state.selectedIndices.size >= maxSelections) {
|
|
5432
|
-
this._announce(`Maximum ${maxSelections} selections allowed`);
|
|
5433
|
-
return; // Max selections reached
|
|
5434
|
-
}
|
|
5435
|
-
this._state.selectedIndices.add(index);
|
|
5436
|
-
this._state.selectedItems.set(index, item);
|
|
6929
|
+
if (!isCurrentlySelected && maxSelections > 0 && this._engine.state.selectedKeys.size >= maxSelections) {
|
|
6930
|
+
this._announce(`Maximum ${maxSelections} selections allowed`);
|
|
6931
|
+
this._renderOptions();
|
|
6932
|
+
return;
|
|
5437
6933
|
}
|
|
6934
|
+
this._dispatchSelection({ type: 'TOGGLE_KEY_REQUEST', key: record.key, reason }, 'multi option selection');
|
|
5438
6935
|
// Re-render to update styles (safer than trying to find the element in filtered list)
|
|
5439
6936
|
this._renderOptions();
|
|
5440
6937
|
}
|
|
5441
6938
|
this._rangeAnchorIndex = index;
|
|
5442
6939
|
this._updateInputDisplay();
|
|
6940
|
+
const selectDetail = this._getSelectEventDetail(record);
|
|
6941
|
+
this._emit('select', selectDetail);
|
|
5443
6942
|
this._emitChange();
|
|
5444
|
-
const
|
|
5445
|
-
const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
5446
|
-
const label = getLabel(item);
|
|
6943
|
+
const label = selectDetail.label;
|
|
5447
6944
|
if (this._config.selection.mode === 'single') {
|
|
5448
6945
|
this._announce(`Selected ${label}`);
|
|
5449
6946
|
}
|
|
@@ -5452,25 +6949,34 @@
|
|
|
5452
6949
|
const action = isCurrentlySelected ? 'Deselected' : 'Selected';
|
|
5453
6950
|
this._announce(`${action} ${label}. ${selectedCount} selected`);
|
|
5454
6951
|
}
|
|
5455
|
-
this._config.callbacks.onSelect?.(
|
|
5456
|
-
item: item,
|
|
5457
|
-
index,
|
|
5458
|
-
value: getValue(item),
|
|
5459
|
-
label: getLabel(item),
|
|
5460
|
-
selected: this._state.selectedIndices.has(index),
|
|
5461
|
-
});
|
|
6952
|
+
this._config.callbacks.onSelect?.(selectDetail);
|
|
5462
6953
|
}
|
|
5463
|
-
_handleOptionRemove(index) {
|
|
5464
|
-
|
|
5465
|
-
const
|
|
5466
|
-
|
|
5467
|
-
|
|
6954
|
+
_handleOptionRemove(index, key) {
|
|
6955
|
+
this._ensureRegistryCurrent();
|
|
6956
|
+
const entry = this._getSelectedEntries().find((selected) => key === undefined
|
|
6957
|
+
? selected.index === index
|
|
6958
|
+
: selected.key === key);
|
|
6959
|
+
if (!entry)
|
|
6960
|
+
return;
|
|
6961
|
+
const item = entry.item;
|
|
6962
|
+
const activeElement = this._shadow.activeElement;
|
|
6963
|
+
const focusedBadge = activeElement instanceof HTMLButtonElement
|
|
6964
|
+
&& activeElement.getAttribute('part') === 'chip-remove'
|
|
6965
|
+
? activeElement.closest('.selection-badge')
|
|
6966
|
+
: null;
|
|
6967
|
+
const hadFocusedChipRemove = focusedBadge !== null;
|
|
6968
|
+
const focusedBadgeIndex = focusedBadge
|
|
6969
|
+
? Array.from(this._inputContent.querySelectorAll('.selection-badge')).indexOf(focusedBadge)
|
|
6970
|
+
: -1;
|
|
6971
|
+
const option = this._itemRegistry.keyToRecord.has(entry.key)
|
|
6972
|
+
? this._getOptionElementByIndex(entry.index)
|
|
6973
|
+
: null;
|
|
6974
|
+
this._dispatchSelection({ type: 'DESELECT_KEY_REQUEST', key: entry.key, reason: 'pointer' }, 'remove selected item');
|
|
5468
6975
|
if (option && 'setSelected' in option && typeof option.setSelected === 'function') {
|
|
5469
6976
|
option.setSelected(false);
|
|
5470
6977
|
}
|
|
5471
6978
|
else if (option) {
|
|
5472
6979
|
option.classList.remove('selected', 'sm-selected', 'smilodon-option--selected');
|
|
5473
|
-
option.setAttribute('aria-selected', 'false');
|
|
5474
6980
|
const stateTokens = (option.dataset.smState || '')
|
|
5475
6981
|
.split(' ')
|
|
5476
6982
|
.map(token => token.trim())
|
|
@@ -5485,13 +6991,24 @@
|
|
|
5485
6991
|
this._updateInputDisplay();
|
|
5486
6992
|
this._renderOptions();
|
|
5487
6993
|
this._emitChange();
|
|
6994
|
+
if (hadFocusedChipRemove && this.isConnected) {
|
|
6995
|
+
const removeButtons = this._inputContent.querySelectorAll('[part="chip-remove"]');
|
|
6996
|
+
if (removeButtons.length > 0) {
|
|
6997
|
+
const nextFocusIndex = Math.min(Math.max(focusedBadgeIndex, 0), removeButtons.length - 1);
|
|
6998
|
+
removeButtons[nextFocusIndex].focus();
|
|
6999
|
+
}
|
|
7000
|
+
else {
|
|
7001
|
+
this._applyFocusIntent('input', 'selected-chip-removed');
|
|
7002
|
+
}
|
|
7003
|
+
}
|
|
5488
7004
|
const config = option && 'getConfig' in option && typeof option.getConfig === 'function'
|
|
5489
7005
|
? option.getConfig()
|
|
5490
7006
|
: undefined;
|
|
5491
|
-
this._emit('remove', { item: config?.item ?? item, index });
|
|
7007
|
+
this._emit('remove', { item: config?.item ?? item, index: entry.index });
|
|
5492
7008
|
}
|
|
5493
7009
|
_updateInputDisplay() {
|
|
5494
|
-
const
|
|
7010
|
+
const selectedEntries = this._getSelectedEntries();
|
|
7011
|
+
const selectedItems = selectedEntries.map(({ item }) => item);
|
|
5495
7012
|
const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
5496
7013
|
if (selectedItems.length === 0) {
|
|
5497
7014
|
this._input.value = '';
|
|
@@ -5511,8 +7028,7 @@
|
|
|
5511
7028
|
const existingBadges = this._inputContent.querySelectorAll('.selection-badge');
|
|
5512
7029
|
existingBadges.forEach(badge => badge.remove());
|
|
5513
7030
|
// Create badges for each selected item
|
|
5514
|
-
|
|
5515
|
-
selectedEntries.forEach(([index, item]) => {
|
|
7031
|
+
selectedEntries.forEach(({ key, index, item }) => {
|
|
5516
7032
|
const badge = document.createElement('span');
|
|
5517
7033
|
badge.className = 'selection-badge';
|
|
5518
7034
|
if (this._config.styles.classNames?.badge) {
|
|
@@ -5555,7 +7071,7 @@
|
|
|
5555
7071
|
removeBtn.addEventListener('click', (e) => {
|
|
5556
7072
|
e.stopPropagation();
|
|
5557
7073
|
e.preventDefault();
|
|
5558
|
-
this._handleOptionRemove(index);
|
|
7074
|
+
this._handleOptionRemove(index, key);
|
|
5559
7075
|
});
|
|
5560
7076
|
badge.appendChild(removeBtn);
|
|
5561
7077
|
}
|
|
@@ -5568,7 +7084,7 @@
|
|
|
5568
7084
|
// Add fade-out animation
|
|
5569
7085
|
this._optionsContainer.style.opacity = '0';
|
|
5570
7086
|
this._optionsContainer.style.transition = 'opacity 0.15s ease-out';
|
|
5571
|
-
|
|
7087
|
+
this._setLifecycleTimeout(() => {
|
|
5572
7088
|
this._renderOptions();
|
|
5573
7089
|
// Fade back in
|
|
5574
7090
|
this._optionsContainer.style.opacity = '1';
|
|
@@ -5579,7 +7095,9 @@
|
|
|
5579
7095
|
if (this._state.selectedIndices.size === 0)
|
|
5580
7096
|
return;
|
|
5581
7097
|
const target = this._config.scrollToSelected.multiSelectTarget;
|
|
5582
|
-
const indices = Array.from(this._state.selectedIndices).sort((a, b) => a - b);
|
|
7098
|
+
const indices = Array.from(this._state.selectedIndices).filter((index) => index >= 0).sort((a, b) => a - b);
|
|
7099
|
+
if (indices.length === 0)
|
|
7100
|
+
return;
|
|
5583
7101
|
// For multi-select, find the closest selected item to the current scroll position
|
|
5584
7102
|
let targetIndex;
|
|
5585
7103
|
if (this._config.selection.mode === 'multi' && indices.length > 1) {
|
|
@@ -5607,7 +7125,14 @@
|
|
|
5607
7125
|
targetIndex = target === 'first' ? indices[0] : indices[indices.length - 1];
|
|
5608
7126
|
}
|
|
5609
7127
|
// Find and scroll to the target option
|
|
5610
|
-
|
|
7128
|
+
let option = this._getOptionElementByIndex(targetIndex);
|
|
7129
|
+
if (!option && this._guardedVirtualizer) {
|
|
7130
|
+
const targetKey = this._itemRegistry.indexToKey.get(targetIndex);
|
|
7131
|
+
if (targetKey !== undefined) {
|
|
7132
|
+
this._materializeGuardedVirtualKey(targetKey);
|
|
7133
|
+
option = this._getOptionElementByIndex(targetIndex);
|
|
7134
|
+
}
|
|
7135
|
+
}
|
|
5611
7136
|
if (option) {
|
|
5612
7137
|
// Use smooth scrolling with center alignment for better UX
|
|
5613
7138
|
if (typeof option.scrollIntoView === 'function') {
|
|
@@ -5617,7 +7142,7 @@
|
|
|
5617
7142
|
});
|
|
5618
7143
|
}
|
|
5619
7144
|
// Also set it as active for keyboard navigation
|
|
5620
|
-
this._setActive(targetIndex);
|
|
7145
|
+
this._setActive(targetIndex, 'programmatic');
|
|
5621
7146
|
}
|
|
5622
7147
|
}
|
|
5623
7148
|
async _loadMoreItems() {
|
|
@@ -5649,12 +7174,7 @@
|
|
|
5649
7174
|
}
|
|
5650
7175
|
_setBusy(busy) {
|
|
5651
7176
|
this._state.isBusy = busy;
|
|
5652
|
-
|
|
5653
|
-
this._dropdown.setAttribute('aria-busy', 'true');
|
|
5654
|
-
}
|
|
5655
|
-
else {
|
|
5656
|
-
this._dropdown.removeAttribute('aria-busy');
|
|
5657
|
-
}
|
|
7177
|
+
this._syncListboxAccessibility();
|
|
5658
7178
|
// Trigger re-render to show/hide busy indicator
|
|
5659
7179
|
// We use _renderOptions to handle the UI update
|
|
5660
7180
|
this._renderOptions();
|
|
@@ -5823,10 +7343,11 @@
|
|
|
5823
7343
|
};
|
|
5824
7344
|
}
|
|
5825
7345
|
_emitChange() {
|
|
5826
|
-
const
|
|
7346
|
+
const selectedEntries = this._getSelectedEntries();
|
|
7347
|
+
const selectedItems = selectedEntries.map(({ item }) => item);
|
|
5827
7348
|
const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
|
|
5828
7349
|
const selectedValues = selectedItems.map(getValue);
|
|
5829
|
-
const selectedIndices =
|
|
7350
|
+
const selectedIndices = selectedEntries.map(({ index }) => index).filter((index) => index >= 0);
|
|
5830
7351
|
this._emit('change', { selectedItems, selectedValues, selectedIndices });
|
|
5831
7352
|
this._config.callbacks.onChange?.(selectedItems, selectedValues);
|
|
5832
7353
|
}
|
|
@@ -5835,8 +7356,11 @@
|
|
|
5835
7356
|
return this._optionRenderer;
|
|
5836
7357
|
}
|
|
5837
7358
|
set optionRenderer(renderer) {
|
|
7359
|
+
if (this._optionRenderer === renderer)
|
|
7360
|
+
return;
|
|
7361
|
+
this._invalidateGuardedMeasurements();
|
|
5838
7362
|
this._optionRenderer = renderer;
|
|
5839
|
-
this._setGlobalStylesMirroring(Boolean(renderer || this._classMap));
|
|
7363
|
+
this._setGlobalStylesMirroring(Boolean(renderer || this._classMap || this._groupHeaderRenderer));
|
|
5840
7364
|
this._track('style', 'optionRendererChanged', { enabled: Boolean(renderer) });
|
|
5841
7365
|
this._renderOptions();
|
|
5842
7366
|
}
|
|
@@ -5844,12 +7368,12 @@
|
|
|
5844
7368
|
* Set items to display in the select
|
|
5845
7369
|
*/
|
|
5846
7370
|
setItems(items) {
|
|
7371
|
+
this._invalidateGuardedMeasurements();
|
|
5847
7372
|
const previousLength = this._state.loadedItems.length;
|
|
5848
|
-
this.
|
|
5849
|
-
|
|
5850
|
-
if (this._state.
|
|
5851
|
-
this.
|
|
5852
|
-
}
|
|
7373
|
+
this._rebuildItemRegistry(items);
|
|
7374
|
+
this._state.groupedItems = this._groupItemsByProperty(items);
|
|
7375
|
+
if (!this._state.searchQuery)
|
|
7376
|
+
this._updateInputDisplay();
|
|
5853
7377
|
const newLength = this._state.loadedItems.length;
|
|
5854
7378
|
// When infinite scroll is active (preserveScrollPosition = true),
|
|
5855
7379
|
// we need to maintain scroll position during the update
|
|
@@ -5865,7 +7389,7 @@
|
|
|
5865
7389
|
// new ones simply appear below.
|
|
5866
7390
|
this._dropdown.scrollTop = targetScrollTop;
|
|
5867
7391
|
// Ensure it sticks after layout
|
|
5868
|
-
|
|
7392
|
+
this._requestLifecycleFrame(() => {
|
|
5869
7393
|
if (this._dropdown) {
|
|
5870
7394
|
this._dropdown.scrollTop = targetScrollTop;
|
|
5871
7395
|
}
|
|
@@ -5881,19 +7405,42 @@
|
|
|
5881
7405
|
this._renderOptions();
|
|
5882
7406
|
}
|
|
5883
7407
|
}
|
|
7408
|
+
_groupItemsByProperty(items) {
|
|
7409
|
+
const hasGroupData = items.some((item, index) => this._itemAdapter.getGroupKey?.(item, index) !== null);
|
|
7410
|
+
if (!hasGroupData) {
|
|
7411
|
+
return [];
|
|
7412
|
+
}
|
|
7413
|
+
const groups = new Map();
|
|
7414
|
+
items.forEach((item, index) => {
|
|
7415
|
+
const rawGroup = this._itemAdapter.getGroupKey?.(item, index);
|
|
7416
|
+
const label = rawGroup == null ? 'Ungrouped' : String(rawGroup);
|
|
7417
|
+
const options = groups.get(label);
|
|
7418
|
+
if (options) {
|
|
7419
|
+
options.push(item);
|
|
7420
|
+
}
|
|
7421
|
+
else {
|
|
7422
|
+
groups.set(label, [item]);
|
|
7423
|
+
}
|
|
7424
|
+
});
|
|
7425
|
+
return Array.from(groups.entries()).map(([label, options]) => ({ label, options }));
|
|
7426
|
+
}
|
|
5884
7427
|
/**
|
|
5885
7428
|
* Set grouped items
|
|
5886
7429
|
*/
|
|
5887
7430
|
setGroupedItems(groupedItems) {
|
|
7431
|
+
this._destroyGuardedVirtualizer();
|
|
7432
|
+
this._rebuildItemRegistry(groupedItems.flatMap(group => group.options));
|
|
5888
7433
|
this._state.groupedItems = groupedItems;
|
|
5889
|
-
this._state.loadedItems = groupedItems.flatMap(group => group.options);
|
|
5890
7434
|
this._renderOptions();
|
|
7435
|
+
if (!this._state.searchQuery)
|
|
7436
|
+
this._updateInputDisplay();
|
|
5891
7437
|
}
|
|
5892
7438
|
/**
|
|
5893
7439
|
* Get currently selected items
|
|
5894
7440
|
*/
|
|
5895
7441
|
getSelectedItems() {
|
|
5896
|
-
|
|
7442
|
+
this._ensureRegistryCurrent();
|
|
7443
|
+
return this._getSelectedEntries().map((entry) => entry.item);
|
|
5897
7444
|
}
|
|
5898
7445
|
/**
|
|
5899
7446
|
* Get all loaded items
|
|
@@ -5905,8 +7452,7 @@
|
|
|
5905
7452
|
* Get currently selected values
|
|
5906
7453
|
*/
|
|
5907
7454
|
getSelectedValues() {
|
|
5908
|
-
|
|
5909
|
-
return this.getSelectedItems().map(getValue);
|
|
7455
|
+
return this.getSelectedItems().map((item) => this._getItemValue(item));
|
|
5910
7456
|
}
|
|
5911
7457
|
/**
|
|
5912
7458
|
* Set selected items by value
|
|
@@ -5916,16 +7462,10 @@
|
|
|
5916
7462
|
await this._loadSelectedItemsByValues(values);
|
|
5917
7463
|
}
|
|
5918
7464
|
else {
|
|
5919
|
-
|
|
5920
|
-
const
|
|
5921
|
-
|
|
5922
|
-
this.
|
|
5923
|
-
this._state.loadedItems.forEach((item, index) => {
|
|
5924
|
-
if (arrayIncludesValue(values, getValue(item)) && !this._isOptionSelectionBlocked(item)) {
|
|
5925
|
-
this._state.selectedIndices.add(index);
|
|
5926
|
-
this._state.selectedItems.set(index, item);
|
|
5927
|
-
}
|
|
5928
|
-
});
|
|
7465
|
+
this._ensureRegistryCurrent();
|
|
7466
|
+
const selected = this._itemRegistry.records
|
|
7467
|
+
.filter((record) => arrayIncludesValue(values, record.value) && !this._isOptionSelectionBlocked(record.item));
|
|
7468
|
+
this._requestSelectionReplacement(selected, 'external-value-sync', 'setSelectedValues');
|
|
5929
7469
|
this._renderOptions();
|
|
5930
7470
|
this._updateInputDisplay();
|
|
5931
7471
|
this._emitChange();
|
|
@@ -5938,16 +7478,15 @@
|
|
|
5938
7478
|
if (!this._config.serverSide.fetchSelectedItems)
|
|
5939
7479
|
return;
|
|
5940
7480
|
this._setBusy(true);
|
|
7481
|
+
const generation = ++this._asyncGeneration;
|
|
5941
7482
|
try {
|
|
5942
7483
|
const items = await this._config.serverSide.fetchSelectedItems(values);
|
|
5943
|
-
this.
|
|
5944
|
-
|
|
5945
|
-
items.
|
|
5946
|
-
|
|
5947
|
-
return;
|
|
5948
|
-
this._state.selectedIndices.add(index);
|
|
5949
|
-
this._state.selectedItems.set(index, item);
|
|
7484
|
+
if (generation !== this._asyncGeneration || !this.isConnected)
|
|
7485
|
+
return;
|
|
7486
|
+
const fetchedRegistry = createItemRegistry(items, this._itemAdapter, {
|
|
7487
|
+
duplicateKeyPolicy: this._getDuplicateKeyPolicy(),
|
|
5950
7488
|
});
|
|
7489
|
+
this._requestSelectionReplacement(fetchedRegistry.records, 'external-value-sync', 'fetched selected items');
|
|
5951
7490
|
this._renderOptions();
|
|
5952
7491
|
this._updateInputDisplay();
|
|
5953
7492
|
this._emitChange();
|
|
@@ -5957,18 +7496,22 @@
|
|
|
5957
7496
|
}
|
|
5958
7497
|
}
|
|
5959
7498
|
catch (error) {
|
|
7499
|
+
if (generation !== this._asyncGeneration || !this.isConnected)
|
|
7500
|
+
return;
|
|
5960
7501
|
this._handleError(error);
|
|
5961
7502
|
}
|
|
5962
7503
|
finally {
|
|
5963
|
-
this.
|
|
7504
|
+
if (generation === this._asyncGeneration && this.isConnected) {
|
|
7505
|
+
this._setBusy(false);
|
|
7506
|
+
}
|
|
5964
7507
|
}
|
|
5965
7508
|
}
|
|
5966
7509
|
/**
|
|
5967
7510
|
* Clear all selections
|
|
5968
7511
|
*/
|
|
5969
7512
|
clear() {
|
|
5970
|
-
this.
|
|
5971
|
-
this.
|
|
7513
|
+
this._ensureRegistryCurrent();
|
|
7514
|
+
this._dispatchSelection({ type: 'CLEAR_SELECTION_REQUEST', reason: 'programmatic' }, 'clear');
|
|
5972
7515
|
this._renderOptions();
|
|
5973
7516
|
this._updateInputDisplay();
|
|
5974
7517
|
this._emitChange();
|
|
@@ -6004,8 +7547,17 @@
|
|
|
6004
7547
|
* Update component configuration
|
|
6005
7548
|
*/
|
|
6006
7549
|
updateConfig(config) {
|
|
7550
|
+
this._invalidateGuardedMeasurements();
|
|
6007
7551
|
const previousMode = this._config.selection.mode;
|
|
7552
|
+
const previousEnabled = this._config.enabled;
|
|
6008
7553
|
this._config = this._mergeConfig(this._config, config);
|
|
7554
|
+
this._rebuildItemRegistry();
|
|
7555
|
+
if (previousEnabled !== this._config.enabled) {
|
|
7556
|
+
if (!this._config.enabled && this._state.isOpen) {
|
|
7557
|
+
this._handleClose('disabled');
|
|
7558
|
+
}
|
|
7559
|
+
this._runEngineEffects(this._engine.dispatch({ type: 'DISABLED_CHANGED', disabled: !this._config.enabled }));
|
|
7560
|
+
}
|
|
6009
7561
|
if (previousMode !== this._config.selection.mode &&
|
|
6010
7562
|
this._config.limitations?.autoMitigateRuntimeModeSwitch) {
|
|
6011
7563
|
this.clear();
|
|
@@ -6023,7 +7575,7 @@
|
|
|
6023
7575
|
// Update input state based on new config
|
|
6024
7576
|
if (this._input) {
|
|
6025
7577
|
this._input.readOnly = !this._config.searchable;
|
|
6026
|
-
this.
|
|
7578
|
+
this._syncComboboxAccessibility();
|
|
6027
7579
|
if (this._state.selectedIndices.size === 0) {
|
|
6028
7580
|
this._input.placeholder = this._config.placeholder || 'Select an option...';
|
|
6029
7581
|
}
|
|
@@ -6041,14 +7593,7 @@
|
|
|
6041
7593
|
}
|
|
6042
7594
|
}
|
|
6043
7595
|
this._syncStyleConfigVariables();
|
|
6044
|
-
|
|
6045
|
-
if (this._config.selection.mode === 'multi') {
|
|
6046
|
-
this._dropdown.setAttribute('aria-multiselectable', 'true');
|
|
6047
|
-
}
|
|
6048
|
-
else {
|
|
6049
|
-
this._dropdown.removeAttribute('aria-multiselectable');
|
|
6050
|
-
}
|
|
6051
|
-
}
|
|
7596
|
+
this._syncListboxAccessibility();
|
|
6052
7597
|
if (this.isConnected) {
|
|
6053
7598
|
this._syncDirectionConfig();
|
|
6054
7599
|
this._syncInputContainerMode();
|
|
@@ -6076,6 +7621,8 @@
|
|
|
6076
7621
|
if (!Object.prototype.hasOwnProperty.call(source, key))
|
|
6077
7622
|
continue;
|
|
6078
7623
|
const sourceValue = source[key];
|
|
7624
|
+
if (sourceValue === undefined)
|
|
7625
|
+
continue;
|
|
6079
7626
|
const targetValue = result[key];
|
|
6080
7627
|
// Optional adapter props are commonly forwarded as undefined. Preserve
|
|
6081
7628
|
// the existing/default config in that case instead of erasing a section.
|
|
@@ -6109,7 +7656,7 @@
|
|
|
6109
7656
|
clearedSelection: shouldClearSelection && hadSelection,
|
|
6110
7657
|
clearedSearch: shouldClearSearch && hadSearch,
|
|
6111
7658
|
});
|
|
6112
|
-
this.
|
|
7659
|
+
this._applyFocusIntent('input', 'clear-control-complete');
|
|
6113
7660
|
}
|
|
6114
7661
|
_syncClearControlState() {
|
|
6115
7662
|
if (!this._clearControl || !this._inputContainer || !this._arrowContainer)
|
|
@@ -6135,7 +7682,9 @@
|
|
|
6135
7682
|
setError(message) {
|
|
6136
7683
|
this._hasError = true;
|
|
6137
7684
|
this._errorMessage = message;
|
|
6138
|
-
this.
|
|
7685
|
+
if (this._errorDescription)
|
|
7686
|
+
this._errorDescription.textContent = message;
|
|
7687
|
+
this._syncComboboxAccessibility();
|
|
6139
7688
|
this._announce(`Error: ${message}`);
|
|
6140
7689
|
}
|
|
6141
7690
|
/**
|
|
@@ -6144,20 +7693,22 @@
|
|
|
6144
7693
|
clearError() {
|
|
6145
7694
|
this._hasError = false;
|
|
6146
7695
|
this._errorMessage = '';
|
|
6147
|
-
this.
|
|
7696
|
+
if (this._errorDescription)
|
|
7697
|
+
this._errorDescription.textContent = '';
|
|
7698
|
+
this._syncComboboxAccessibility();
|
|
6148
7699
|
}
|
|
6149
7700
|
/**
|
|
6150
7701
|
* Set required state
|
|
6151
7702
|
*/
|
|
6152
7703
|
setRequired(required) {
|
|
6153
7704
|
if (required) {
|
|
6154
|
-
this._input.setAttribute('aria-required', 'true');
|
|
6155
7705
|
this._input.setAttribute('required', '');
|
|
6156
7706
|
}
|
|
6157
7707
|
else {
|
|
6158
|
-
this._input.removeAttribute('aria-required');
|
|
6159
7708
|
this._input.removeAttribute('required');
|
|
6160
7709
|
}
|
|
7710
|
+
this._syncEngineSelectionConfig();
|
|
7711
|
+
this._syncComboboxAccessibility();
|
|
6161
7712
|
}
|
|
6162
7713
|
/**
|
|
6163
7714
|
* Validate selection (for required fields)
|
|
@@ -6174,15 +7725,414 @@
|
|
|
6174
7725
|
/**
|
|
6175
7726
|
* Render options based on current state
|
|
6176
7727
|
*/
|
|
7728
|
+
_deriveVirtualizationScope(filteredIndices) {
|
|
7729
|
+
const keyForIndex = (index) => this._itemRegistry.indexToKey.get(index);
|
|
7730
|
+
const filteredKeys = filteredIndices
|
|
7731
|
+
.map(keyForIndex)
|
|
7732
|
+
.filter((key) => key !== undefined);
|
|
7733
|
+
return {
|
|
7734
|
+
dataKeys: this._itemRegistry.records.map((record) => record.key),
|
|
7735
|
+
filteredKeys,
|
|
7736
|
+
registeredKeys: Array.from(this._engine.state.registeredKeys),
|
|
7737
|
+
// The current chunked path schedules all filtered items for rendering;
|
|
7738
|
+
// this scaffold records that target set until bounded windows are wired.
|
|
7739
|
+
windowKeys: this._renderPlan.intendedRenderKeys.slice(),
|
|
7740
|
+
mountedKeys: this._getMountedVirtualizationKeys(this._optionsContainer.children),
|
|
7741
|
+
selectedKeys: new Set(this._engine.state.selectedKeys),
|
|
7742
|
+
activeKey: this._engine.state.activeKey,
|
|
7743
|
+
};
|
|
7744
|
+
}
|
|
7745
|
+
_getMountedVirtualizationKeys(targets) {
|
|
7746
|
+
const keyForIndex = (index) => this._itemRegistry.indexToKey.get(index);
|
|
7747
|
+
return Array.from(targets)
|
|
7748
|
+
.filter((target) => target instanceof HTMLElement
|
|
7749
|
+
&& (target.hasAttribute('data-sm-selectable') || target.hasAttribute('data-selectable')))
|
|
7750
|
+
.map((target) => Number(target.getAttribute('data-sm-index') ?? target.getAttribute('data-index')))
|
|
7751
|
+
.filter(Number.isInteger)
|
|
7752
|
+
.map(keyForIndex)
|
|
7753
|
+
.filter((key) => key !== undefined);
|
|
7754
|
+
}
|
|
7755
|
+
_refreshMountedVirtualizationScope() {
|
|
7756
|
+
this._virtualizationScope = {
|
|
7757
|
+
...this._virtualizationScope,
|
|
7758
|
+
mountedKeys: this._getMountedVirtualizationKeys(this._optionsContainer.children),
|
|
7759
|
+
registeredKeys: Array.from(this._engine.state.registeredKeys),
|
|
7760
|
+
selectedKeys: new Set(this._engine.state.selectedKeys),
|
|
7761
|
+
activeKey: this._engine.state.activeKey,
|
|
7762
|
+
};
|
|
7763
|
+
}
|
|
7764
|
+
_appendMountedVirtualizationTargets(targets) {
|
|
7765
|
+
const mountedKeys = this._getMountedVirtualizationKeys(targets);
|
|
7766
|
+
this._virtualizationScope = {
|
|
7767
|
+
...this._virtualizationScope,
|
|
7768
|
+
mountedKeys: [...new Set([...this._virtualizationScope.mountedKeys, ...mountedKeys])],
|
|
7769
|
+
registeredKeys: Array.from(this._engine.state.registeredKeys),
|
|
7770
|
+
selectedKeys: new Set(this._engine.state.selectedKeys),
|
|
7771
|
+
activeKey: this._engine.state.activeKey,
|
|
7772
|
+
};
|
|
7773
|
+
}
|
|
7774
|
+
_createLegacyRenderPlan(filteredIndices) {
|
|
7775
|
+
const indices = [...filteredIndices];
|
|
7776
|
+
const filteredKeys = indices
|
|
7777
|
+
.map((index) => this._itemRegistry.indexToKey.get(index))
|
|
7778
|
+
.filter((key) => key !== undefined);
|
|
7779
|
+
const filteredIndexByKey = new Map();
|
|
7780
|
+
filteredKeys.forEach((key, filteredIndex) => filteredIndexByKey.set(key, filteredIndex));
|
|
7781
|
+
return {
|
|
7782
|
+
mode: 'legacy-full',
|
|
7783
|
+
filteredIndices: indices,
|
|
7784
|
+
filteredKeys,
|
|
7785
|
+
intendedRenderKeys: filteredKeys.slice(),
|
|
7786
|
+
filteredIndexByKey,
|
|
7787
|
+
};
|
|
7788
|
+
}
|
|
7789
|
+
_createVirtualRenderPlan(startIndex, endIndex) {
|
|
7790
|
+
const filteredKeys = this._renderPlan.filteredKeys.slice();
|
|
7791
|
+
const intendedRenderKeys = computeWindowKeys(filteredKeys, startIndex, endIndex);
|
|
7792
|
+
const filteredIndices = intendedRenderKeys
|
|
7793
|
+
.map((key) => this._itemRegistry.keyToIndex.get(key))
|
|
7794
|
+
.filter((index) => index !== undefined);
|
|
7795
|
+
return {
|
|
7796
|
+
mode: 'virtual-window',
|
|
7797
|
+
filteredIndices,
|
|
7798
|
+
filteredKeys,
|
|
7799
|
+
intendedRenderKeys,
|
|
7800
|
+
filteredIndexByKey: new Map(this._renderPlan.filteredIndexByKey),
|
|
7801
|
+
};
|
|
7802
|
+
}
|
|
7803
|
+
_ensureGuardedVirtualizer(filteredKeys) {
|
|
7804
|
+
const keysChanged = this._guardedVirtualizer !== null && (filteredKeys.length !== this._guardedVirtualizerKeys.length
|
|
7805
|
+
|| filteredKeys.some((key, index) => !Object.is(key, this._guardedVirtualizerKeys[index])));
|
|
7806
|
+
if (keysChanged)
|
|
7807
|
+
this._destroyGuardedVirtualizer(true);
|
|
7808
|
+
if (!this._guardedVirtualizer) {
|
|
7809
|
+
// The controller only calculates ranges and scroll intents. Give it a
|
|
7810
|
+
// detached container so its internal styling never touches rendered UI.
|
|
7811
|
+
this._guardedVirtualizer = new Virtualizer(document.createElement('div'), filteredKeys.length, (index) => this._renderPlan.filteredKeys[index], {
|
|
7812
|
+
estimatedItemHeight: this._resolveGuardedEstimatedItemHeight(),
|
|
7813
|
+
buffer: 0,
|
|
7814
|
+
measurementThreshold: 0,
|
|
7815
|
+
});
|
|
7816
|
+
this._guardedVirtualScrollTop = this._optionsContainer.scrollTop;
|
|
7817
|
+
this._guardedVirtualViewportHeight = this._resolveGuardedViewportHeight();
|
|
7818
|
+
this._optionsContainer.addEventListener('scroll', this._onGuardedVirtualScroll, { passive: true });
|
|
7819
|
+
this._guardedScrollListenerAttached = true;
|
|
7820
|
+
}
|
|
7821
|
+
this._guardedVirtualizerKeys = [...filteredKeys];
|
|
7822
|
+
return this._guardedVirtualizer;
|
|
7823
|
+
}
|
|
7824
|
+
_resolveGuardedEstimatedItemHeight() {
|
|
7825
|
+
const configuredHeight = this._config.estimatedItemHeight;
|
|
7826
|
+
if (typeof configuredHeight === 'number' && Number.isFinite(configuredHeight) && configuredHeight > 0) {
|
|
7827
|
+
return configuredHeight;
|
|
7828
|
+
}
|
|
7829
|
+
const renderedOption = this._optionsContainer.querySelector('[part="option"]');
|
|
7830
|
+
const measuredHeight = renderedOption?.getBoundingClientRect().height || renderedOption?.offsetHeight;
|
|
7831
|
+
if (typeof measuredHeight === 'number' && Number.isFinite(measuredHeight) && measuredHeight > 0) {
|
|
7832
|
+
return measuredHeight;
|
|
7833
|
+
}
|
|
7834
|
+
return 48;
|
|
7835
|
+
}
|
|
7836
|
+
_resolveGuardedViewportHeight() {
|
|
7837
|
+
const candidates = [
|
|
7838
|
+
this._optionsContainer.clientHeight,
|
|
7839
|
+
this._optionsContainer.getBoundingClientRect().height,
|
|
7840
|
+
];
|
|
7841
|
+
return candidates.find((height) => Number.isFinite(height) && height > 0) ?? 0;
|
|
7842
|
+
}
|
|
7843
|
+
_clampGuardedScrollPosition(itemCount, viewportHeight) {
|
|
7844
|
+
if (!this._guardedVirtualizer)
|
|
7845
|
+
return 0;
|
|
7846
|
+
const totalHeight = this._guardedVirtualizer.cumulativeOffset(itemCount);
|
|
7847
|
+
const maxScrollTop = Math.max(0, totalHeight - viewportHeight);
|
|
7848
|
+
const scrollTop = Math.min(Math.max(0, this._guardedVirtualScrollTop), maxScrollTop);
|
|
7849
|
+
this._guardedVirtualScrollTop = scrollTop;
|
|
7850
|
+
if (this._optionsContainer.scrollTop !== scrollTop) {
|
|
7851
|
+
this._optionsContainer.scrollTop = scrollTop;
|
|
7852
|
+
}
|
|
7853
|
+
return scrollTop;
|
|
7854
|
+
}
|
|
7855
|
+
_createGuardedSpacer(position, height) {
|
|
7856
|
+
const spacer = document.createElement('div');
|
|
7857
|
+
spacer.setAttribute('data-virtual-spacer', position);
|
|
7858
|
+
spacer.setAttribute('aria-hidden', 'true');
|
|
7859
|
+
spacer.setAttribute('role', 'presentation');
|
|
7860
|
+
spacer.style.height = `${Number.isFinite(height) ? Math.max(0, height) : 0}px`;
|
|
7861
|
+
spacer.style.minHeight = '0';
|
|
7862
|
+
spacer.style.flex = '0 0 auto';
|
|
7863
|
+
spacer.style.pointerEvents = 'none';
|
|
7864
|
+
return spacer;
|
|
7865
|
+
}
|
|
7866
|
+
_appendGuardedTopSpacer() {
|
|
7867
|
+
const virtualizer = this._guardedVirtualizer;
|
|
7868
|
+
const window = this._guardedVirtualWindow;
|
|
7869
|
+
if (!virtualizer || !window)
|
|
7870
|
+
return;
|
|
7871
|
+
this._optionsContainer.appendChild(this._createGuardedSpacer('top', virtualizer.cumulativeOffset(window.startIndex)));
|
|
7872
|
+
}
|
|
7873
|
+
_appendGuardedBottomSpacer() {
|
|
7874
|
+
const virtualizer = this._guardedVirtualizer;
|
|
7875
|
+
const window = this._guardedVirtualWindow;
|
|
7876
|
+
if (!virtualizer || !window)
|
|
7877
|
+
return;
|
|
7878
|
+
const totalHeight = virtualizer.cumulativeOffset(this._renderPlan.filteredKeys.length);
|
|
7879
|
+
const endOffset = virtualizer.cumulativeOffset(window.endIndex);
|
|
7880
|
+
this._optionsContainer.appendChild(this._createGuardedSpacer('bottom', Math.max(0, totalHeight - endOffset)));
|
|
7881
|
+
}
|
|
7882
|
+
_resolveGuardedOverscan() {
|
|
7883
|
+
return 2;
|
|
7884
|
+
}
|
|
7885
|
+
_applyGuardedWindowUpdate(update, overscan = 0) {
|
|
7886
|
+
if (!this._guardedVirtualizer)
|
|
7887
|
+
return false;
|
|
7888
|
+
this._guardedVirtualScrollTop = update.scrollTop;
|
|
7889
|
+
this._guardedVirtualViewportHeight = update.viewportHeight;
|
|
7890
|
+
const itemCount = this._renderPlan.filteredKeys.length;
|
|
7891
|
+
const nextWindow = {
|
|
7892
|
+
startIndex: Math.max(0, update.window.startIndex - overscan),
|
|
7893
|
+
endIndex: Math.min(itemCount, update.window.endIndex + 1 + overscan),
|
|
7894
|
+
};
|
|
7895
|
+
const changed = this._guardedVirtualWindow?.startIndex !== nextWindow.startIndex
|
|
7896
|
+
|| this._guardedVirtualWindow?.endIndex !== nextWindow.endIndex;
|
|
7897
|
+
this._guardedVirtualWindow = nextWindow;
|
|
7898
|
+
return changed;
|
|
7899
|
+
}
|
|
7900
|
+
_scheduleGuardedVirtualWindowUpdate() {
|
|
7901
|
+
if (!this._guardedVirtualizer || this._guardedVirtualWindowRafId !== null)
|
|
7902
|
+
return;
|
|
7903
|
+
this._guardedVirtualWindowRafId = this._requestLifecycleFrame(() => {
|
|
7904
|
+
this._guardedVirtualWindowRafId = null;
|
|
7905
|
+
const virtualizer = this._guardedVirtualizer;
|
|
7906
|
+
if (!virtualizer)
|
|
7907
|
+
return;
|
|
7908
|
+
const update = virtualizer.updateWindow(this._optionsContainer.scrollTop, this._resolveGuardedViewportHeight());
|
|
7909
|
+
this._guardedVirtualOverscan = this._resolveGuardedOverscan();
|
|
7910
|
+
if (update && this._applyGuardedWindowUpdate(update, this._guardedVirtualOverscan)) {
|
|
7911
|
+
this._renderOptions();
|
|
7912
|
+
}
|
|
7913
|
+
});
|
|
7914
|
+
}
|
|
7915
|
+
_cancelGuardedVirtualWindowUpdate() {
|
|
7916
|
+
if (this._guardedVirtualWindowRafId === null)
|
|
7917
|
+
return;
|
|
7918
|
+
cancelAnimationFrame(this._guardedVirtualWindowRafId);
|
|
7919
|
+
this._lifecycleRafs.delete(this._guardedVirtualWindowRafId);
|
|
7920
|
+
this._guardedVirtualWindowRafId = null;
|
|
7921
|
+
}
|
|
7922
|
+
_applyGuardedScrollResult(result) {
|
|
7923
|
+
if (!result || !this._guardedVirtualizer)
|
|
7924
|
+
return false;
|
|
7925
|
+
this._guardedVirtualScrollTop = result.scrollTop;
|
|
7926
|
+
this._optionsContainer.scrollTop = result.scrollTop;
|
|
7927
|
+
const update = this._guardedVirtualizer.updateWindow(this._guardedVirtualScrollTop, this._guardedVirtualViewportHeight);
|
|
7928
|
+
if (!update)
|
|
7929
|
+
return false;
|
|
7930
|
+
this._guardedVirtualWindow = {
|
|
7931
|
+
startIndex: update.window.startIndex,
|
|
7932
|
+
endIndex: update.window.endIndex + 1,
|
|
7933
|
+
};
|
|
7934
|
+
this._guardedVirtualOverscan = 0;
|
|
7935
|
+
return true;
|
|
7936
|
+
}
|
|
7937
|
+
_updateGuardedVirtualWindow(scrollTop, viewportHeight) {
|
|
7938
|
+
this._guardedVirtualizationRequested = true;
|
|
7939
|
+
const virtualizer = this._ensureGuardedVirtualizer(this._renderPlan.filteredKeys);
|
|
7940
|
+
this._guardedVirtualScrollTop = scrollTop;
|
|
7941
|
+
this._guardedVirtualViewportHeight = viewportHeight;
|
|
7942
|
+
const update = virtualizer.updateWindow(scrollTop, viewportHeight);
|
|
7943
|
+
if (!update)
|
|
7944
|
+
return false;
|
|
7945
|
+
this._guardedVirtualOverscan = 0;
|
|
7946
|
+
this._applyGuardedWindowUpdate(update, this._guardedVirtualOverscan);
|
|
7947
|
+
this._optionsContainer.scrollTop = update.scrollTop;
|
|
7948
|
+
return true;
|
|
7949
|
+
}
|
|
7950
|
+
_destroyGuardedVirtualizer(preserveRequestedMode = false) {
|
|
7951
|
+
this._cancelGuardedVirtualWindowUpdate();
|
|
7952
|
+
this._guardedMeasurementGeneration += 1;
|
|
7953
|
+
if (this._guardedScrollListenerAttached) {
|
|
7954
|
+
this._optionsContainer.removeEventListener('scroll', this._onGuardedVirtualScroll);
|
|
7955
|
+
this._guardedScrollListenerAttached = false;
|
|
7956
|
+
}
|
|
7957
|
+
this._guardedVirtualizer?.destroy();
|
|
7958
|
+
this._guardedVirtualizer = null;
|
|
7959
|
+
this._teardownGuardedOptionResizeObserver();
|
|
7960
|
+
if (!preserveRequestedMode)
|
|
7961
|
+
this._guardedVirtualizationRequested = false;
|
|
7962
|
+
this._guardedVirtualizerKeys = [];
|
|
7963
|
+
this._guardedVirtualWindow = null;
|
|
7964
|
+
this._guardedVirtualScrollTop = 0;
|
|
7965
|
+
this._guardedVirtualViewportHeight = 0;
|
|
7966
|
+
this._guardedVirtualOverscan = 0;
|
|
7967
|
+
}
|
|
7968
|
+
_invalidateGuardedMeasurements() {
|
|
7969
|
+
if (this._guardedVirtualizer)
|
|
7970
|
+
this._destroyGuardedVirtualizer(true);
|
|
7971
|
+
}
|
|
7972
|
+
_createGuardedOptionResizeObserver(generation) {
|
|
7973
|
+
if (typeof ResizeObserver === 'undefined')
|
|
7974
|
+
return;
|
|
7975
|
+
this._guardedOptionResizeObserver = new ResizeObserver((entries) => {
|
|
7976
|
+
if (generation !== this._guardedMeasurementGeneration
|
|
7977
|
+
|| !this._guardedVirtualizer
|
|
7978
|
+
|| !this._guardedVirtualizationRequested
|
|
7979
|
+
|| !this.isConnected)
|
|
7980
|
+
return;
|
|
7981
|
+
const virtualizer = this._guardedVirtualizer;
|
|
7982
|
+
const scrollTop = this._optionsContainer.scrollTop;
|
|
7983
|
+
const viewportHeight = this._resolveGuardedViewportHeight();
|
|
7984
|
+
const anchor = this._captureGuardedScrollAnchor(virtualizer, scrollTop, viewportHeight);
|
|
7985
|
+
let changed = false;
|
|
7986
|
+
for (const entry of entries) {
|
|
7987
|
+
const option = entry.target;
|
|
7988
|
+
if (!(option instanceof HTMLElement) || !this._guardedObservedOptions.has(option))
|
|
7989
|
+
continue;
|
|
7990
|
+
if (!option.isConnected || !this._optionsContainer.contains(option))
|
|
7991
|
+
continue;
|
|
7992
|
+
const meta = this._guardedOptionMeasurements.get(option);
|
|
7993
|
+
if (!meta || meta.generation !== generation)
|
|
7994
|
+
continue;
|
|
7995
|
+
const dataIndex = Number(option.getAttribute('data-index'));
|
|
7996
|
+
if (!Number.isInteger(dataIndex) || !Object.is(this._itemRegistry.indexToKey.get(dataIndex), meta.key))
|
|
7997
|
+
continue;
|
|
7998
|
+
const currentIndex = this._renderPlan.filteredIndexByKey.get(meta.key);
|
|
7999
|
+
if (currentIndex === undefined || currentIndex !== meta.index)
|
|
8000
|
+
continue;
|
|
8001
|
+
const reportedHeight = entry.borderBoxSize?.[0]?.blockSize;
|
|
8002
|
+
const height = typeof reportedHeight === 'number' ? reportedHeight : entry.contentRect.height;
|
|
8003
|
+
if (!Number.isFinite(height) || height <= 0)
|
|
8004
|
+
continue;
|
|
8005
|
+
changed = virtualizer.measureItem(meta.index, height) || changed;
|
|
8006
|
+
}
|
|
8007
|
+
if (!changed)
|
|
8008
|
+
return;
|
|
8009
|
+
if (anchor) {
|
|
8010
|
+
const currentAnchorIndex = this._renderPlan.filteredIndexByKey.get(anchor.key);
|
|
8011
|
+
if (currentAnchorIndex !== undefined) {
|
|
8012
|
+
const anchoredScrollTop = Math.max(0, virtualizer.cumulativeOffset(currentAnchorIndex) + anchor.offset);
|
|
8013
|
+
this._guardedVirtualScrollTop = anchoredScrollTop;
|
|
8014
|
+
this._optionsContainer.scrollTop = anchoredScrollTop;
|
|
8015
|
+
}
|
|
8016
|
+
}
|
|
8017
|
+
const update = virtualizer.updateWindow(this._optionsContainer.scrollTop, viewportHeight);
|
|
8018
|
+
if (update)
|
|
8019
|
+
this._applyGuardedWindowUpdate(update, this._guardedVirtualOverscan);
|
|
8020
|
+
this._renderOptions();
|
|
8021
|
+
});
|
|
8022
|
+
}
|
|
8023
|
+
_captureGuardedScrollAnchor(virtualizer, scrollTop, viewportHeight) {
|
|
8024
|
+
const viewportTop = this._optionsContainer.getBoundingClientRect().top + this._optionsContainer.clientTop;
|
|
8025
|
+
const viewportBottom = viewportTop + viewportHeight;
|
|
8026
|
+
const visible = Array.from(this._guardedObservedOptions)
|
|
8027
|
+
.map((option) => ({ option, rect: option.getBoundingClientRect() }))
|
|
8028
|
+
.filter(({ rect }) => rect.height > 0 && rect.bottom > viewportTop && rect.top < viewportBottom)
|
|
8029
|
+
.sort((left, right) => left.rect.top - right.rect.top)[0];
|
|
8030
|
+
const visibleMeta = visible ? this._guardedOptionMeasurements.get(visible.option) : undefined;
|
|
8031
|
+
if (visibleMeta && visibleMeta.generation === this._guardedMeasurementGeneration) {
|
|
8032
|
+
return {
|
|
8033
|
+
key: visibleMeta.key,
|
|
8034
|
+
offset: scrollTop - virtualizer.cumulativeOffset(visibleMeta.index),
|
|
8035
|
+
};
|
|
8036
|
+
}
|
|
8037
|
+
if (this._renderPlan.filteredKeys.length === 0)
|
|
8038
|
+
return null;
|
|
8039
|
+
const index = virtualizer.computeWindow(scrollTop, 0).startIndex;
|
|
8040
|
+
const key = this._renderPlan.filteredKeys[index];
|
|
8041
|
+
return key === undefined ? null : {
|
|
8042
|
+
key,
|
|
8043
|
+
offset: scrollTop - virtualizer.cumulativeOffset(index),
|
|
8044
|
+
};
|
|
8045
|
+
}
|
|
8046
|
+
_syncGuardedOptionResizeTargets() {
|
|
8047
|
+
const virtualizer = this._guardedVirtualizer;
|
|
8048
|
+
if (!virtualizer || !this._guardedVirtualizationRequested || !this.isConnected)
|
|
8049
|
+
return;
|
|
8050
|
+
const candidates = [];
|
|
8051
|
+
for (const child of Array.from(this._optionsContainer.children)) {
|
|
8052
|
+
if (!(child instanceof HTMLElement) || !child.hasAttribute('data-index'))
|
|
8053
|
+
continue;
|
|
8054
|
+
const dataIndex = Number(child.getAttribute('data-index'));
|
|
8055
|
+
if (!Number.isInteger(dataIndex))
|
|
8056
|
+
continue;
|
|
8057
|
+
const key = this._itemRegistry.indexToKey.get(dataIndex);
|
|
8058
|
+
const index = key === undefined ? undefined : this._renderPlan.filteredIndexByKey.get(key);
|
|
8059
|
+
if (key === undefined || index === undefined)
|
|
8060
|
+
continue;
|
|
8061
|
+
candidates.push({ option: child, key, index });
|
|
8062
|
+
}
|
|
8063
|
+
const reusedTarget = candidates.some(({ option }) => this._guardedObserverSeenOptions.has(option));
|
|
8064
|
+
if (reusedTarget && this._guardedOptionResizeObserver) {
|
|
8065
|
+
this._guardedMeasurementGeneration += 1;
|
|
8066
|
+
this._teardownGuardedOptionResizeObserver();
|
|
8067
|
+
}
|
|
8068
|
+
if (!this._guardedOptionResizeObserver) {
|
|
8069
|
+
this._createGuardedOptionResizeObserver(this._guardedMeasurementGeneration);
|
|
8070
|
+
}
|
|
8071
|
+
const observer = this._guardedOptionResizeObserver;
|
|
8072
|
+
if (!observer)
|
|
8073
|
+
return;
|
|
8074
|
+
const current = new Set();
|
|
8075
|
+
for (const { option, key, index } of candidates) {
|
|
8076
|
+
current.add(option);
|
|
8077
|
+
this._guardedOptionMeasurements.set(option, {
|
|
8078
|
+
key,
|
|
8079
|
+
index,
|
|
8080
|
+
generation: this._guardedMeasurementGeneration,
|
|
8081
|
+
});
|
|
8082
|
+
if (!this._guardedObservedOptions.has(option)) {
|
|
8083
|
+
observer.observe(option);
|
|
8084
|
+
this._guardedObserverSeenOptions.add(option);
|
|
8085
|
+
}
|
|
8086
|
+
}
|
|
8087
|
+
for (const previous of this._guardedObservedOptions) {
|
|
8088
|
+
if (!current.has(previous))
|
|
8089
|
+
observer.unobserve(previous);
|
|
8090
|
+
}
|
|
8091
|
+
this._guardedObservedOptions = current;
|
|
8092
|
+
}
|
|
8093
|
+
_teardownGuardedOptionResizeObserver() {
|
|
8094
|
+
for (const option of this._guardedObservedOptions) {
|
|
8095
|
+
this._guardedOptionResizeObserver?.unobserve(option);
|
|
8096
|
+
}
|
|
8097
|
+
this._guardedObservedOptions.clear();
|
|
8098
|
+
this._guardedObserverSeenOptions = new WeakSet();
|
|
8099
|
+
this._guardedOptionResizeObserver?.disconnect();
|
|
8100
|
+
this._guardedOptionResizeObserver = null;
|
|
8101
|
+
this._guardedOptionMeasurements = new WeakMap();
|
|
8102
|
+
}
|
|
8103
|
+
_materializeGuardedVirtualKey(key) {
|
|
8104
|
+
if (!this._guardedVirtualizer)
|
|
8105
|
+
return false;
|
|
8106
|
+
const targetIndex = this._renderPlan.filteredIndexByKey.get(key);
|
|
8107
|
+
if (targetIndex === undefined)
|
|
8108
|
+
return false;
|
|
8109
|
+
this._ensureGuardedVirtualizer(this._renderPlan.filteredKeys);
|
|
8110
|
+
const result = this._guardedVirtualizer.scrollToKey(key, (candidate) => this._renderPlan.filteredIndexByKey.get(candidate));
|
|
8111
|
+
if (!this._applyGuardedScrollResult(result))
|
|
8112
|
+
return false;
|
|
8113
|
+
this._renderOptions();
|
|
8114
|
+
return true;
|
|
8115
|
+
}
|
|
8116
|
+
_refreshVirtualizationScope(filteredIndices) {
|
|
8117
|
+
this._virtualizationScope = this._deriveVirtualizationScope(filteredIndices);
|
|
8118
|
+
}
|
|
6177
8119
|
_renderOptions() {
|
|
8120
|
+
this._ensureRegistryCurrent();
|
|
8121
|
+
const scrollTopBeforeRender = this._optionsContainer.scrollTop;
|
|
6178
8122
|
this._renderCycleId += 1;
|
|
6179
8123
|
const renderCycleId = this._renderCycleId;
|
|
8124
|
+
if (this._guardedVirtualizer) {
|
|
8125
|
+
this._guardedVirtualScrollTop = this._optionsContainer.scrollTop;
|
|
8126
|
+
}
|
|
6180
8127
|
// Cleanup observer
|
|
6181
8128
|
if (this._loadMoreTrigger && this._intersectionObserver) {
|
|
6182
8129
|
this._intersectionObserver.unobserve(this._loadMoreTrigger);
|
|
6183
8130
|
}
|
|
8131
|
+
this._unobserveGuardedOptionResizeTargets();
|
|
6184
8132
|
// Clear options container
|
|
6185
8133
|
this._optionsContainer.innerHTML = '';
|
|
8134
|
+
let filteredIndicesForScope = [];
|
|
8135
|
+
let guardedScrollTopToRestore = null;
|
|
6186
8136
|
// Ensure dropdown only contains options container (cleanup legacy direct children)
|
|
6187
8137
|
// We need to preserve optionsContainer, so we can't just clear dropdown.innerHTML
|
|
6188
8138
|
// But we can check if there are other children and remove them
|
|
@@ -6197,6 +8147,10 @@
|
|
|
6197
8147
|
}
|
|
6198
8148
|
// Show searching state (exclusive state)
|
|
6199
8149
|
if (this._state.isSearching) {
|
|
8150
|
+
this._registerShadowItems([]);
|
|
8151
|
+
this._renderPlan = this._createLegacyRenderPlan([]);
|
|
8152
|
+
this._refreshVirtualizationScope(filteredIndicesForScope);
|
|
8153
|
+
this._syncLegacyActiveAria();
|
|
6200
8154
|
const searching = document.createElement('div');
|
|
6201
8155
|
searching.className = 'searching-state';
|
|
6202
8156
|
searching.setAttribute('part', 'loading');
|
|
@@ -6208,8 +8162,17 @@
|
|
|
6208
8162
|
const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
6209
8163
|
// Filter items by search query
|
|
6210
8164
|
const query = this._state.searchQuery.toLowerCase();
|
|
8165
|
+
if (this._guardedMeasurementQuery !== null && this._guardedMeasurementQuery !== query) {
|
|
8166
|
+
this._invalidateGuardedMeasurements();
|
|
8167
|
+
}
|
|
8168
|
+
this._guardedMeasurementQuery = query;
|
|
6211
8169
|
// Handle Grouped Items Rendering (when no search query)
|
|
6212
8170
|
if (this._state.groupedItems.length > 0 && !query) {
|
|
8171
|
+
this._destroyGuardedVirtualizer();
|
|
8172
|
+
this._renderPlan = this._createLegacyRenderPlan(this._itemRegistry.records.map((record) => record.index));
|
|
8173
|
+
filteredIndicesForScope = this._renderPlan.filteredIndices;
|
|
8174
|
+
this._registerShadowItems(filteredIndicesForScope);
|
|
8175
|
+
this._refreshVirtualizationScope(filteredIndicesForScope);
|
|
6213
8176
|
this._state.groupedItems.forEach((group, groupIndex) => {
|
|
6214
8177
|
let header;
|
|
6215
8178
|
if (this.groupHeaderRenderer) {
|
|
@@ -6247,7 +8210,7 @@
|
|
|
6247
8210
|
}
|
|
6248
8211
|
else {
|
|
6249
8212
|
// Normal rendering (flat list or filtered)
|
|
6250
|
-
const
|
|
8213
|
+
const computedFilteredIndices = [];
|
|
6251
8214
|
this._state.loadedItems.forEach((item, index) => {
|
|
6252
8215
|
if (query) {
|
|
6253
8216
|
try {
|
|
@@ -6259,8 +8222,40 @@
|
|
|
6259
8222
|
return;
|
|
6260
8223
|
}
|
|
6261
8224
|
}
|
|
6262
|
-
|
|
8225
|
+
computedFilteredIndices.push(index);
|
|
6263
8226
|
});
|
|
8227
|
+
this._renderPlan = this._createLegacyRenderPlan(computedFilteredIndices);
|
|
8228
|
+
filteredIndicesForScope = this._renderPlan.filteredIndices;
|
|
8229
|
+
this._registerShadowItems(filteredIndicesForScope);
|
|
8230
|
+
if (this._guardedVirtualizationRequested && !this._guardedVirtualizer) {
|
|
8231
|
+
this._ensureGuardedVirtualizer(this._renderPlan.filteredKeys);
|
|
8232
|
+
this._guardedVirtualViewportHeight = this._resolveGuardedViewportHeight();
|
|
8233
|
+
// A data reorder/filter may have invalidated the old virtualizer, but
|
|
8234
|
+
// the physical position is still available before the DOM is cleared.
|
|
8235
|
+
this._guardedVirtualScrollTop = scrollTopBeforeRender;
|
|
8236
|
+
}
|
|
8237
|
+
if (this._guardedVirtualizer) {
|
|
8238
|
+
const virtualizer = this._ensureGuardedVirtualizer(this._renderPlan.filteredKeys);
|
|
8239
|
+
const measuredViewportHeight = this._resolveGuardedViewportHeight();
|
|
8240
|
+
if (measuredViewportHeight > 0) {
|
|
8241
|
+
this._guardedVirtualViewportHeight = measuredViewportHeight;
|
|
8242
|
+
}
|
|
8243
|
+
const scrollTop = this._clampGuardedScrollPosition(this._renderPlan.filteredKeys.length, this._guardedVirtualViewportHeight);
|
|
8244
|
+
guardedScrollTopToRestore = scrollTop;
|
|
8245
|
+
const update = virtualizer.updateWindow(scrollTop, this._guardedVirtualViewportHeight);
|
|
8246
|
+
if (update) {
|
|
8247
|
+
this._applyGuardedWindowUpdate(update, this._guardedVirtualOverscan);
|
|
8248
|
+
}
|
|
8249
|
+
else {
|
|
8250
|
+
this._guardedVirtualWindow = null;
|
|
8251
|
+
}
|
|
8252
|
+
}
|
|
8253
|
+
if (this._guardedVirtualWindow) {
|
|
8254
|
+
this._renderPlan = this._createVirtualRenderPlan(this._guardedVirtualWindow.startIndex, this._guardedVirtualWindow.endIndex);
|
|
8255
|
+
}
|
|
8256
|
+
const filteredIndices = this._renderPlan.filteredIndices;
|
|
8257
|
+
this._refreshVirtualizationScope(filteredIndicesForScope);
|
|
8258
|
+
this._appendGuardedTopSpacer();
|
|
6264
8259
|
if (filteredIndices.length === 0 && !this._state.isBusy) {
|
|
6265
8260
|
const empty = document.createElement('div');
|
|
6266
8261
|
empty.setAttribute('part', 'no-results');
|
|
@@ -6274,7 +8269,8 @@
|
|
|
6274
8269
|
this._optionsContainer.appendChild(empty);
|
|
6275
8270
|
}
|
|
6276
8271
|
else {
|
|
6277
|
-
const shouldIncrementalRender = this.
|
|
8272
|
+
const shouldIncrementalRender = !this._guardedVirtualizer
|
|
8273
|
+
&& this._config.virtualize !== false
|
|
6278
8274
|
&& this._state.groupedItems.length === 0
|
|
6279
8275
|
&& filteredIndices.length > 300;
|
|
6280
8276
|
if (shouldIncrementalRender) {
|
|
@@ -6282,7 +8278,7 @@
|
|
|
6282
8278
|
let cursor = 0;
|
|
6283
8279
|
let maxRenderTarget = 0;
|
|
6284
8280
|
if (this._state.selectedIndices.size > 0 && this._config.scrollToSelected.enabled) {
|
|
6285
|
-
const indices = Array.from(this._state.selectedIndices).sort((a, b) => a - b);
|
|
8281
|
+
const indices = Array.from(this._state.selectedIndices).filter((index) => index >= 0).sort((a, b) => a - b);
|
|
6286
8282
|
const targetIndex = this._config.scrollToSelected.multiSelectTarget === 'first' ? indices[0] : indices[indices.length - 1];
|
|
6287
8283
|
const filteredPos = filteredIndices.indexOf(targetIndex);
|
|
6288
8284
|
if (filteredPos !== -1) {
|
|
@@ -6300,9 +8296,13 @@
|
|
|
6300
8296
|
const item = this._state.loadedItems[itemIndex];
|
|
6301
8297
|
this._renderSingleOption(item, itemIndex, getValue, getLabel, fragment);
|
|
6302
8298
|
}
|
|
8299
|
+
const renderedTargets = Array.from(fragment.children);
|
|
6303
8300
|
this._optionsContainer.appendChild(fragment);
|
|
8301
|
+
this._appendMountedVirtualizationTargets(renderedTargets);
|
|
8302
|
+
this._syncRenderedOptionAccessibility();
|
|
8303
|
+
this._syncLegacyActiveAria();
|
|
6304
8304
|
if (cursor < filteredIndices.length) {
|
|
6305
|
-
|
|
8305
|
+
this._requestLifecycleFrame(renderChunk);
|
|
6306
8306
|
}
|
|
6307
8307
|
else {
|
|
6308
8308
|
if (renderCycleId !== this._renderCycleId)
|
|
@@ -6321,7 +8321,9 @@
|
|
|
6321
8321
|
this._renderSingleOption(item, itemIndex, getValue, getLabel);
|
|
6322
8322
|
});
|
|
6323
8323
|
}
|
|
8324
|
+
this._appendGuardedBottomSpacer();
|
|
6324
8325
|
}
|
|
8326
|
+
this._refreshMountedVirtualizationScope();
|
|
6325
8327
|
// Append Busy Indicator if busy
|
|
6326
8328
|
if (this._state.isBusy && this._config.busyBucket.enabled) {
|
|
6327
8329
|
const busyBucket = document.createElement('div');
|
|
@@ -6343,8 +8345,23 @@
|
|
|
6343
8345
|
else if ((this._config.loadMore.enabled || this._config.infiniteScroll.enabled) && this._state.loadedItems.length > 0) {
|
|
6344
8346
|
this._addLoadMoreTrigger();
|
|
6345
8347
|
}
|
|
8348
|
+
// Rebuilding the list temporarily removes its spacer geometry. Browsers
|
|
8349
|
+
// clamp scrollTop during that interval, so restore the logical position
|
|
8350
|
+
// only after the new top/bottom spacers and mounted rows are present.
|
|
8351
|
+
if (guardedScrollTopToRestore !== null) {
|
|
8352
|
+
this._optionsContainer.scrollTop = guardedScrollTopToRestore;
|
|
8353
|
+
}
|
|
8354
|
+
this._syncRenderedOptionAccessibility();
|
|
8355
|
+
this._syncLegacyActiveAria();
|
|
8356
|
+
this._syncGuardedOptionResizeTargets();
|
|
6346
8357
|
this._finalizePerfMarks();
|
|
6347
8358
|
}
|
|
8359
|
+
_unobserveGuardedOptionResizeTargets() {
|
|
8360
|
+
for (const option of this._guardedObservedOptions) {
|
|
8361
|
+
this._guardedOptionResizeObserver?.unobserve(option);
|
|
8362
|
+
}
|
|
8363
|
+
this._guardedObservedOptions.clear();
|
|
8364
|
+
}
|
|
6348
8365
|
_renderSingleOption(item, index, getValue, getLabel, targetContainer = this._optionsContainer) {
|
|
6349
8366
|
const isSelected = this._state.selectedIndices.has(index);
|
|
6350
8367
|
const isDisabled = Boolean(item?.disabled);
|
|
@@ -6380,16 +8397,16 @@
|
|
|
6380
8397
|
showRemoveButton: this._config.selection.mode === 'multi' && this._config.selection.showRemoveButton,
|
|
6381
8398
|
removeButtonIcon: this._config.selection.removeButtonIcon,
|
|
6382
8399
|
classMap: this.classMap,
|
|
6383
|
-
className: this._config.styles
|
|
8400
|
+
className: this._config.styles?.classNames?.option,
|
|
6384
8401
|
});
|
|
6385
|
-
if (this._config.styles
|
|
8402
|
+
if (this._config.styles?.classNames?.option) {
|
|
6386
8403
|
option.classList.add(...this._config.styles.classNames.option.split(' ').filter(Boolean));
|
|
6387
8404
|
}
|
|
6388
8405
|
option.setAttribute('dir', this._config.direction ?? 'ltr');
|
|
6389
|
-
if (isSelected && this._config.styles
|
|
8406
|
+
if (isSelected && this._config.styles?.classNames?.selectedOption) {
|
|
6390
8407
|
option.classList.add(...this._config.styles.classNames.selectedOption.split(' ').filter(Boolean));
|
|
6391
8408
|
}
|
|
6392
|
-
if (this._state.activeIndex === index && this._config.styles
|
|
8409
|
+
if (this._state.activeIndex === index && this._config.styles?.classNames?.activeOption) {
|
|
6393
8410
|
option.classList.add(...this._config.styles.classNames.activeOption.split(' ').filter(Boolean));
|
|
6394
8411
|
}
|
|
6395
8412
|
if (isDisabled && this._config.styles.classNames?.disabledOption) {
|
|
@@ -6519,16 +8536,18 @@
|
|
|
6519
8536
|
if (meta.value != null) {
|
|
6520
8537
|
optionEl.dataset.smValue = String(meta.value);
|
|
6521
8538
|
}
|
|
6522
|
-
|
|
6523
|
-
|
|
6524
|
-
optionEl.setAttribute('role', 'option');
|
|
8539
|
+
else {
|
|
8540
|
+
delete optionEl.dataset.smValue;
|
|
6525
8541
|
}
|
|
6526
|
-
|
|
6527
|
-
|
|
6528
|
-
|
|
8542
|
+
// Refresh component-generated IDs when a renderer reuses an option node,
|
|
8543
|
+
// while preserving an ID that the renderer supplied itself.
|
|
8544
|
+
const previousNormalizedId = this._customOptionNormalizedIds.get(optionEl);
|
|
8545
|
+
if (!optionEl.id || (previousNormalizedId !== undefined && optionEl.id === previousNormalizedId)) {
|
|
8546
|
+
optionEl.id = meta.id;
|
|
8547
|
+
this._customOptionNormalizedIds.set(optionEl, meta.id);
|
|
6529
8548
|
}
|
|
6530
8549
|
else {
|
|
6531
|
-
|
|
8550
|
+
this._customOptionNormalizedIds.delete(optionEl);
|
|
6532
8551
|
}
|
|
6533
8552
|
if (!optionEl.hasAttribute('tabindex')) {
|
|
6534
8553
|
optionEl.tabIndex = -1;
|
|
@@ -6578,6 +8597,8 @@
|
|
|
6578
8597
|
/** live set of all connected instances; used to auto-close siblings */
|
|
6579
8598
|
EnhancedSelect._instances = new Set();
|
|
6580
8599
|
EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS = 'smilodon-mirrored-styles-scope';
|
|
8600
|
+
EnhancedSelect._MIRRORED_STYLESHEET_CACHE_LIMIT = 32;
|
|
8601
|
+
EnhancedSelect._mirroredStylesheetTextCache = new Map();
|
|
6581
8602
|
// Register custom element
|
|
6582
8603
|
if (!customElements.get('enhanced-select')) {
|
|
6583
8604
|
customElements.define('enhanced-select', EnhancedSelect);
|
|
@@ -6935,7 +8956,8 @@
|
|
|
6935
8956
|
this.rafId = 0;
|
|
6936
8957
|
this.measuring = false;
|
|
6937
8958
|
this.observer = null;
|
|
6938
|
-
this.
|
|
8959
|
+
this.workTotal = 0;
|
|
8960
|
+
this.workCount = 0;
|
|
6939
8961
|
/**
|
|
6940
8962
|
* Measure frame timing via requestAnimationFrame
|
|
6941
8963
|
*/
|
|
@@ -6952,7 +8974,6 @@
|
|
|
6952
8974
|
this.lastFrameTime = now;
|
|
6953
8975
|
this.rafId = requestAnimationFrame(this.measureFrame);
|
|
6954
8976
|
};
|
|
6955
|
-
this.setupObserver();
|
|
6956
8977
|
}
|
|
6957
8978
|
/**
|
|
6958
8979
|
* Setup PerformanceObserver for long tasks
|
|
@@ -6962,12 +8983,15 @@
|
|
|
6962
8983
|
return;
|
|
6963
8984
|
try {
|
|
6964
8985
|
this.observer = new PerformanceObserver((list) => {
|
|
8986
|
+
if (!this.measuring)
|
|
8987
|
+
return;
|
|
6965
8988
|
for (const entry of list.getEntries()) {
|
|
6966
8989
|
if (entry.entryType === 'longtask' && entry.duration > 50) {
|
|
6967
8990
|
this.longTasks++;
|
|
6968
8991
|
}
|
|
6969
8992
|
if (entry.entryType === 'measure') {
|
|
6970
|
-
this.
|
|
8993
|
+
this.workTotal += entry.duration;
|
|
8994
|
+
this.workCount += 1;
|
|
6971
8995
|
}
|
|
6972
8996
|
}
|
|
6973
8997
|
});
|
|
@@ -6992,9 +9016,11 @@
|
|
|
6992
9016
|
return;
|
|
6993
9017
|
this.measuring = true;
|
|
6994
9018
|
this.frameTimes = [];
|
|
6995
|
-
this.
|
|
9019
|
+
this.workTotal = 0;
|
|
9020
|
+
this.workCount = 0;
|
|
6996
9021
|
this.longTasks = 0;
|
|
6997
9022
|
this.lastFrameTime = performance.now();
|
|
9023
|
+
this.setupObserver();
|
|
6998
9024
|
this.measureFrame();
|
|
6999
9025
|
}
|
|
7000
9026
|
/**
|
|
@@ -7006,6 +9032,10 @@
|
|
|
7006
9032
|
cancelAnimationFrame(this.rafId);
|
|
7007
9033
|
this.rafId = 0;
|
|
7008
9034
|
}
|
|
9035
|
+
if (this.observer) {
|
|
9036
|
+
this.observer.disconnect();
|
|
9037
|
+
this.observer = null;
|
|
9038
|
+
}
|
|
7009
9039
|
}
|
|
7010
9040
|
/**
|
|
7011
9041
|
* Mark start of work for custom measurement
|
|
@@ -7017,10 +9047,15 @@
|
|
|
7017
9047
|
* Mark end of work and measure duration
|
|
7018
9048
|
*/
|
|
7019
9049
|
markEnd(label) {
|
|
7020
|
-
|
|
7021
|
-
|
|
7022
|
-
|
|
7023
|
-
|
|
9050
|
+
const startMark = `${label}-start`;
|
|
9051
|
+
const endMark = `${label}-end`;
|
|
9052
|
+
performance.mark(endMark);
|
|
9053
|
+
const measure = performance.measure(label, startMark, endMark);
|
|
9054
|
+
const duration = measure ? measure.duration : 0;
|
|
9055
|
+
performance.clearMarks(startMark);
|
|
9056
|
+
performance.clearMarks(endMark);
|
|
9057
|
+
performance.clearMeasures(label);
|
|
9058
|
+
return duration;
|
|
7024
9059
|
}
|
|
7025
9060
|
/**
|
|
7026
9061
|
* Get current metrics snapshot
|
|
@@ -7032,8 +9067,8 @@
|
|
|
7032
9067
|
const fps = avgFrameTime > 0 ? 1000 / avgFrameTime : 0;
|
|
7033
9068
|
const longFrames = this.frameTimes.filter(t => t > 16.67).length;
|
|
7034
9069
|
const droppedFrames = this.frameTimes.filter(t => t > 33.33).length; // > 2 frames
|
|
7035
|
-
const avgMainThreadWork = this.
|
|
7036
|
-
? this.
|
|
9070
|
+
const avgMainThreadWork = this.workCount > 0
|
|
9071
|
+
? this.workTotal / this.workCount
|
|
7037
9072
|
: 0;
|
|
7038
9073
|
const metrics = {
|
|
7039
9074
|
frames: {
|
|
@@ -7092,7 +9127,8 @@
|
|
|
7092
9127
|
*/
|
|
7093
9128
|
reset() {
|
|
7094
9129
|
this.frameTimes = [];
|
|
7095
|
-
this.
|
|
9130
|
+
this.workTotal = 0;
|
|
9131
|
+
this.workCount = 0;
|
|
7096
9132
|
this.longTasks = 0;
|
|
7097
9133
|
}
|
|
7098
9134
|
/**
|
|
@@ -7597,33 +9633,48 @@
|
|
|
7597
9633
|
exports.CSPFeatures = CSPFeatures;
|
|
7598
9634
|
exports.CustomOptionPool = CustomOptionPool;
|
|
7599
9635
|
exports.DOMPool = DOMPool;
|
|
9636
|
+
exports.DefaultSelectEffectRunner = DefaultSelectEffectRunner;
|
|
7600
9637
|
exports.EnhancedSelect = EnhancedSelect;
|
|
7601
9638
|
exports.FenwickTree = FenwickTree;
|
|
7602
9639
|
exports.NativeSelectElement = NativeSelectElement;
|
|
7603
9640
|
exports.OptionRenderer = OptionRenderer;
|
|
7604
9641
|
exports.PerformanceTelemetry = PerformanceTelemetry;
|
|
9642
|
+
exports.SelectEngineBridge = SelectEngineBridge;
|
|
7605
9643
|
exports.SelectOption = SelectOption;
|
|
7606
9644
|
exports.Virtualizer = Virtualizer;
|
|
7607
9645
|
exports.WorkerManager = WorkerManager;
|
|
7608
9646
|
exports.applyClasses = applyClasses;
|
|
7609
9647
|
exports.applyDefaultTheme = applyDefaultTheme;
|
|
9648
|
+
exports.applySelectionRequest = applySelectionRequest;
|
|
7610
9649
|
exports.arrayIncludesValue = arrayIncludesValue;
|
|
7611
9650
|
exports.arraysEqualByValue = arraysEqualByValue;
|
|
7612
9651
|
exports.configureSelect = configureSelect;
|
|
7613
9652
|
exports.containsSuspiciousPatterns = containsSuspiciousPatterns;
|
|
9653
|
+
exports.createDefaultItemAdapter = createDefaultItemAdapter;
|
|
7614
9654
|
exports.createElement = createElement;
|
|
9655
|
+
exports.createInitialSelectState = createInitialSelectState;
|
|
9656
|
+
exports.createItemRegistry = createItemRegistry;
|
|
7615
9657
|
exports.createRendererHelpers = createRendererHelpers;
|
|
7616
9658
|
exports.createTextNode = createTextNode;
|
|
7617
9659
|
exports.defaultTheme = defaultTheme;
|
|
7618
9660
|
exports.detectEnvironment = detectEnvironment;
|
|
7619
9661
|
exports.escapeAttribute = escapeAttribute;
|
|
7620
9662
|
exports.escapeHTML = escapeHTML;
|
|
9663
|
+
exports.getActiveKey = getActiveKey;
|
|
7621
9664
|
exports.getCustomProperty = getCustomProperty;
|
|
9665
|
+
exports.getDefaultDuplicateKeyPolicy = getDefaultDuplicateKeyPolicy;
|
|
7622
9666
|
exports.getHTMLSanitizer = getHTMLSanitizer;
|
|
9667
|
+
exports.getSelectedKeys = getSelectedKeys;
|
|
7623
9668
|
exports.getTelemetry = getTelemetry;
|
|
7624
9669
|
exports.getWorkerManager = getWorkerManager;
|
|
7625
9670
|
exports.hasOverflowHiddenAncestor = hasOverflowHiddenAncestor;
|
|
9671
|
+
exports.hasSelection = hasSelection;
|
|
7626
9672
|
exports.injectShadowStyles = injectShadowStyles;
|
|
9673
|
+
exports.isDisabled = isDisabled;
|
|
9674
|
+
exports.isFocused = isFocused;
|
|
9675
|
+
exports.isOpen = isOpen;
|
|
9676
|
+
exports.isRegisteredKey = isRegisteredKey;
|
|
9677
|
+
exports.isSelected = isSelected;
|
|
7627
9678
|
exports.measureAsync = measureAsync;
|
|
7628
9679
|
exports.measureSync = measureSync;
|
|
7629
9680
|
exports.removeCustomProperty = removeCustomProperty;
|
|
@@ -7637,6 +9688,7 @@
|
|
|
7637
9688
|
exports.setTextContent = setTextContent;
|
|
7638
9689
|
exports.shadowDOMStyles = shadowDOMStyles;
|
|
7639
9690
|
exports.toggleClass = toggleClass;
|
|
9691
|
+
exports.transition = transition;
|
|
7640
9692
|
exports.validateTemplate = validateTemplate;
|
|
7641
9693
|
exports.valuesEqual = valuesEqual;
|
|
7642
9694
|
exports.warnCSPViolation = warnCSPViolation;
|