@smilodon/react 1.10.0 → 1.11.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/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,
@@ -27,6 +49,7 @@ const Select = forwardRef((props, ref) => {
27
49
  styles: stylesConfig,
28
50
  config: coreConfig,
29
51
  removeButtonIcon,
52
+ arrowIcon,
30
53
  disabledOptionBehavior,
31
54
  showSelectedIndicator = true,
32
55
  toggleOnTriggerClick = true,
@@ -51,6 +74,9 @@ const Select = forwardRef((props, ref) => {
51
74
  onLoadMore,
52
75
  onClear,
53
76
  onDiagnostic,
77
+ onDataRequestCancel,
78
+ onDataProjection,
79
+ onDataLoadingConfigError,
54
80
  trackingEnabled = false,
55
81
  trackEvents = true,
56
82
  trackStyling = true,
@@ -64,16 +90,65 @@ const Select = forwardRef((props, ref) => {
64
90
  onCreate,
65
91
  classMap,
66
92
  groupHeaderRenderer,
93
+ "aria-busy": consumerAriaBusy,
67
94
  ...hostProps
68
95
  } = props;
96
+ const [isControlled] = useState(value !== void 0);
97
+ const [internalValue, setInternalValue] = useState(defaultValue);
98
+ const hasAppliedInitialValueRef = useRef(false);
69
99
  const elementRef = useRef(null);
100
+ const coreModuleRef = useRef(null);
101
+ const acceptedDataLoadingConfigRef = useRef({
102
+ ...DEFAULT_DATA_LOADING_CONFIG
103
+ });
104
+ const configUpdateStackRef = useRef([]);
105
+ const configUpdateInProgressRef = useRef(false);
106
+ const queuedConfigUpdatesRef = useRef([]);
107
+ const queuedImperativeConfigsRef = useRef([]);
108
+ const activeListenersRef = useRef(null);
109
+ const callbackStateRef = useRef({
110
+ onSelect,
111
+ onChange,
112
+ onOpen,
113
+ onClose,
114
+ onSearch,
115
+ onLoadMore,
116
+ onClear,
117
+ onDiagnostic,
118
+ onDataRequestCancel,
119
+ onDataProjection,
120
+ onDataLoadingConfigError,
121
+ isControlled,
122
+ multiple
123
+ });
124
+ callbackStateRef.current = {
125
+ onSelect,
126
+ onChange,
127
+ onOpen,
128
+ onClose,
129
+ onSearch,
130
+ onLoadMore,
131
+ onClear,
132
+ onDiagnostic,
133
+ onDataRequestCancel,
134
+ onDataProjection,
135
+ onDataLoadingConfigError,
136
+ isControlled,
137
+ multiple
138
+ };
139
+ const ariaBusyInputsRef = useRef({ consumerAriaBusy, loading });
140
+ ariaBusyInputsRef.current = { consumerAriaBusy, loading };
141
+ const initializingRef = useRef(true);
142
+ const compatibilityFailureReportedRef = useRef(false);
143
+ const unusableRef = useRef(false);
144
+ const [acceptedProtocol, setAcceptedProtocol] = useState("legacy");
145
+ const [isCoreModuleLoaded, setIsCoreModuleLoaded] = useState(false);
146
+ const [isAcknowledgedQualified, setIsAcknowledgedQualified] = useState(false);
147
+ const [isUnusable, setIsUnusable] = useState(false);
70
148
  const resolvedDropdownPlacement = useMemo(() => {
71
149
  if (placement == null ? void 0 : placement.startsWith("top")) return "top";
72
150
  return "bottom";
73
151
  }, [placement]);
74
- const [isControlled] = useState(value !== void 0);
75
- const [internalValue, setInternalValue] = useState(defaultValue);
76
- const hasAppliedInitialValueRef = useRef(false);
77
152
  const reactRendererCache = useRef(
78
153
  /* @__PURE__ */ new Map()
79
154
  );
@@ -124,8 +199,8 @@ const Select = forwardRef((props, ref) => {
124
199
  const resolvedOptionRenderer = useMemo(() => {
125
200
  if (optionRenderer) {
126
201
  return (item, index, helpers) => {
127
- var _a;
128
- return ((_a = optionRendererRef.current) == null ? void 0 : _a.call(optionRendererRef, item, index, helpers)) || document.createElement("div");
202
+ var _a2;
203
+ return ((_a2 = optionRendererRef.current) == null ? void 0 : _a2.call(optionRendererRef, item, index, helpers)) || document.createElement("div");
129
204
  };
130
205
  }
131
206
  const hasReactRenderer = !!(customRenderer ?? renderItem);
@@ -204,6 +279,240 @@ const Select = forwardRef((props, ref) => {
204
279
  reactRendererCache.current.clear();
205
280
  }, [!!optionRenderer, !!(customRenderer ?? renderItem), scheduleRootUnmount]);
206
281
  const [isElementReady, setIsElementReady] = useState(false);
282
+ const acknowledgedRequested = acceptedProtocol === "acknowledged" || ((_a = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _a.protocol) === "acknowledged";
283
+ const dataLoadingConfigured = (coreConfig == null ? void 0 : coreConfig.dataLoading) !== void 0;
284
+ const coreFacadeRequired = acknowledgedRequested || dataLoadingConfigured;
285
+ const isElementUsable = isElementReady && (!acknowledgedRequested || isAcknowledgedQualified) && (!coreFacadeRequired || isCoreModuleLoaded) && !isUnusable;
286
+ const markUnusable = useCallback(
287
+ (message) => {
288
+ if (unusableRef.current) return;
289
+ unusableRef.current = true;
290
+ setIsUnusable(true);
291
+ const element = elementRef.current;
292
+ const { consumerAriaBusy: currentConsumerBusy } = ariaBusyInputsRef.current;
293
+ if (element) {
294
+ if (currentConsumerBusy === void 0) element.removeAttribute("aria-busy");
295
+ else element.setAttribute("aria-busy", String(currentConsumerBusy));
296
+ }
297
+ if (!compatibilityFailureReportedRef.current) {
298
+ compatibilityFailureReportedRef.current = true;
299
+ console.error(`[Smilodon React] ${message}`);
300
+ }
301
+ },
302
+ []
303
+ );
304
+ const syncHostBusy = useCallback((element) => {
305
+ const { consumerAriaBusy: currentConsumerBusy, loading: externalBusy } = ariaBusyInputsRef.current;
306
+ if (acceptedDataLoadingConfigRef.current.protocol === "acknowledged" && !unusableRef.current) {
307
+ const composed = composeBusyToken(currentConsumerBusy, externalBusy);
308
+ if (composed === void 0) element.removeAttribute("aria-busy");
309
+ else element.setAttribute("aria-busy", String(composed));
310
+ return;
311
+ }
312
+ if (currentConsumerBusy === void 0) element.removeAttribute("aria-busy");
313
+ else element.setAttribute("aria-busy", String(currentConsumerBusy));
314
+ }, []);
315
+ const applyInstanceConfig = useCallback(
316
+ (element, config) => {
317
+ if (!(element == null ? void 0 : element.updateConfig) || unusableRef.current) return;
318
+ if (configUpdateInProgressRef.current) {
319
+ queuedConfigUpdatesRef.current.push({ ...config });
320
+ return;
321
+ }
322
+ configUpdateInProgressRef.current = true;
323
+ const queue = [{ ...config }];
324
+ let processed = 0;
325
+ try {
326
+ while (queue.length > 0 && !unusableRef.current) {
327
+ if (processed++ >= 100) {
328
+ queuedConfigUpdatesRef.current = [];
329
+ markUnusable(
330
+ "Too many reentrant configuration updates were queued; the wrapper has been stopped to avoid an unbounded update loop."
331
+ );
332
+ break;
333
+ }
334
+ const currentConfig = queue.shift();
335
+ const module = coreModuleRef.current;
336
+ let validation;
337
+ if (currentConfig.dataLoading !== void 0) {
338
+ const candidate = isPlainRecord(currentConfig.dataLoading) ? { ...acceptedDataLoadingConfigRef.current, ...currentConfig.dataLoading } : currentConfig.dataLoading;
339
+ if (!module) {
340
+ markUnusable(
341
+ "The core validator is unavailable; acknowledged configuration cannot be reconciled safely."
342
+ );
343
+ break;
344
+ }
345
+ try {
346
+ validation = module.validateDataLoadingConfig(candidate);
347
+ } catch (error2) {
348
+ markUnusable(
349
+ `Core data-loading preflight failed unexpectedly: ${String(error2)}`
350
+ );
351
+ break;
352
+ }
353
+ }
354
+ const update = { sawConfigError: false };
355
+ configUpdateStackRef.current.push(update);
356
+ let threw = false;
357
+ try {
358
+ element.updateConfig(currentConfig);
359
+ } catch (error2) {
360
+ threw = true;
361
+ markUnusable(
362
+ `Core configuration acceptance is uncertain after updateConfig() threw: ${String(error2)}`
363
+ );
364
+ } finally {
365
+ configUpdateStackRef.current.pop();
366
+ }
367
+ if ((validation == null ? void 0 : validation.valid) && !threw) {
368
+ if (update.sawConfigError) {
369
+ markUnusable(
370
+ "Core reported a data-loading configuration error for a preflight-valid candidate; the accepted protocol is uncertain."
371
+ );
372
+ break;
373
+ }
374
+ acceptedDataLoadingConfigRef.current = { ...validation.value };
375
+ setAcceptedProtocol(validation.value.protocol);
376
+ syncHostBusy(element);
377
+ }
378
+ if (queuedConfigUpdatesRef.current.length > 0) {
379
+ queue.push(...queuedConfigUpdatesRef.current);
380
+ queuedConfigUpdatesRef.current = [];
381
+ }
382
+ }
383
+ } finally {
384
+ configUpdateInProgressRef.current = false;
385
+ }
386
+ },
387
+ [markUnusable, syncHostBusy]
388
+ );
389
+ const requiresAcknowledgedQualification = acceptedProtocol === "acknowledged" || ((_b = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _b.protocol) === "acknowledged";
390
+ const qualifyAcknowledgedElement = useCallback(() => {
391
+ const element = elementRef.current;
392
+ const module = coreModuleRef.current;
393
+ if (!element || !module || typeof customElements === "undefined") return false;
394
+ const registered = customElements.get("enhanced-select");
395
+ const observed = registered == null ? void 0 : registered.observedAttributes;
396
+ 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";
397
+ if (!compatible) {
398
+ markUnusable(
399
+ "Acknowledged data loading requires the exact peer-resolved EnhancedSelect constructor with the Phase 2F facade and aria-busy observation support."
400
+ );
401
+ return false;
402
+ }
403
+ setIsAcknowledgedQualified(true);
404
+ return true;
405
+ }, [markUnusable]);
406
+ const subscribeToCoreEvents = useCallback((element) => {
407
+ const currentSubscription = activeListenersRef.current;
408
+ if ((currentSubscription == null ? void 0 : currentSubscription.element) === element) return currentSubscription.dispose;
409
+ currentSubscription == null ? void 0 : currentSubscription.dispose();
410
+ const handleSelect = (event) => {
411
+ var _a2, _b2;
412
+ const detail = event.detail;
413
+ (_b2 = (_a2 = callbackStateRef.current).onSelect) == null ? void 0 : _b2.call(_a2, detail.item, detail.index);
414
+ };
415
+ const handleChange = (event) => {
416
+ var _a2;
417
+ const { selectedItems, selectedValues } = event.detail;
418
+ const callbacks = callbackStateRef.current;
419
+ const values = selectedValues;
420
+ const nextSingleValue = values.length > 0 ? values[0] : "";
421
+ if (initializingRef.current) return;
422
+ if (!callbacks.isControlled) {
423
+ setInternalValue(callbacks.multiple ? values : nextSingleValue);
424
+ }
425
+ (_a2 = callbacks.onChange) == null ? void 0 : _a2.call(
426
+ callbacks,
427
+ callbacks.multiple ? values : nextSingleValue,
428
+ selectedItems
429
+ );
430
+ };
431
+ const handleOpen = () => {
432
+ var _a2, _b2;
433
+ return (_b2 = (_a2 = callbackStateRef.current).onOpen) == null ? void 0 : _b2.call(_a2);
434
+ };
435
+ const handleClose = () => {
436
+ var _a2, _b2;
437
+ return (_b2 = (_a2 = callbackStateRef.current).onClose) == null ? void 0 : _b2.call(_a2);
438
+ };
439
+ const handleSearch = (event) => {
440
+ const { query, results, count, request } = event.detail;
441
+ const callback = callbackStateRef.current.onSearch;
442
+ if (!callback) return;
443
+ if (request) callback(query, results, count, request);
444
+ else callback(query, results, count);
445
+ };
446
+ const handleLoadMore = (event) => {
447
+ const { page, request } = event.detail;
448
+ const callback = callbackStateRef.current.onLoadMore;
449
+ if (!callback) return;
450
+ if (request) callback(page, request);
451
+ else callback(page);
452
+ };
453
+ const handleClear = (event) => {
454
+ var _a2, _b2;
455
+ const { clearedSelection, clearedSearch } = event.detail;
456
+ (_b2 = (_a2 = callbackStateRef.current).onClear) == null ? void 0 : _b2.call(_a2, { clearedSelection, clearedSearch });
457
+ };
458
+ const handleDiagnostic = (event) => {
459
+ var _a2, _b2;
460
+ (_b2 = (_a2 = callbackStateRef.current).onDiagnostic) == null ? void 0 : _b2.call(
461
+ _a2,
462
+ event.detail
463
+ );
464
+ };
465
+ const handleDataRequestCancel = (event) => {
466
+ var _a2, _b2;
467
+ (_b2 = (_a2 = callbackStateRef.current).onDataRequestCancel) == null ? void 0 : _b2.call(
468
+ _a2,
469
+ event.detail
470
+ );
471
+ };
472
+ const handleDataProjection = (event) => {
473
+ var _a2, _b2;
474
+ (_b2 = (_a2 = callbackStateRef.current).onDataProjection) == null ? void 0 : _b2.call(
475
+ _a2,
476
+ event.detail
477
+ );
478
+ };
479
+ const handleDataLoadingConfigError = (event) => {
480
+ var _a2, _b2;
481
+ const activeUpdate = configUpdateStackRef.current[configUpdateStackRef.current.length - 1];
482
+ if (activeUpdate) activeUpdate.sawConfigError = true;
483
+ (_b2 = (_a2 = callbackStateRef.current).onDataLoadingConfigError) == null ? void 0 : _b2.call(
484
+ _a2,
485
+ event.detail
486
+ );
487
+ };
488
+ const listeners = [
489
+ ["select", handleSelect],
490
+ ["change", handleChange],
491
+ ["open", handleOpen],
492
+ ["close", handleClose],
493
+ ["search", handleSearch],
494
+ ["loadMore", handleLoadMore],
495
+ ["clear", handleClear],
496
+ ["diagnostic", handleDiagnostic],
497
+ ["dataRequestCancel", handleDataRequestCancel],
498
+ ["dataProjection", handleDataProjection],
499
+ ["dataLoadingConfigError", handleDataLoadingConfigError]
500
+ ];
501
+ for (const [name, listener] of listeners) {
502
+ element.addEventListener(name, listener);
503
+ }
504
+ const dispose = () => {
505
+ var _a2;
506
+ if (((_a2 = activeListenersRef.current) == null ? void 0 : _a2.dispose) === dispose) {
507
+ activeListenersRef.current = null;
508
+ }
509
+ for (const [name, listener] of listeners) {
510
+ element.removeEventListener(name, listener);
511
+ }
512
+ };
513
+ activeListenersRef.current = { element, dispose };
514
+ return dispose;
515
+ }, []);
207
516
  useEffect(() => {
208
517
  const element = elementRef.current;
209
518
  if (!element || !isElementReady) return;
@@ -234,23 +543,61 @@ const Select = forwardRef((props, ref) => {
234
543
  }
235
544
  }, [isElementReady, classMap, areClassMapsEqual]);
236
545
  useEffect(() => {
237
- if (typeof window !== "undefined") {
238
- if (customElements.get("enhanced-select")) {
239
- setIsElementReady(true);
240
- } else {
241
- import("@smilodon/core").then((module) => {
242
- if (!customElements.get("enhanced-select")) {
243
- customElements.define("enhanced-select", module.EnhancedSelect);
244
- }
245
- setIsElementReady(true);
246
- });
546
+ if (typeof window === "undefined" || typeof customElements === "undefined") return;
547
+ let active = true;
548
+ const existingConstructor = customElements.get("enhanced-select");
549
+ if (existingConstructor) setIsElementReady(true);
550
+ import("@smilodon/core").then((module) => {
551
+ if (!active) return;
552
+ coreModuleRef.current = module;
553
+ setIsCoreModuleLoaded(true);
554
+ const currentConstructor = customElements.get("enhanced-select");
555
+ if (!currentConstructor) {
556
+ customElements.define("enhanced-select", module.EnhancedSelect);
247
557
  }
248
- }
558
+ setIsElementReady(true);
559
+ }).catch((error2) => {
560
+ if (!active) return;
561
+ if (requiresAcknowledgedQualification) {
562
+ markUnusable(`Unable to load the compatible core runtime: ${String(error2)}`);
563
+ } else if (!customElements.get("enhanced-select")) {
564
+ console.error(`[Smilodon React] Unable to register enhanced-select: ${String(error2)}`);
565
+ }
566
+ });
567
+ return () => {
568
+ active = false;
569
+ };
570
+ }, [markUnusable]);
571
+ useEffect(() => {
572
+ if (!isElementReady || !isCoreModuleLoaded || !requiresAcknowledgedQualification || isUnusable) return;
573
+ qualifyAcknowledgedElement();
574
+ }, [
575
+ isElementReady,
576
+ isCoreModuleLoaded,
577
+ requiresAcknowledgedQualification,
578
+ isUnusable,
579
+ qualifyAcknowledgedElement
580
+ ]);
581
+ useEffect(() => {
582
+ const element = elementRef.current;
583
+ if (!(acknowledgedRequested || dataLoadingConfigured) || !isElementUsable || !element) return;
584
+ return subscribeToCoreEvents(element);
585
+ }, [
586
+ acknowledgedRequested,
587
+ dataLoadingConfigured,
588
+ isElementUsable,
589
+ subscribeToCoreEvents
590
+ ]);
591
+ useEffect(() => {
592
+ return () => {
593
+ var _a2;
594
+ return (_a2 = activeListenersRef.current) == null ? void 0 : _a2.dispose();
595
+ };
249
596
  }, []);
250
597
  useEffect(() => {
251
- var _a;
598
+ var _a2;
252
599
  const element = elementRef.current;
253
- if (!element || !isElementReady) return;
600
+ if (!element || !isElementUsable) return;
254
601
  if (!element.setItems) {
255
602
  console.warn("Enhanced select element not fully initialized yet");
256
603
  return;
@@ -310,13 +657,13 @@ const Select = forwardRef((props, ref) => {
310
657
  autoMitigateRuntimeModeSwitch
311
658
  }
312
659
  };
313
- element.updateConfig(config);
660
+ applyInstanceConfig(element, config);
314
661
  if (coreConfig) {
315
- element.updateConfig(coreConfig);
662
+ applyInstanceConfig(element, coreConfig);
316
663
  }
317
664
  if (!isControlled && !hasAppliedInitialValueRef.current && internalValue !== void 0) {
318
665
  const values = Array.isArray(internalValue) ? internalValue : [internalValue];
319
- const currentValues = ((_a = element.getSelectedValues) == null ? void 0 : _a.call(element)) || [];
666
+ const currentValues = ((_a2 = element.getSelectedValues) == null ? void 0 : _a2.call(element)) || [];
320
667
  if (!areValuesEqual(values, currentValues)) {
321
668
  element.setSelectedValues(values);
322
669
  }
@@ -331,7 +678,7 @@ const Select = forwardRef((props, ref) => {
331
678
  element.setRequired(true);
332
679
  }
333
680
  }, [
334
- isElementReady,
681
+ isElementUsable,
335
682
  items,
336
683
  groupedItems,
337
684
  searchable,
@@ -373,11 +720,12 @@ const Select = forwardRef((props, ref) => {
373
720
  required,
374
721
  internalValue,
375
722
  isControlled,
376
- areValuesEqual
723
+ areValuesEqual,
724
+ applyInstanceConfig
377
725
  ]);
378
726
  useEffect(() => {
379
727
  const element = elementRef.current;
380
- if (!element || !isElementReady || !element.setItems) return;
728
+ if (!element || !isElementUsable || !element.setItems) return;
381
729
  if (groupedItems) {
382
730
  element.setGroupedItems(groupedItems);
383
731
  } else if (items && items.length && items[0] && items[0].group !== void 0) {
@@ -395,23 +743,23 @@ const Select = forwardRef((props, ref) => {
395
743
  } else {
396
744
  element.setItems(items);
397
745
  }
398
- }, [items, groupedItems, isElementReady]);
746
+ }, [items, groupedItems, isElementUsable]);
399
747
  useEffect(() => {
400
- var _a;
748
+ var _a2;
401
749
  const element = elementRef.current;
402
- if (!element || !isControlled || !isElementReady || !element.setSelectedValues)
750
+ if (!element || !isControlled || !isElementUsable || !element.setSelectedValues)
403
751
  return;
404
752
  if (value !== void 0) {
405
753
  const values = Array.isArray(value) ? value : [value];
406
- const currentValues = ((_a = element.getSelectedValues) == null ? void 0 : _a.call(element)) || [];
754
+ const currentValues = ((_a2 = element.getSelectedValues) == null ? void 0 : _a2.call(element)) || [];
407
755
  if (!areValuesEqual(values, currentValues)) {
408
756
  element.setSelectedValues(values);
409
757
  }
410
758
  }
411
- }, [value, isControlled, isElementReady, areValuesEqual]);
759
+ }, [value, isControlled, isElementUsable, areValuesEqual]);
412
760
  useEffect(() => {
413
761
  const element = elementRef.current;
414
- if (!element || !isElementReady || !element.updateConfig) return;
762
+ if (!element || !isElementUsable || !element.updateConfig) return;
415
763
  const config = {
416
764
  searchable,
417
765
  placeholder,
@@ -461,9 +809,9 @@ const Select = forwardRef((props, ref) => {
461
809
  autoMitigateRuntimeModeSwitch
462
810
  }
463
811
  };
464
- element.updateConfig(config);
812
+ applyInstanceConfig(element, config);
465
813
  if (coreConfig) {
466
- element.updateConfig(coreConfig);
814
+ applyInstanceConfig(element, coreConfig);
467
815
  }
468
816
  }, [
469
817
  searchable,
@@ -499,188 +847,232 @@ const Select = forwardRef((props, ref) => {
499
847
  autoMitigateRuntimeModeSwitch,
500
848
  direction,
501
849
  resolvedDropdownPlacement,
502
- isElementReady
850
+ isElementUsable,
851
+ applyInstanceConfig
503
852
  ]);
504
853
  useEffect(() => {
505
854
  const element = elementRef.current;
506
- if (!element || !isElementReady) return;
855
+ if (!element || !isElementUsable) return;
507
856
  if (error) {
508
857
  if (element.setError)
509
858
  element.setError(errorMessage || "Invalid selection");
510
859
  } else {
511
860
  if (element.clearError) element.clearError();
512
861
  }
513
- }, [error, errorMessage, isElementReady]);
862
+ }, [error, errorMessage, isElementUsable]);
514
863
  useEffect(() => {
515
864
  const element = elementRef.current;
516
- if (!element || !isElementReady || !element.setRequired) return;
865
+ if (!element || !isElementUsable || !element.setRequired) return;
517
866
  element.setRequired(required);
518
- }, [required, isElementReady]);
867
+ }, [required, isElementUsable]);
519
868
  useEffect(() => {
869
+ var _a2, _b2;
520
870
  const element = elementRef.current;
521
- if (!element) return;
522
- const handleSelect = (e) => {
523
- const { item, index } = e.detail;
524
- onSelect == null ? void 0 : onSelect(item, index);
525
- };
526
- const handleChange = (e) => {
527
- const { selectedItems, selectedValues } = e.detail;
528
- const values = selectedValues;
529
- const nextSingleValue = values.length > 0 ? values[0] : "";
530
- if (!isControlled) {
531
- setInternalValue(multiple ? values : nextSingleValue);
871
+ if (!element || !isElementUsable) return;
872
+ if (isCoreModuleLoaded && queuedImperativeConfigsRef.current.length > 0) {
873
+ const queued = queuedImperativeConfigsRef.current;
874
+ queuedImperativeConfigsRef.current = [];
875
+ for (const config of queued) {
876
+ const requestsAcknowledged = acceptedDataLoadingConfigRef.current.protocol === "acknowledged" || ((_a2 = config.dataLoading) == null ? void 0 : _a2.protocol) === "acknowledged";
877
+ if (requestsAcknowledged && !qualifyAcknowledgedElement()) break;
878
+ if (config.dataLoading !== void 0 || requestsAcknowledged) {
879
+ subscribeToCoreEvents(element);
880
+ }
881
+ applyInstanceConfig(element, config);
882
+ if (unusableRef.current) break;
532
883
  }
533
- if (onChange) {
534
- const value2 = multiple ? values : nextSingleValue;
535
- onChange(value2, selectedItems);
536
- }
537
- };
538
- const handleOpen = (e) => {
539
- onOpen == null ? void 0 : onOpen();
540
- };
541
- const handleClose = (e) => {
542
- onClose == null ? void 0 : onClose();
543
- };
544
- const handleSearch = (e) => {
545
- const { query, results, count } = e.detail;
546
- onSearch == null ? void 0 : onSearch(query, results, count);
547
- };
548
- const handleLoadMore = (e) => {
549
- const { page } = e.detail;
550
- onLoadMore == null ? void 0 : onLoadMore(page);
551
- };
552
- const handleClear = (e) => {
553
- onClear == null ? void 0 : onClear({
554
- clearedSelection: e.detail.clearedSelection,
555
- clearedSearch: e.detail.clearedSearch
556
- });
557
- };
558
- const handleDiagnostic = (e) => {
559
- onDiagnostic == null ? void 0 : onDiagnostic(e.detail);
560
- };
561
- element.addEventListener("select", handleSelect);
562
- element.addEventListener("change", handleChange);
563
- element.addEventListener("open", handleOpen);
564
- element.addEventListener("close", handleClose);
565
- element.addEventListener("search", handleSearch);
566
- element.addEventListener("loadMore", handleLoadMore);
567
- element.addEventListener("clear", handleClear);
568
- element.addEventListener("diagnostic", handleDiagnostic);
569
- return () => {
570
- element.removeEventListener("select", handleSelect);
571
- element.removeEventListener("change", handleChange);
572
- element.removeEventListener("open", handleOpen);
573
- element.removeEventListener("close", handleClose);
574
- element.removeEventListener("search", handleSearch);
575
- element.removeEventListener("loadMore", handleLoadMore);
576
- element.removeEventListener("clear", handleClear);
577
- element.removeEventListener("diagnostic", handleDiagnostic);
578
- };
884
+ }
885
+ initializingRef.current = false;
886
+ syncHostBusy(element);
887
+ if (acceptedDataLoadingConfigRef.current.protocol === "acknowledged") {
888
+ (_b2 = element.startDataLoading) == null ? void 0 : _b2.call(element);
889
+ }
579
890
  }, [
580
- onSelect,
581
- onChange,
582
- onOpen,
583
- onClose,
584
- onSearch,
585
- onLoadMore,
586
- onClear,
587
- onDiagnostic,
588
- isControlled,
589
- multiple
891
+ isElementUsable,
892
+ acceptedProtocol,
893
+ acknowledgedRequested,
894
+ coreConfig,
895
+ items,
896
+ groupedItems,
897
+ value,
898
+ defaultValue,
899
+ loading,
900
+ consumerAriaBusy,
901
+ syncHostBusy,
902
+ isCoreModuleLoaded,
903
+ qualifyAcknowledgedElement,
904
+ subscribeToCoreEvents,
905
+ applyInstanceConfig
906
+ ]);
907
+ useEffect(() => {
908
+ const element = elementRef.current;
909
+ if (!element || acknowledgedRequested || dataLoadingConfigured || !isElementReady) return;
910
+ return subscribeToCoreEvents(element);
911
+ }, [
912
+ acknowledgedRequested,
913
+ dataLoadingConfigured,
914
+ isElementReady,
915
+ subscribeToCoreEvents
590
916
  ]);
591
917
  useImperativeHandle(ref, () => ({
592
918
  focus: () => {
593
- var _a;
594
- (_a = elementRef.current) == null ? void 0 : _a.focus();
919
+ var _a2;
920
+ const element = elementRef.current;
921
+ const input = (_a2 = element == null ? void 0 : element.shadowRoot) == null ? void 0 : _a2.querySelector("input");
922
+ if (input instanceof HTMLElement) {
923
+ input.focus();
924
+ } else {
925
+ element == null ? void 0 : element.focus();
926
+ }
595
927
  },
596
928
  open: () => {
597
- var _a;
598
- (_a = elementRef.current) == null ? void 0 : _a.open();
929
+ var _a2;
930
+ (_a2 = elementRef.current) == null ? void 0 : _a2.open();
599
931
  },
600
932
  close: () => {
601
- var _a;
602
- (_a = elementRef.current) == null ? void 0 : _a.close();
933
+ var _a2;
934
+ (_a2 = elementRef.current) == null ? void 0 : _a2.close();
603
935
  },
604
936
  getSelectedItems: () => {
605
- var _a;
606
- return ((_a = elementRef.current) == null ? void 0 : _a.getSelectedItems()) || [];
937
+ var _a2;
938
+ return ((_a2 = elementRef.current) == null ? void 0 : _a2.getSelectedItems()) || [];
607
939
  },
608
940
  getSelectedValues: () => {
609
- var _a;
610
- return ((_a = elementRef.current) == null ? void 0 : _a.getSelectedValues()) || [];
941
+ var _a2;
942
+ return ((_a2 = elementRef.current) == null ? void 0 : _a2.getSelectedValues()) || [];
611
943
  },
612
944
  setItems: (items2) => {
613
- var _a;
614
- (_a = elementRef.current) == null ? void 0 : _a.setItems(items2);
945
+ var _a2;
946
+ (_a2 = elementRef.current) == null ? void 0 : _a2.setItems(items2);
615
947
  },
616
948
  setGroupedItems: (groups) => {
617
- var _a;
618
- (_a = elementRef.current) == null ? void 0 : _a.setGroupedItems(groups);
949
+ var _a2;
950
+ (_a2 = elementRef.current) == null ? void 0 : _a2.setGroupedItems(groups);
619
951
  },
620
952
  clear: () => {
621
- var _a;
622
- (_a = elementRef.current) == null ? void 0 : _a.setSelectedValues([]);
953
+ var _a2;
954
+ (_a2 = elementRef.current) == null ? void 0 : _a2.setSelectedValues([]);
623
955
  if (!isControlled) {
624
956
  setInternalValue(multiple ? [] : void 0);
625
957
  }
626
958
  onChange == null ? void 0 : onChange(multiple ? [] : "", []);
627
959
  },
628
960
  clearSearch: () => {
629
- var _a, _b;
630
- (_b = (_a = elementRef.current) == null ? void 0 : _a.clearSearch) == null ? void 0 : _b.call(_a);
961
+ var _a2, _b2;
962
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.clearSearch) == null ? void 0 : _b2.call(_a2);
631
963
  },
632
964
  updateConfig: (config) => {
633
- var _a, _b;
634
- (_b = (_a = elementRef.current) == null ? void 0 : _a.updateConfig) == null ? void 0 : _b.call(_a, config);
965
+ var _a2;
966
+ const element = elementRef.current;
967
+ if (!element || unusableRef.current) return;
968
+ const requestsAcknowledged = acceptedDataLoadingConfigRef.current.protocol === "acknowledged" || ((_a2 = config.dataLoading) == null ? void 0 : _a2.protocol) === "acknowledged";
969
+ if (!isElementReady || !isCoreModuleLoaded) {
970
+ queuedImperativeConfigsRef.current.push({ ...config });
971
+ return;
972
+ }
973
+ if (requestsAcknowledged && !qualifyAcknowledgedElement()) return;
974
+ if (config.dataLoading !== void 0 || requestsAcknowledged) {
975
+ subscribeToCoreEvents(element);
976
+ }
977
+ applyInstanceConfig(element, config);
978
+ },
979
+ getDataLoadingState: () => {
980
+ var _a2, _b2, _c;
981
+ const snapshot = (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getDataLoadingState) == null ? void 0 : _b2.call(_a2);
982
+ if (snapshot) return snapshot;
983
+ return {
984
+ status: "idle",
985
+ connected: Boolean((_c = elementRef.current) == null ? void 0 : _c.isConnected),
986
+ busy: false,
987
+ projectionPending: false,
988
+ queryGeneration: 0,
989
+ activeRequest: null,
990
+ hasMore: false,
991
+ nextEligiblePosition: {
992
+ mode: "page",
993
+ page: DEFAULT_DATA_LOADING_CONFIG.firstPage
994
+ },
995
+ error: null,
996
+ resourceLimit: null
997
+ };
998
+ },
999
+ completeDataRequest: (completion) => {
1000
+ var _a2, _b2;
1001
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.completeDataRequest) == null ? void 0 : _b2.call(_a2, completion)) ?? "unknown-request";
635
1002
  },
636
1003
  setError: (message) => {
637
- var _a, _b;
638
- (_b = (_a = elementRef.current) == null ? void 0 : _a.setError) == null ? void 0 : _b.call(_a, message);
1004
+ var _a2, _b2;
1005
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.setError) == null ? void 0 : _b2.call(_a2, message);
639
1006
  },
640
1007
  clearError: () => {
641
- var _a, _b;
642
- (_b = (_a = elementRef.current) == null ? void 0 : _a.clearError) == null ? void 0 : _b.call(_a);
1008
+ var _a2, _b2;
1009
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.clearError) == null ? void 0 : _b2.call(_a2);
643
1010
  },
644
1011
  setRequired: (isRequired) => {
645
- var _a, _b;
646
- (_b = (_a = elementRef.current) == null ? void 0 : _a.setRequired) == null ? void 0 : _b.call(_a, isRequired);
1012
+ var _a2, _b2;
1013
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.setRequired) == null ? void 0 : _b2.call(_a2, isRequired);
647
1014
  },
648
1015
  validate: () => {
649
- var _a, _b;
650
- return ((_b = (_a = elementRef.current) == null ? void 0 : _a.validate) == null ? void 0 : _b.call(_a)) ?? true;
1016
+ var _a2, _b2;
1017
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.validate) == null ? void 0 : _b2.call(_a2)) ?? true;
651
1018
  },
652
1019
  getCapabilities: () => {
653
- var _a, _b;
654
- return (_b = (_a = elementRef.current) == null ? void 0 : _a.getCapabilities) == null ? void 0 : _b.call(_a);
1020
+ var _a2, _b2;
1021
+ return (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getCapabilities) == null ? void 0 : _b2.call(_a2);
655
1022
  },
656
1023
  getKnownLimitations: () => {
657
- var _a, _b;
658
- return ((_b = (_a = elementRef.current) == null ? void 0 : _a.getKnownLimitations) == null ? void 0 : _b.call(_a)) || [];
1024
+ var _a2, _b2;
1025
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getKnownLimitations) == null ? void 0 : _b2.call(_a2)) || [];
659
1026
  },
660
1027
  getTrackingSnapshot: () => {
661
- var _a, _b;
662
- return ((_b = (_a = elementRef.current) == null ? void 0 : _a.getTrackingSnapshot) == null ? void 0 : _b.call(_a)) || {
1028
+ var _a2, _b2;
1029
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getTrackingSnapshot) == null ? void 0 : _b2.call(_a2)) || {
663
1030
  events: [],
664
1031
  styles: [],
665
1032
  limitations: []
666
1033
  };
667
1034
  },
668
1035
  clearTracking: (source) => {
669
- var _a, _b;
670
- (_b = (_a = elementRef.current) == null ? void 0 : _a.clearTracking) == null ? void 0 : _b.call(_a, source);
1036
+ var _a2, _b2;
1037
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.clearTracking) == null ? void 0 : _b2.call(_a2, source);
671
1038
  },
672
1039
  setLimitationPolicies: (policies) => {
673
- var _a, _b;
674
- (_b = (_a = elementRef.current) == null ? void 0 : _a.setLimitationPolicies) == null ? void 0 : _b.call(_a, policies);
1040
+ var _a2, _b2;
1041
+ (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.setLimitationPolicies) == null ? void 0 : _b2.call(_a2, policies);
675
1042
  }
676
- }));
677
- return React.createElement("enhanced-select", {
678
- ref: elementRef,
679
- ...hostProps,
680
- className,
681
- dir: direction,
682
- style
683
- });
1043
+ }), [
1044
+ applyInstanceConfig,
1045
+ isElementReady,
1046
+ isCoreModuleLoaded,
1047
+ qualifyAcknowledgedElement,
1048
+ subscribeToCoreEvents,
1049
+ isElementUsable,
1050
+ isControlled,
1051
+ multiple,
1052
+ onChange
1053
+ ]);
1054
+ const renderedAriaBusy = acceptedProtocol === "acknowledged" && !isUnusable ? composeBusyToken(consumerAriaBusy, loading) : consumerAriaBusy;
1055
+ return React.createElement(
1056
+ "enhanced-select",
1057
+ {
1058
+ ref: elementRef,
1059
+ ...hostProps,
1060
+ "aria-busy": renderedAriaBusy,
1061
+ "data-custom-arrow": arrowIcon != null ? "true" : void 0,
1062
+ class: className,
1063
+ dir: direction,
1064
+ style
1065
+ },
1066
+ arrowIcon != null ? React.createElement(
1067
+ "span",
1068
+ {
1069
+ slot: "arrow",
1070
+ className: "smilodon-custom-arrow",
1071
+ "aria-hidden": "true"
1072
+ },
1073
+ arrowIcon
1074
+ ) : null
1075
+ );
684
1076
  });
685
1077
  Select.displayName = "Select";
686
1078
  export {