@smilodon/react 1.9.3 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,9 +1,31 @@
1
1
  "use client";
2
2
  import { jsx, Fragment } from "react/jsx-runtime";
3
- import React, { forwardRef, useRef, useMemo, useState, useCallback, useEffect, useImperativeHandle } from "react";
3
+ import React, { forwardRef, useState, useRef, useMemo, useCallback, useEffect, useImperativeHandle } from "react";
4
4
  import { createRoot } from "react-dom/client";
5
5
  import { configureSelect, resetSelectConfig, selectConfig } from "@smilodon/core";
6
+ const DEFAULT_DATA_LOADING_CONFIG = {
7
+ protocol: "legacy",
8
+ searchMode: "local",
9
+ paginationMode: "page",
10
+ firstPage: 1
11
+ };
12
+ function isPlainRecord(value) {
13
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
14
+ return false;
15
+ }
16
+ const prototype = Object.getPrototypeOf(value);
17
+ return prototype === Object.prototype || prototype === null;
18
+ }
19
+ function isBusyToken(value) {
20
+ return value === true || value === "true";
21
+ }
22
+ function composeBusyToken(consumerValue, externalBusy) {
23
+ if (externalBusy || isBusyToken(consumerValue)) return "true";
24
+ if (consumerValue === false || consumerValue === "false") return consumerValue;
25
+ return consumerValue;
26
+ }
6
27
  const Select = forwardRef((props, ref) => {
28
+ var _a, _b;
7
29
  const {
8
30
  items = [],
9
31
  groupedItems,
@@ -51,6 +73,9 @@ const Select = forwardRef((props, ref) => {
51
73
  onLoadMore,
52
74
  onClear,
53
75
  onDiagnostic,
76
+ onDataRequestCancel,
77
+ onDataProjection,
78
+ onDataLoadingConfigError,
54
79
  trackingEnabled = false,
55
80
  trackEvents = true,
56
81
  trackStyling = true,
@@ -64,22 +89,97 @@ const Select = forwardRef((props, ref) => {
64
89
  onCreate,
65
90
  classMap,
66
91
  groupHeaderRenderer,
92
+ "aria-busy": consumerAriaBusy,
67
93
  ...hostProps
68
94
  } = props;
95
+ const [isControlled] = useState(value !== void 0);
96
+ const [internalValue, setInternalValue] = useState(defaultValue);
97
+ const hasAppliedInitialValueRef = useRef(false);
69
98
  const elementRef = useRef(null);
99
+ const coreModuleRef = useRef(null);
100
+ const acceptedDataLoadingConfigRef = useRef({
101
+ ...DEFAULT_DATA_LOADING_CONFIG
102
+ });
103
+ const configUpdateStackRef = useRef([]);
104
+ const configUpdateInProgressRef = useRef(false);
105
+ const queuedConfigUpdatesRef = useRef([]);
106
+ const queuedImperativeConfigsRef = useRef([]);
107
+ const activeListenersRef = useRef(null);
108
+ const callbackStateRef = useRef({
109
+ onSelect,
110
+ onChange,
111
+ onOpen,
112
+ onClose,
113
+ onSearch,
114
+ onLoadMore,
115
+ onClear,
116
+ onDiagnostic,
117
+ onDataRequestCancel,
118
+ onDataProjection,
119
+ onDataLoadingConfigError,
120
+ isControlled,
121
+ multiple
122
+ });
123
+ callbackStateRef.current = {
124
+ onSelect,
125
+ onChange,
126
+ onOpen,
127
+ onClose,
128
+ onSearch,
129
+ onLoadMore,
130
+ onClear,
131
+ onDiagnostic,
132
+ onDataRequestCancel,
133
+ onDataProjection,
134
+ onDataLoadingConfigError,
135
+ isControlled,
136
+ multiple
137
+ };
138
+ const ariaBusyInputsRef = useRef({ consumerAriaBusy, loading });
139
+ ariaBusyInputsRef.current = { consumerAriaBusy, loading };
140
+ const initializingRef = useRef(true);
141
+ const compatibilityFailureReportedRef = useRef(false);
142
+ const unusableRef = useRef(false);
143
+ const [acceptedProtocol, setAcceptedProtocol] = useState("legacy");
144
+ const [isCoreModuleLoaded, setIsCoreModuleLoaded] = useState(false);
145
+ const [isAcknowledgedQualified, setIsAcknowledgedQualified] = useState(false);
146
+ const [isUnusable, setIsUnusable] = useState(false);
70
147
  const resolvedDropdownPlacement = useMemo(() => {
71
148
  if (placement == null ? void 0 : placement.startsWith("top")) return "top";
72
149
  return "bottom";
73
150
  }, [placement]);
74
- const [isControlled] = useState(value !== void 0);
75
- const [internalValue, setInternalValue] = useState(defaultValue);
76
- const hasAppliedInitialValueRef = useRef(false);
77
- const reactRendererCache = useRef(/* @__PURE__ */ new Map());
78
- const groupHeaderRendererCache = useRef(/* @__PURE__ */ new Map());
79
- const areValuesEqual = useCallback((nextValues, currentValues) => {
80
- if (nextValues.length !== currentValues.length) return false;
81
- return nextValues.every((value2, index) => value2 === currentValues[index]);
82
- }, []);
151
+ const reactRendererCache = useRef(
152
+ /* @__PURE__ */ new Map()
153
+ );
154
+ const groupHeaderRendererCache = useRef(
155
+ /* @__PURE__ */ new Map()
156
+ );
157
+ const areValuesEqual = useCallback(
158
+ (nextValues, currentValues) => {
159
+ if (nextValues.length !== currentValues.length) return false;
160
+ return nextValues.every((value2, index) => value2 === currentValues[index]);
161
+ },
162
+ []
163
+ );
164
+ const setElementPropertyIfChanged = useCallback(
165
+ (element, property, value2) => {
166
+ if (element[property] !== value2) {
167
+ element[property] = value2;
168
+ }
169
+ },
170
+ []
171
+ );
172
+ const areClassMapsEqual = useCallback(
173
+ (left, right) => {
174
+ if (left === right) return true;
175
+ if (!left || !right) return false;
176
+ const leftKeys = Object.keys(left);
177
+ const rightKeys = Object.keys(right);
178
+ if (leftKeys.length !== rightKeys.length) return false;
179
+ return leftKeys.every((key) => left[key] === right[key]);
180
+ },
181
+ []
182
+ );
83
183
  const scheduleRootUnmount = useCallback((root) => {
84
184
  if (typeof queueMicrotask === "function") {
85
185
  queueMicrotask(() => root.unmount());
@@ -96,10 +196,10 @@ const Select = forwardRef((props, ref) => {
96
196
  groupHeaderRendererRef.current = groupHeaderRenderer;
97
197
  }, [customRenderer, renderItem, optionRenderer, groupHeaderRenderer]);
98
198
  const resolvedOptionRenderer = useMemo(() => {
99
- if (!!optionRenderer) {
199
+ if (optionRenderer) {
100
200
  return (item, index, helpers) => {
101
- var _a;
102
- return ((_a = optionRendererRef.current) == null ? void 0 : _a.call(optionRendererRef, item, index, helpers)) || document.createElement("div");
201
+ var _a2;
202
+ return ((_a2 = optionRendererRef.current) == null ? void 0 : _a2.call(optionRendererRef, item, index, helpers)) || document.createElement("div");
103
203
  };
104
204
  }
105
205
  const hasReactRenderer = !!(customRenderer ?? renderItem);
@@ -136,9 +236,13 @@ const Select = forwardRef((props, ref) => {
136
236
  }, [!!groupHeaderRenderer]);
137
237
  useEffect(() => {
138
238
  return () => {
139
- reactRendererCache.current.forEach(({ root }) => scheduleRootUnmount(root));
239
+ reactRendererCache.current.forEach(
240
+ ({ root }) => scheduleRootUnmount(root)
241
+ );
140
242
  reactRendererCache.current.clear();
141
- groupHeaderRendererCache.current.forEach(({ root }) => scheduleRootUnmount(root));
243
+ groupHeaderRendererCache.current.forEach(
244
+ ({ root }) => scheduleRootUnmount(root)
245
+ );
142
246
  groupHeaderRendererCache.current.clear();
143
247
  };
144
248
  }, [scheduleRootUnmount]);
@@ -162,28 +266,337 @@ const Select = forwardRef((props, ref) => {
162
266
  }, [groupedItems, scheduleRootUnmount]);
163
267
  useEffect(() => {
164
268
  if (groupHeaderRenderer) return;
165
- groupHeaderRendererCache.current.forEach(({ root }) => scheduleRootUnmount(root));
269
+ groupHeaderRendererCache.current.forEach(
270
+ ({ root }) => scheduleRootUnmount(root)
271
+ );
166
272
  groupHeaderRendererCache.current.clear();
167
273
  }, [groupHeaderRenderer, scheduleRootUnmount]);
168
- const [isElementReady, setIsElementReady] = useState(false);
169
274
  useEffect(() => {
170
- if (typeof window !== "undefined") {
171
- if (customElements.get("enhanced-select")) {
172
- setIsElementReady(true);
173
- } else {
174
- import("@smilodon/core").then((module) => {
175
- if (!customElements.get("enhanced-select")) {
176
- customElements.define("enhanced-select", module.EnhancedSelect);
275
+ const frameworkRendererActive = !optionRenderer && Boolean(customRenderer ?? renderItem);
276
+ if (frameworkRendererActive) return;
277
+ reactRendererCache.current.forEach(({ root }) => scheduleRootUnmount(root));
278
+ reactRendererCache.current.clear();
279
+ }, [!!optionRenderer, !!(customRenderer ?? renderItem), scheduleRootUnmount]);
280
+ const [isElementReady, setIsElementReady] = useState(false);
281
+ const acknowledgedRequested = acceptedProtocol === "acknowledged" || ((_a = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _a.protocol) === "acknowledged";
282
+ const dataLoadingConfigured = (coreConfig == null ? void 0 : coreConfig.dataLoading) !== void 0;
283
+ const coreFacadeRequired = acknowledgedRequested || dataLoadingConfigured;
284
+ const isElementUsable = isElementReady && (!acknowledgedRequested || isAcknowledgedQualified) && (!coreFacadeRequired || isCoreModuleLoaded) && !isUnusable;
285
+ const markUnusable = useCallback(
286
+ (message) => {
287
+ if (unusableRef.current) return;
288
+ unusableRef.current = true;
289
+ setIsUnusable(true);
290
+ const element = elementRef.current;
291
+ const { consumerAriaBusy: currentConsumerBusy } = ariaBusyInputsRef.current;
292
+ if (element) {
293
+ if (currentConsumerBusy === void 0) element.removeAttribute("aria-busy");
294
+ else element.setAttribute("aria-busy", String(currentConsumerBusy));
295
+ }
296
+ if (!compatibilityFailureReportedRef.current) {
297
+ compatibilityFailureReportedRef.current = true;
298
+ console.error(`[Smilodon React] ${message}`);
299
+ }
300
+ },
301
+ []
302
+ );
303
+ const syncHostBusy = useCallback((element) => {
304
+ const { consumerAriaBusy: currentConsumerBusy, loading: externalBusy } = ariaBusyInputsRef.current;
305
+ if (acceptedDataLoadingConfigRef.current.protocol === "acknowledged" && !unusableRef.current) {
306
+ const composed = composeBusyToken(currentConsumerBusy, externalBusy);
307
+ if (composed === void 0) element.removeAttribute("aria-busy");
308
+ else element.setAttribute("aria-busy", String(composed));
309
+ return;
310
+ }
311
+ if (currentConsumerBusy === void 0) element.removeAttribute("aria-busy");
312
+ else element.setAttribute("aria-busy", String(currentConsumerBusy));
313
+ }, []);
314
+ const applyInstanceConfig = useCallback(
315
+ (element, config) => {
316
+ if (!(element == null ? void 0 : element.updateConfig) || unusableRef.current) return;
317
+ if (configUpdateInProgressRef.current) {
318
+ queuedConfigUpdatesRef.current.push({ ...config });
319
+ return;
320
+ }
321
+ configUpdateInProgressRef.current = true;
322
+ const queue = [{ ...config }];
323
+ let processed = 0;
324
+ try {
325
+ while (queue.length > 0 && !unusableRef.current) {
326
+ if (processed++ >= 100) {
327
+ queuedConfigUpdatesRef.current = [];
328
+ markUnusable(
329
+ "Too many reentrant configuration updates were queued; the wrapper has been stopped to avoid an unbounded update loop."
330
+ );
331
+ break;
332
+ }
333
+ const currentConfig = queue.shift();
334
+ const module = coreModuleRef.current;
335
+ let validation;
336
+ if (currentConfig.dataLoading !== void 0) {
337
+ const candidate = isPlainRecord(currentConfig.dataLoading) ? { ...acceptedDataLoadingConfigRef.current, ...currentConfig.dataLoading } : currentConfig.dataLoading;
338
+ if (!module) {
339
+ markUnusable(
340
+ "The core validator is unavailable; acknowledged configuration cannot be reconciled safely."
341
+ );
342
+ break;
343
+ }
344
+ try {
345
+ validation = module.validateDataLoadingConfig(candidate);
346
+ } catch (error2) {
347
+ markUnusable(
348
+ `Core data-loading preflight failed unexpectedly: ${String(error2)}`
349
+ );
350
+ break;
351
+ }
352
+ }
353
+ const update = { sawConfigError: false };
354
+ configUpdateStackRef.current.push(update);
355
+ let threw = false;
356
+ try {
357
+ element.updateConfig(currentConfig);
358
+ } catch (error2) {
359
+ threw = true;
360
+ markUnusable(
361
+ `Core configuration acceptance is uncertain after updateConfig() threw: ${String(error2)}`
362
+ );
363
+ } finally {
364
+ configUpdateStackRef.current.pop();
177
365
  }
178
- setIsElementReady(true);
179
- });
366
+ if ((validation == null ? void 0 : validation.valid) && !threw) {
367
+ if (update.sawConfigError) {
368
+ markUnusable(
369
+ "Core reported a data-loading configuration error for a preflight-valid candidate; the accepted protocol is uncertain."
370
+ );
371
+ break;
372
+ }
373
+ acceptedDataLoadingConfigRef.current = { ...validation.value };
374
+ setAcceptedProtocol(validation.value.protocol);
375
+ syncHostBusy(element);
376
+ }
377
+ if (queuedConfigUpdatesRef.current.length > 0) {
378
+ queue.push(...queuedConfigUpdatesRef.current);
379
+ queuedConfigUpdatesRef.current = [];
380
+ }
381
+ }
382
+ } finally {
383
+ configUpdateInProgressRef.current = false;
180
384
  }
385
+ },
386
+ [markUnusable, syncHostBusy]
387
+ );
388
+ const requiresAcknowledgedQualification = acceptedProtocol === "acknowledged" || ((_b = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _b.protocol) === "acknowledged";
389
+ const qualifyAcknowledgedElement = useCallback(() => {
390
+ const element = elementRef.current;
391
+ const module = coreModuleRef.current;
392
+ if (!element || !module || typeof customElements === "undefined") return false;
393
+ const registered = customElements.get("enhanced-select");
394
+ const observed = registered == null ? void 0 : registered.observedAttributes;
395
+ const compatible = registered === module.EnhancedSelect && element.constructor === module.EnhancedSelect && Array.isArray(observed) && observed.includes("aria-busy") && typeof element.updateConfig === "function" && typeof element.startDataLoading === "function" && typeof element.getDataLoadingState === "function" && typeof element.completeDataRequest === "function";
396
+ if (!compatible) {
397
+ markUnusable(
398
+ "Acknowledged data loading requires the exact peer-resolved EnhancedSelect constructor with the Phase 2F facade and aria-busy observation support."
399
+ );
400
+ return false;
181
401
  }
402
+ setIsAcknowledgedQualified(true);
403
+ return true;
404
+ }, [markUnusable]);
405
+ const subscribeToCoreEvents = useCallback((element) => {
406
+ const currentSubscription = activeListenersRef.current;
407
+ if ((currentSubscription == null ? void 0 : currentSubscription.element) === element) return currentSubscription.dispose;
408
+ currentSubscription == null ? void 0 : currentSubscription.dispose();
409
+ const handleSelect = (event) => {
410
+ var _a2, _b2;
411
+ const detail = event.detail;
412
+ (_b2 = (_a2 = callbackStateRef.current).onSelect) == null ? void 0 : _b2.call(_a2, detail.item, detail.index);
413
+ };
414
+ const handleChange = (event) => {
415
+ var _a2;
416
+ const { selectedItems, selectedValues } = event.detail;
417
+ const callbacks = callbackStateRef.current;
418
+ const values = selectedValues;
419
+ const nextSingleValue = values.length > 0 ? values[0] : "";
420
+ if (initializingRef.current) return;
421
+ if (!callbacks.isControlled) {
422
+ setInternalValue(callbacks.multiple ? values : nextSingleValue);
423
+ }
424
+ (_a2 = callbacks.onChange) == null ? void 0 : _a2.call(
425
+ callbacks,
426
+ callbacks.multiple ? values : nextSingleValue,
427
+ selectedItems
428
+ );
429
+ };
430
+ const handleOpen = () => {
431
+ var _a2, _b2;
432
+ return (_b2 = (_a2 = callbackStateRef.current).onOpen) == null ? void 0 : _b2.call(_a2);
433
+ };
434
+ const handleClose = () => {
435
+ var _a2, _b2;
436
+ return (_b2 = (_a2 = callbackStateRef.current).onClose) == null ? void 0 : _b2.call(_a2);
437
+ };
438
+ const handleSearch = (event) => {
439
+ const { query, results, count, request } = event.detail;
440
+ const callback = callbackStateRef.current.onSearch;
441
+ if (!callback) return;
442
+ if (request) callback(query, results, count, request);
443
+ else callback(query, results, count);
444
+ };
445
+ const handleLoadMore = (event) => {
446
+ const { page, request } = event.detail;
447
+ const callback = callbackStateRef.current.onLoadMore;
448
+ if (!callback) return;
449
+ if (request) callback(page, request);
450
+ else callback(page);
451
+ };
452
+ const handleClear = (event) => {
453
+ var _a2, _b2;
454
+ const { clearedSelection, clearedSearch } = event.detail;
455
+ (_b2 = (_a2 = callbackStateRef.current).onClear) == null ? void 0 : _b2.call(_a2, { clearedSelection, clearedSearch });
456
+ };
457
+ const handleDiagnostic = (event) => {
458
+ var _a2, _b2;
459
+ (_b2 = (_a2 = callbackStateRef.current).onDiagnostic) == null ? void 0 : _b2.call(
460
+ _a2,
461
+ event.detail
462
+ );
463
+ };
464
+ const handleDataRequestCancel = (event) => {
465
+ var _a2, _b2;
466
+ (_b2 = (_a2 = callbackStateRef.current).onDataRequestCancel) == null ? void 0 : _b2.call(
467
+ _a2,
468
+ event.detail
469
+ );
470
+ };
471
+ const handleDataProjection = (event) => {
472
+ var _a2, _b2;
473
+ (_b2 = (_a2 = callbackStateRef.current).onDataProjection) == null ? void 0 : _b2.call(
474
+ _a2,
475
+ event.detail
476
+ );
477
+ };
478
+ const handleDataLoadingConfigError = (event) => {
479
+ var _a2, _b2;
480
+ const activeUpdate = configUpdateStackRef.current[configUpdateStackRef.current.length - 1];
481
+ if (activeUpdate) activeUpdate.sawConfigError = true;
482
+ (_b2 = (_a2 = callbackStateRef.current).onDataLoadingConfigError) == null ? void 0 : _b2.call(
483
+ _a2,
484
+ event.detail
485
+ );
486
+ };
487
+ const listeners = [
488
+ ["select", handleSelect],
489
+ ["change", handleChange],
490
+ ["open", handleOpen],
491
+ ["close", handleClose],
492
+ ["search", handleSearch],
493
+ ["loadMore", handleLoadMore],
494
+ ["clear", handleClear],
495
+ ["diagnostic", handleDiagnostic],
496
+ ["dataRequestCancel", handleDataRequestCancel],
497
+ ["dataProjection", handleDataProjection],
498
+ ["dataLoadingConfigError", handleDataLoadingConfigError]
499
+ ];
500
+ for (const [name, listener] of listeners) {
501
+ element.addEventListener(name, listener);
502
+ }
503
+ const dispose = () => {
504
+ var _a2;
505
+ if (((_a2 = activeListenersRef.current) == null ? void 0 : _a2.dispose) === dispose) {
506
+ activeListenersRef.current = null;
507
+ }
508
+ for (const [name, listener] of listeners) {
509
+ element.removeEventListener(name, listener);
510
+ }
511
+ };
512
+ activeListenersRef.current = { element, dispose };
513
+ return dispose;
182
514
  }, []);
183
515
  useEffect(() => {
184
- var _a;
185
516
  const element = elementRef.current;
186
517
  if (!element || !isElementReady) return;
518
+ setElementPropertyIfChanged(
519
+ element,
520
+ "optionRenderer",
521
+ resolvedOptionRenderer
522
+ );
523
+ }, [isElementReady, resolvedOptionRenderer, setElementPropertyIfChanged]);
524
+ useEffect(() => {
525
+ const element = elementRef.current;
526
+ if (!element || !isElementReady) return;
527
+ setElementPropertyIfChanged(
528
+ element,
529
+ "groupHeaderRenderer",
530
+ resolvedGroupHeaderRenderer
531
+ );
532
+ }, [
533
+ isElementReady,
534
+ resolvedGroupHeaderRenderer,
535
+ setElementPropertyIfChanged
536
+ ]);
537
+ useEffect(() => {
538
+ const element = elementRef.current;
539
+ if (!element || !isElementReady) return;
540
+ if (!areClassMapsEqual(element.classMap, classMap)) {
541
+ element.classMap = classMap;
542
+ }
543
+ }, [isElementReady, classMap, areClassMapsEqual]);
544
+ useEffect(() => {
545
+ if (typeof window === "undefined" || typeof customElements === "undefined") return;
546
+ let active = true;
547
+ const existingConstructor = customElements.get("enhanced-select");
548
+ if (existingConstructor) setIsElementReady(true);
549
+ import("@smilodon/core").then((module) => {
550
+ if (!active) return;
551
+ coreModuleRef.current = module;
552
+ setIsCoreModuleLoaded(true);
553
+ const currentConstructor = customElements.get("enhanced-select");
554
+ if (!currentConstructor) {
555
+ customElements.define("enhanced-select", module.EnhancedSelect);
556
+ }
557
+ setIsElementReady(true);
558
+ }).catch((error2) => {
559
+ if (!active) return;
560
+ if (requiresAcknowledgedQualification) {
561
+ markUnusable(`Unable to load the compatible core runtime: ${String(error2)}`);
562
+ } else if (!customElements.get("enhanced-select")) {
563
+ console.error(`[Smilodon React] Unable to register enhanced-select: ${String(error2)}`);
564
+ }
565
+ });
566
+ return () => {
567
+ active = false;
568
+ };
569
+ }, [markUnusable]);
570
+ useEffect(() => {
571
+ if (!isElementReady || !isCoreModuleLoaded || !requiresAcknowledgedQualification || isUnusable) return;
572
+ qualifyAcknowledgedElement();
573
+ }, [
574
+ isElementReady,
575
+ isCoreModuleLoaded,
576
+ requiresAcknowledgedQualification,
577
+ isUnusable,
578
+ qualifyAcknowledgedElement
579
+ ]);
580
+ useEffect(() => {
581
+ const element = elementRef.current;
582
+ if (!(acknowledgedRequested || dataLoadingConfigured) || !isElementUsable || !element) return;
583
+ return subscribeToCoreEvents(element);
584
+ }, [
585
+ acknowledgedRequested,
586
+ dataLoadingConfigured,
587
+ isElementUsable,
588
+ subscribeToCoreEvents
589
+ ]);
590
+ useEffect(() => {
591
+ return () => {
592
+ var _a2;
593
+ return (_a2 = activeListenersRef.current) == null ? void 0 : _a2.dispose();
594
+ };
595
+ }, []);
596
+ useEffect(() => {
597
+ var _a2;
598
+ const element = elementRef.current;
599
+ if (!element || !isElementUsable) return;
187
600
  if (!element.setItems) {
188
601
  console.warn("Enhanced select element not fully initialized yet");
189
602
  return;
@@ -193,8 +606,6 @@ const Select = forwardRef((props, ref) => {
193
606
  } else if (items.length > 0) {
194
607
  element.setItems(items);
195
608
  }
196
- element.optionRenderer = resolvedOptionRenderer;
197
- element.groupHeaderRenderer = resolvedGroupHeaderRenderer;
198
609
  const config = {
199
610
  searchable,
200
611
  placeholder,
@@ -245,18 +656,13 @@ const Select = forwardRef((props, ref) => {
245
656
  autoMitigateRuntimeModeSwitch
246
657
  }
247
658
  };
248
- element.updateConfig(config);
659
+ applyInstanceConfig(element, config);
249
660
  if (coreConfig) {
250
- element.updateConfig(coreConfig);
251
- }
252
- if (classMap) {
253
- element.classMap = classMap;
254
- } else {
255
- element.classMap = void 0;
661
+ applyInstanceConfig(element, coreConfig);
256
662
  }
257
663
  if (!isControlled && !hasAppliedInitialValueRef.current && internalValue !== void 0) {
258
664
  const values = Array.isArray(internalValue) ? internalValue : [internalValue];
259
- const currentValues = ((_a = element.getSelectedValues) == null ? void 0 : _a.call(element)) || [];
665
+ const currentValues = ((_a2 = element.getSelectedValues) == null ? void 0 : _a2.call(element)) || [];
260
666
  if (!areValuesEqual(values, currentValues)) {
261
667
  element.setSelectedValues(values);
262
668
  }
@@ -270,10 +676,55 @@ const Select = forwardRef((props, ref) => {
270
676
  if (required) {
271
677
  element.setRequired(true);
272
678
  }
273
- }, [isElementReady, items, groupedItems, searchable, placeholder, virtualized, estimatedItemHeight, disabled, multiple, maxSelections, selectionConfig, multiSelectDisplay, scrollToSelected, stylesConfig, coreConfig, removeButtonIcon, disabledOptionBehavior, showSelectedIndicator, toggleOnTriggerClick, infiniteScroll, pageSize, creatable, clearable, clearSelectionOnClear, clearSearchOnClear, clearAriaLabel, clearIcon, trackingEnabled, trackEvents, trackStyling, trackLimitations, emitDiagnostics, trackingMaxEntries, limitationPolicies, autoMitigateRuntimeModeSwitch, direction, resolvedDropdownPlacement, error, errorMessage, required, internalValue, isControlled, resolvedOptionRenderer, resolvedGroupHeaderRenderer, areValuesEqual]);
679
+ }, [
680
+ isElementUsable,
681
+ items,
682
+ groupedItems,
683
+ searchable,
684
+ placeholder,
685
+ virtualized,
686
+ estimatedItemHeight,
687
+ disabled,
688
+ multiple,
689
+ maxSelections,
690
+ selectionConfig,
691
+ multiSelectDisplay,
692
+ scrollToSelected,
693
+ stylesConfig,
694
+ coreConfig,
695
+ removeButtonIcon,
696
+ disabledOptionBehavior,
697
+ showSelectedIndicator,
698
+ toggleOnTriggerClick,
699
+ infiniteScroll,
700
+ pageSize,
701
+ creatable,
702
+ clearable,
703
+ clearSelectionOnClear,
704
+ clearSearchOnClear,
705
+ clearAriaLabel,
706
+ clearIcon,
707
+ trackingEnabled,
708
+ trackEvents,
709
+ trackStyling,
710
+ trackLimitations,
711
+ emitDiagnostics,
712
+ trackingMaxEntries,
713
+ limitationPolicies,
714
+ autoMitigateRuntimeModeSwitch,
715
+ direction,
716
+ resolvedDropdownPlacement,
717
+ error,
718
+ errorMessage,
719
+ required,
720
+ internalValue,
721
+ isControlled,
722
+ areValuesEqual,
723
+ applyInstanceConfig
724
+ ]);
274
725
  useEffect(() => {
275
726
  const element = elementRef.current;
276
- if (!element || !isElementReady || !element.setItems) return;
727
+ if (!element || !isElementUsable || !element.setItems) return;
277
728
  if (groupedItems) {
278
729
  element.setGroupedItems(groupedItems);
279
730
  } else if (items && items.length && items[0] && items[0].group !== void 0) {
@@ -283,27 +734,31 @@ const Select = forwardRef((props, ref) => {
283
734
  if (!map.has(g)) map.set(g, []);
284
735
  map.get(g).push(it);
285
736
  });
286
- const groups = Array.from(map.entries()).map(([label, options]) => ({ label, options }));
737
+ const groups = Array.from(map.entries()).map(([label, options]) => ({
738
+ label,
739
+ options
740
+ }));
287
741
  element.setGroupedItems(groups);
288
742
  } else {
289
743
  element.setItems(items);
290
744
  }
291
- }, [items, groupedItems, groupHeaderRenderer, isElementReady]);
745
+ }, [items, groupedItems, isElementUsable]);
292
746
  useEffect(() => {
293
- var _a;
747
+ var _a2;
294
748
  const element = elementRef.current;
295
- if (!element || !isControlled || !isElementReady || !element.setSelectedValues) return;
749
+ if (!element || !isControlled || !isElementUsable || !element.setSelectedValues)
750
+ return;
296
751
  if (value !== void 0) {
297
752
  const values = Array.isArray(value) ? value : [value];
298
- const currentValues = ((_a = element.getSelectedValues) == null ? void 0 : _a.call(element)) || [];
753
+ const currentValues = ((_a2 = element.getSelectedValues) == null ? void 0 : _a2.call(element)) || [];
299
754
  if (!areValuesEqual(values, currentValues)) {
300
755
  element.setSelectedValues(values);
301
756
  }
302
757
  }
303
- }, [value, isControlled, isElementReady, areValuesEqual]);
758
+ }, [value, isControlled, isElementUsable, areValuesEqual]);
304
759
  useEffect(() => {
305
760
  const element = elementRef.current;
306
- if (!element || !isElementReady || !element.updateConfig) return;
761
+ if (!element || !isElementUsable || !element.updateConfig) return;
307
762
  const config = {
308
763
  searchable,
309
764
  placeholder,
@@ -353,172 +808,248 @@ const Select = forwardRef((props, ref) => {
353
808
  autoMitigateRuntimeModeSwitch
354
809
  }
355
810
  };
356
- element.updateConfig(config);
811
+ applyInstanceConfig(element, config);
357
812
  if (coreConfig) {
358
- element.updateConfig(coreConfig);
813
+ applyInstanceConfig(element, coreConfig);
359
814
  }
360
- }, [searchable, placeholder, virtualized, estimatedItemHeight, disabled, multiple, maxSelections, selectionConfig, multiSelectDisplay, scrollToSelected, stylesConfig, coreConfig, removeButtonIcon, disabledOptionBehavior, showSelectedIndicator, toggleOnTriggerClick, infiniteScroll, pageSize, clearable, clearSelectionOnClear, clearSearchOnClear, clearAriaLabel, clearIcon, trackingEnabled, trackEvents, trackStyling, trackLimitations, emitDiagnostics, trackingMaxEntries, limitationPolicies, autoMitigateRuntimeModeSwitch, direction, resolvedDropdownPlacement, isElementReady]);
815
+ }, [
816
+ searchable,
817
+ placeholder,
818
+ virtualized,
819
+ estimatedItemHeight,
820
+ disabled,
821
+ multiple,
822
+ maxSelections,
823
+ selectionConfig,
824
+ multiSelectDisplay,
825
+ scrollToSelected,
826
+ stylesConfig,
827
+ coreConfig,
828
+ removeButtonIcon,
829
+ disabledOptionBehavior,
830
+ showSelectedIndicator,
831
+ toggleOnTriggerClick,
832
+ infiniteScroll,
833
+ pageSize,
834
+ clearable,
835
+ clearSelectionOnClear,
836
+ clearSearchOnClear,
837
+ clearAriaLabel,
838
+ clearIcon,
839
+ trackingEnabled,
840
+ trackEvents,
841
+ trackStyling,
842
+ trackLimitations,
843
+ emitDiagnostics,
844
+ trackingMaxEntries,
845
+ limitationPolicies,
846
+ autoMitigateRuntimeModeSwitch,
847
+ direction,
848
+ resolvedDropdownPlacement,
849
+ isElementUsable,
850
+ applyInstanceConfig
851
+ ]);
361
852
  useEffect(() => {
362
853
  const element = elementRef.current;
363
- if (!element || !isElementReady) return;
854
+ if (!element || !isElementUsable) return;
364
855
  if (error) {
365
- if (element.setError) element.setError(errorMessage || "Invalid selection");
856
+ if (element.setError)
857
+ element.setError(errorMessage || "Invalid selection");
366
858
  } else {
367
859
  if (element.clearError) element.clearError();
368
860
  }
369
- }, [error, errorMessage, isElementReady]);
861
+ }, [error, errorMessage, isElementUsable]);
370
862
  useEffect(() => {
371
863
  const element = elementRef.current;
372
- if (!element || !isElementReady || !element.setRequired) return;
864
+ if (!element || !isElementUsable || !element.setRequired) return;
373
865
  element.setRequired(required);
374
- }, [required, isElementReady]);
866
+ }, [required, isElementUsable]);
375
867
  useEffect(() => {
868
+ var _a2, _b2;
376
869
  const element = elementRef.current;
377
- if (!element) return;
378
- const handleSelect = (e) => {
379
- const { item, index } = e.detail;
380
- onSelect == null ? void 0 : onSelect(item, index);
381
- };
382
- const handleChange = (e) => {
383
- const { selectedItems, selectedValues } = e.detail;
384
- const values = selectedValues;
385
- const nextSingleValue = values.length > 0 ? values[0] : "";
386
- if (!isControlled) {
387
- setInternalValue(multiple ? values : nextSingleValue);
388
- }
389
- if (onChange) {
390
- const value2 = multiple ? values : nextSingleValue;
391
- onChange(value2, selectedItems);
870
+ if (!element || !isElementUsable) return;
871
+ if (isCoreModuleLoaded && queuedImperativeConfigsRef.current.length > 0) {
872
+ const queued = queuedImperativeConfigsRef.current;
873
+ queuedImperativeConfigsRef.current = [];
874
+ for (const config of queued) {
875
+ const requestsAcknowledged = acceptedDataLoadingConfigRef.current.protocol === "acknowledged" || ((_a2 = config.dataLoading) == null ? void 0 : _a2.protocol) === "acknowledged";
876
+ if (requestsAcknowledged && !qualifyAcknowledgedElement()) break;
877
+ if (config.dataLoading !== void 0 || requestsAcknowledged) {
878
+ subscribeToCoreEvents(element);
879
+ }
880
+ applyInstanceConfig(element, config);
881
+ if (unusableRef.current) break;
392
882
  }
393
- };
394
- const handleOpen = (e) => {
395
- onOpen == null ? void 0 : onOpen();
396
- };
397
- const handleClose = (e) => {
398
- onClose == null ? void 0 : onClose();
399
- };
400
- const handleSearch = (e) => {
401
- const { query, results, count } = e.detail;
402
- onSearch == null ? void 0 : onSearch(query, results, count);
403
- };
404
- const handleLoadMore = (e) => {
405
- const { page } = e.detail;
406
- onLoadMore == null ? void 0 : onLoadMore(page);
407
- };
408
- const handleClear = (e) => {
409
- onClear == null ? void 0 : onClear({
410
- clearedSelection: e.detail.clearedSelection,
411
- clearedSearch: e.detail.clearedSearch
412
- });
413
- };
414
- const handleDiagnostic = (e) => {
415
- onDiagnostic == null ? void 0 : onDiagnostic(e.detail);
416
- };
417
- element.addEventListener("select", handleSelect);
418
- element.addEventListener("change", handleChange);
419
- element.addEventListener("open", handleOpen);
420
- element.addEventListener("close", handleClose);
421
- element.addEventListener("search", handleSearch);
422
- element.addEventListener("loadMore", handleLoadMore);
423
- element.addEventListener("clear", handleClear);
424
- element.addEventListener("diagnostic", handleDiagnostic);
425
- return () => {
426
- element.removeEventListener("select", handleSelect);
427
- element.removeEventListener("change", handleChange);
428
- element.removeEventListener("open", handleOpen);
429
- element.removeEventListener("close", handleClose);
430
- element.removeEventListener("search", handleSearch);
431
- element.removeEventListener("loadMore", handleLoadMore);
432
- element.removeEventListener("clear", handleClear);
433
- element.removeEventListener("diagnostic", handleDiagnostic);
434
- };
435
- }, [onSelect, onChange, onOpen, onClose, onSearch, onLoadMore, onClear, onDiagnostic, isControlled, multiple]);
883
+ }
884
+ initializingRef.current = false;
885
+ syncHostBusy(element);
886
+ if (acceptedDataLoadingConfigRef.current.protocol === "acknowledged") {
887
+ (_b2 = element.startDataLoading) == null ? void 0 : _b2.call(element);
888
+ }
889
+ }, [
890
+ isElementUsable,
891
+ acceptedProtocol,
892
+ acknowledgedRequested,
893
+ coreConfig,
894
+ items,
895
+ groupedItems,
896
+ value,
897
+ defaultValue,
898
+ loading,
899
+ consumerAriaBusy,
900
+ syncHostBusy,
901
+ isCoreModuleLoaded,
902
+ qualifyAcknowledgedElement,
903
+ subscribeToCoreEvents,
904
+ applyInstanceConfig
905
+ ]);
906
+ useEffect(() => {
907
+ const element = elementRef.current;
908
+ if (!element || acknowledgedRequested || dataLoadingConfigured || !isElementReady) return;
909
+ return subscribeToCoreEvents(element);
910
+ }, [
911
+ acknowledgedRequested,
912
+ dataLoadingConfigured,
913
+ isElementReady,
914
+ subscribeToCoreEvents
915
+ ]);
436
916
  useImperativeHandle(ref, () => ({
437
917
  focus: () => {
438
- var _a;
439
- (_a = elementRef.current) == null ? void 0 : _a.focus();
918
+ var _a2;
919
+ (_a2 = elementRef.current) == null ? void 0 : _a2.focus();
440
920
  },
441
921
  open: () => {
442
- var _a;
443
- (_a = elementRef.current) == null ? void 0 : _a.open();
922
+ var _a2;
923
+ (_a2 = elementRef.current) == null ? void 0 : _a2.open();
444
924
  },
445
925
  close: () => {
446
- var _a;
447
- (_a = elementRef.current) == null ? void 0 : _a.close();
926
+ var _a2;
927
+ (_a2 = elementRef.current) == null ? void 0 : _a2.close();
448
928
  },
449
929
  getSelectedItems: () => {
450
- var _a;
451
- return ((_a = elementRef.current) == null ? void 0 : _a.getSelectedItems()) || [];
930
+ var _a2;
931
+ return ((_a2 = elementRef.current) == null ? void 0 : _a2.getSelectedItems()) || [];
452
932
  },
453
933
  getSelectedValues: () => {
454
- var _a;
455
- return ((_a = elementRef.current) == null ? void 0 : _a.getSelectedValues()) || [];
934
+ var _a2;
935
+ return ((_a2 = elementRef.current) == null ? void 0 : _a2.getSelectedValues()) || [];
456
936
  },
457
937
  setItems: (items2) => {
458
- var _a;
459
- (_a = elementRef.current) == null ? void 0 : _a.setItems(items2);
938
+ var _a2;
939
+ (_a2 = elementRef.current) == null ? void 0 : _a2.setItems(items2);
460
940
  },
461
941
  setGroupedItems: (groups) => {
462
- var _a;
463
- (_a = elementRef.current) == null ? void 0 : _a.setGroupedItems(groups);
942
+ var _a2;
943
+ (_a2 = elementRef.current) == null ? void 0 : _a2.setGroupedItems(groups);
464
944
  },
465
945
  clear: () => {
466
- var _a;
467
- (_a = elementRef.current) == null ? void 0 : _a.setSelectedValues([]);
946
+ var _a2;
947
+ (_a2 = elementRef.current) == null ? void 0 : _a2.setSelectedValues([]);
468
948
  if (!isControlled) {
469
949
  setInternalValue(multiple ? [] : void 0);
470
950
  }
471
951
  onChange == null ? void 0 : onChange(multiple ? [] : "", []);
472
952
  },
473
953
  clearSearch: () => {
474
- var _a, _b;
475
- (_b = (_a = elementRef.current) == null ? void 0 : _a.clearSearch) == null ? void 0 : _b.call(_a);
954
+ var _a2, _b2;
955
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.clearSearch) == null ? void 0 : _b2.call(_a2);
476
956
  },
477
957
  updateConfig: (config) => {
478
- var _a, _b;
479
- (_b = (_a = elementRef.current) == null ? void 0 : _a.updateConfig) == null ? void 0 : _b.call(_a, config);
958
+ var _a2;
959
+ const element = elementRef.current;
960
+ if (!element || unusableRef.current) return;
961
+ const requestsAcknowledged = acceptedDataLoadingConfigRef.current.protocol === "acknowledged" || ((_a2 = config.dataLoading) == null ? void 0 : _a2.protocol) === "acknowledged";
962
+ if (!isElementReady || !isCoreModuleLoaded) {
963
+ queuedImperativeConfigsRef.current.push({ ...config });
964
+ return;
965
+ }
966
+ if (requestsAcknowledged && !qualifyAcknowledgedElement()) return;
967
+ if (config.dataLoading !== void 0 || requestsAcknowledged) {
968
+ subscribeToCoreEvents(element);
969
+ }
970
+ applyInstanceConfig(element, config);
971
+ },
972
+ getDataLoadingState: () => {
973
+ var _a2, _b2, _c;
974
+ const snapshot = (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getDataLoadingState) == null ? void 0 : _b2.call(_a2);
975
+ if (snapshot) return snapshot;
976
+ return {
977
+ status: "idle",
978
+ connected: Boolean((_c = elementRef.current) == null ? void 0 : _c.isConnected),
979
+ busy: false,
980
+ projectionPending: false,
981
+ queryGeneration: 0,
982
+ activeRequest: null,
983
+ hasMore: false,
984
+ nextEligiblePosition: {
985
+ mode: "page",
986
+ page: DEFAULT_DATA_LOADING_CONFIG.firstPage
987
+ },
988
+ error: null,
989
+ resourceLimit: null
990
+ };
991
+ },
992
+ completeDataRequest: (completion) => {
993
+ var _a2, _b2;
994
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.completeDataRequest) == null ? void 0 : _b2.call(_a2, completion)) ?? "unknown-request";
480
995
  },
481
996
  setError: (message) => {
482
- var _a, _b;
483
- (_b = (_a = elementRef.current) == null ? void 0 : _a.setError) == null ? void 0 : _b.call(_a, message);
997
+ var _a2, _b2;
998
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.setError) == null ? void 0 : _b2.call(_a2, message);
484
999
  },
485
1000
  clearError: () => {
486
- var _a, _b;
487
- (_b = (_a = elementRef.current) == null ? void 0 : _a.clearError) == null ? void 0 : _b.call(_a);
1001
+ var _a2, _b2;
1002
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.clearError) == null ? void 0 : _b2.call(_a2);
488
1003
  },
489
1004
  setRequired: (isRequired) => {
490
- var _a, _b;
491
- (_b = (_a = elementRef.current) == null ? void 0 : _a.setRequired) == null ? void 0 : _b.call(_a, isRequired);
1005
+ var _a2, _b2;
1006
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.setRequired) == null ? void 0 : _b2.call(_a2, isRequired);
492
1007
  },
493
1008
  validate: () => {
494
- var _a, _b;
495
- return ((_b = (_a = elementRef.current) == null ? void 0 : _a.validate) == null ? void 0 : _b.call(_a)) ?? true;
1009
+ var _a2, _b2;
1010
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.validate) == null ? void 0 : _b2.call(_a2)) ?? true;
496
1011
  },
497
1012
  getCapabilities: () => {
498
- var _a, _b;
499
- return (_b = (_a = elementRef.current) == null ? void 0 : _a.getCapabilities) == null ? void 0 : _b.call(_a);
1013
+ var _a2, _b2;
1014
+ return (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getCapabilities) == null ? void 0 : _b2.call(_a2);
500
1015
  },
501
1016
  getKnownLimitations: () => {
502
- var _a, _b;
503
- return ((_b = (_a = elementRef.current) == null ? void 0 : _a.getKnownLimitations) == null ? void 0 : _b.call(_a)) || [];
1017
+ var _a2, _b2;
1018
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getKnownLimitations) == null ? void 0 : _b2.call(_a2)) || [];
504
1019
  },
505
1020
  getTrackingSnapshot: () => {
506
- var _a, _b;
507
- return ((_b = (_a = elementRef.current) == null ? void 0 : _a.getTrackingSnapshot) == null ? void 0 : _b.call(_a)) || { events: [], styles: [], limitations: [] };
1021
+ var _a2, _b2;
1022
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getTrackingSnapshot) == null ? void 0 : _b2.call(_a2)) || {
1023
+ events: [],
1024
+ styles: [],
1025
+ limitations: []
1026
+ };
508
1027
  },
509
1028
  clearTracking: (source) => {
510
- var _a, _b;
511
- (_b = (_a = elementRef.current) == null ? void 0 : _a.clearTracking) == null ? void 0 : _b.call(_a, source);
1029
+ var _a2, _b2;
1030
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.clearTracking) == null ? void 0 : _b2.call(_a2, source);
512
1031
  },
513
1032
  setLimitationPolicies: (policies) => {
514
- var _a, _b;
515
- (_b = (_a = elementRef.current) == null ? void 0 : _a.setLimitationPolicies) == null ? void 0 : _b.call(_a, policies);
1033
+ var _a2, _b2;
1034
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.setLimitationPolicies) == null ? void 0 : _b2.call(_a2, policies);
516
1035
  }
517
- }));
1036
+ }), [
1037
+ applyInstanceConfig,
1038
+ isElementReady,
1039
+ isCoreModuleLoaded,
1040
+ qualifyAcknowledgedElement,
1041
+ subscribeToCoreEvents,
1042
+ isElementUsable,
1043
+ isControlled,
1044
+ multiple,
1045
+ onChange
1046
+ ]);
1047
+ const renderedAriaBusy = acceptedProtocol === "acknowledged" && !isUnusable ? composeBusyToken(consumerAriaBusy, loading) : consumerAriaBusy;
518
1048
  return React.createElement("enhanced-select", {
519
1049
  ref: elementRef,
520
1050
  ...hostProps,
521
- className,
1051
+ "aria-busy": renderedAriaBusy,
1052
+ class: className,
522
1053
  dir: direction,
523
1054
  style
524
1055
  });