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