@smilodon/react 1.12.0 → 1.12.2

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
@@ -8,7 +8,8 @@ const DEFAULT_DATA_LOADING_CONFIG = {
8
8
  protocol: "legacy",
9
9
  searchMode: "local",
10
10
  paginationMode: "page",
11
- firstPage: 1
11
+ firstPage: 1,
12
+ minSearchCharacters: 0
12
13
  };
13
14
  function isPlainRecord(value) {
14
15
  if (value === null || typeof value !== "object" || Array.isArray(value)) {
@@ -17,6 +18,64 @@ function isPlainRecord(value) {
17
18
  const prototype = Object.getPrototypeOf(value);
18
19
  return prototype === Object.prototype || prototype === null;
19
20
  }
21
+ function areSemanticallyEqual(left, right, seen = /* @__PURE__ */ new WeakMap()) {
22
+ var _a;
23
+ if (Object.is(left, right)) return true;
24
+ if (left === null || right === null || typeof left !== "object" || typeof right !== "object") {
25
+ return false;
26
+ }
27
+ const leftArray = Array.isArray(left);
28
+ const rightArray = Array.isArray(right);
29
+ if (leftArray !== rightArray) return false;
30
+ if (!leftArray && (!isPlainRecord(left) || !isPlainRecord(right))) return false;
31
+ if (leftArray && !Array.isArray(right)) return false;
32
+ if (leftArray && rightArray && left.length !== right.length) return false;
33
+ const leftPrototype = Object.getPrototypeOf(left);
34
+ const rightPrototype = Object.getPrototypeOf(right);
35
+ if (leftPrototype !== rightPrototype && !(isPlainRecord(left) && isPlainRecord(right))) return false;
36
+ const prior = (_a = seen.get(left)) == null ? void 0 : _a.get(right);
37
+ if (prior !== void 0) return prior;
38
+ let rightPairs = seen.get(left);
39
+ if (!rightPairs) {
40
+ rightPairs = /* @__PURE__ */ new WeakMap();
41
+ seen.set(left, rightPairs);
42
+ }
43
+ rightPairs.set(right, true);
44
+ const enumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => {
45
+ var _a2;
46
+ return (_a2 = Object.getOwnPropertyDescriptor(value, key)) == null ? void 0 : _a2.enumerable;
47
+ });
48
+ const leftKeys = enumerableKeys(left);
49
+ const rightKeys = enumerableKeys(right);
50
+ const rightKeySet = new Set(rightKeys);
51
+ if (leftKeys.length !== rightKeys.length) {
52
+ rightPairs.set(right, false);
53
+ return false;
54
+ }
55
+ for (const key of leftKeys) {
56
+ if (!rightKeySet.has(key)) {
57
+ rightPairs.set(right, false);
58
+ return false;
59
+ }
60
+ const leftDescriptor = Object.getOwnPropertyDescriptor(left, key);
61
+ const rightDescriptor = Object.getOwnPropertyDescriptor(right, key);
62
+ if ("value" in leftDescriptor !== "value" in rightDescriptor) {
63
+ rightPairs.set(right, false);
64
+ return false;
65
+ }
66
+ if ("value" in leftDescriptor) {
67
+ if (!areSemanticallyEqual(leftDescriptor.value, rightDescriptor.value, seen)) {
68
+ rightPairs.set(right, false);
69
+ return false;
70
+ }
71
+ } else if (leftDescriptor.get !== rightDescriptor.get || leftDescriptor.set !== rightDescriptor.set) {
72
+ rightPairs.set(right, false);
73
+ return false;
74
+ }
75
+ }
76
+ return true;
77
+ }
78
+ const EMPTY_ITEMS = [];
20
79
  function isBusyToken(value) {
21
80
  return value === true || value === "true";
22
81
  }
@@ -28,7 +87,7 @@ function composeBusyToken(consumerValue, externalBusy) {
28
87
  const Select = forwardRef((props, ref) => {
29
88
  var _a, _b, _c;
30
89
  const {
31
- items = [],
90
+ items = EMPTY_ITEMS,
32
91
  groupedItems,
33
92
  value,
34
93
  defaultValue,
@@ -195,6 +254,13 @@ const Select = forwardRef((props, ref) => {
195
254
  }
196
255
  Promise.resolve().then(() => root.unmount());
197
256
  }, []);
257
+ const reconcileRendererCache = useCallback((cache, renderedOptions) => {
258
+ for (const [index, entry] of cache) {
259
+ if (renderedOptions.contains(entry.container)) continue;
260
+ if (cache.get(index) === entry) cache.delete(index);
261
+ scheduleRootUnmount(entry.root);
262
+ }
263
+ }, [scheduleRootUnmount]);
198
264
  const customRendererRef = useRef(customRenderer ?? renderItem);
199
265
  const optionRendererRef = useRef(optionRenderer);
200
266
  const groupHeaderRendererRef = useRef(groupHeaderRenderer);
@@ -254,24 +320,6 @@ const Select = forwardRef((props, ref) => {
254
320
  groupHeaderRendererCache.current.clear();
255
321
  };
256
322
  }, [scheduleRootUnmount]);
257
- useEffect(() => {
258
- const totalItems = groupedItems ? groupedItems.reduce((count, group) => count + group.options.length, 0) : items.length;
259
- reactRendererCache.current.forEach((entry, index) => {
260
- if (index >= totalItems) {
261
- scheduleRootUnmount(entry.root);
262
- reactRendererCache.current.delete(index);
263
- }
264
- });
265
- }, [items, groupedItems, scheduleRootUnmount]);
266
- useEffect(() => {
267
- const totalGroups = (groupedItems == null ? void 0 : groupedItems.length) ?? 0;
268
- groupHeaderRendererCache.current.forEach((entry, index) => {
269
- if (index >= totalGroups) {
270
- scheduleRootUnmount(entry.root);
271
- groupHeaderRendererCache.current.delete(index);
272
- }
273
- });
274
- }, [groupedItems, scheduleRootUnmount]);
275
323
  useEffect(() => {
276
324
  if (groupHeaderRenderer) return;
277
325
  groupHeaderRendererCache.current.forEach(
@@ -293,6 +341,37 @@ const Select = forwardRef((props, ref) => {
293
341
  const dataLoadingConfigured = (coreConfig == null ? void 0 : coreConfig.dataLoading) !== void 0;
294
342
  const coreFacadeRequired = acknowledgedRequested || dataLoadingConfigured;
295
343
  const isElementUsable = isElementReady && (!acknowledgedRequested || isAcknowledgedQualified) && (!coreFacadeRequired || isCoreModuleLoaded) && !isUnusable;
344
+ useEffect(() => {
345
+ var _a2;
346
+ if (!isElementReady) return;
347
+ const shadow = (_a2 = elementRef.current) == null ? void 0 : _a2.shadowRoot;
348
+ const renderedOptions = shadow == null ? void 0 : shadow.querySelector(".options-container");
349
+ if (!renderedOptions) return;
350
+ let disposed = false;
351
+ let reconciliationQueued = false;
352
+ const reconcile = () => {
353
+ reconciliationQueued = false;
354
+ if (disposed) return;
355
+ reconcileRendererCache(reactRendererCache.current, renderedOptions);
356
+ reconcileRendererCache(groupHeaderRendererCache.current, renderedOptions);
357
+ };
358
+ const queueReconciliation = () => {
359
+ if (reconciliationQueued || disposed) return;
360
+ reconciliationQueued = true;
361
+ if (typeof queueMicrotask === "function") {
362
+ queueMicrotask(reconcile);
363
+ } else {
364
+ Promise.resolve().then(reconcile);
365
+ }
366
+ };
367
+ const observer = new MutationObserver(queueReconciliation);
368
+ observer.observe(renderedOptions, { childList: true });
369
+ queueReconciliation();
370
+ return () => {
371
+ disposed = true;
372
+ observer.disconnect();
373
+ };
374
+ }, [isElementReady, reconcileRendererCache]);
296
375
  const markUnusable = useCallback(
297
376
  (message) => {
298
377
  if (unusableRef.current) return;
@@ -615,156 +694,108 @@ const Select = forwardRef((props, ref) => {
615
694
  return (_a2 = activeListenersRef.current) == null ? void 0 : _a2.dispose();
616
695
  };
617
696
  }, []);
618
- useEffect(() => {
619
- var _a2;
620
- const element = elementRef.current;
621
- if (!element || !isElementUsable) return;
622
- if (!element.setItems) {
623
- console.warn("Enhanced select element not fully initialized yet");
624
- return;
625
- }
626
- if (groupedItems) {
627
- element.setGroupedItems(groupedItems);
628
- } else if (items.length > 0) {
629
- element.setItems(items);
630
- }
631
- const config = {
632
- searchable,
633
- placeholder,
634
- virtualize: virtualized,
635
- estimatedItemHeight,
636
- enabled: !disabled,
637
- dropdownPlacement: {
638
- mode: resolvedDropdownPlacement
639
- },
640
- selection: {
641
- ...selectionConfig,
642
- mode: multiple ? "multi" : "single",
643
- maxSelections,
644
- removeButtonIcon: typeof removeButtonIcon === "string" ? removeButtonIcon : void 0,
645
- disabledOptionBehavior,
646
- showSelectedIndicator,
647
- toggleOnTriggerClick
648
- },
649
- direction,
650
- multiSelectDisplay,
651
- infiniteScroll: {
652
- enabled: infiniteScroll,
653
- pageSize
654
- },
655
- scrollToSelected: {
656
- enabled: true,
657
- ...scrollToSelected
658
- },
659
- creatable,
660
- styles: stylesConfig,
661
- clearControl: {
662
- enabled: clearable,
663
- clearSelection: clearSelectionOnClear,
664
- clearSearch: clearSearchOnClear,
665
- ariaLabel: clearAriaLabel,
666
- icon: clearIcon
667
- },
668
- tracking: {
669
- enabled: trackingEnabled,
670
- events: trackEvents,
671
- styling: trackStyling,
672
- limitations: trackLimitations,
673
- emitDiagnostics,
674
- maxEntries: trackingMaxEntries
675
- },
676
- limitations: {
677
- policies: limitationPolicies,
678
- autoMitigateRuntimeModeSwitch
679
- }
680
- };
681
- applyInstanceConfig(element, config);
682
- if (coreConfig) {
683
- applyInstanceConfig(element, coreConfig);
684
- }
685
- if (!isControlled && !hasAppliedInitialValueRef.current && internalValue !== void 0) {
686
- const values = Array.isArray(internalValue) ? internalValue : [internalValue];
687
- const currentValues = ((_a2 = element.getSelectedValues) == null ? void 0 : _a2.call(element)) || [];
688
- if (!areValuesEqual(values, currentValues)) {
689
- element.setSelectedValues(values);
690
- }
691
- hasAppliedInitialValueRef.current = true;
692
- }
693
- if (error) {
694
- element.setError(errorMessage || "Invalid selection");
695
- } else {
696
- element.clearError();
697
- }
698
- if (required) {
699
- element.setRequired(true);
700
- }
701
- }, [
702
- isElementUsable,
703
- items,
704
- groupedItems,
697
+ const componentConfig = {
705
698
  searchable,
706
699
  placeholder,
707
- virtualized,
700
+ virtualize: virtualized,
708
701
  estimatedItemHeight,
709
- disabled,
710
- multiple,
711
- maxSelections,
712
- selectionConfig,
702
+ enabled: !disabled,
703
+ dropdownPlacement: { mode: resolvedDropdownPlacement },
704
+ selection: {
705
+ ...selectionConfig,
706
+ mode: multiple ? "multi" : "single",
707
+ maxSelections,
708
+ removeButtonIcon: typeof removeButtonIcon === "string" ? removeButtonIcon : void 0,
709
+ disabledOptionBehavior,
710
+ showSelectedIndicator,
711
+ toggleOnTriggerClick
712
+ },
713
+ direction,
713
714
  multiSelectDisplay,
714
- scrollToSelected,
715
- stylesConfig,
716
- coreConfig,
717
- removeButtonIcon,
718
- disabledOptionBehavior,
719
- showSelectedIndicator,
720
- toggleOnTriggerClick,
721
- infiniteScroll,
722
- pageSize,
715
+ infiniteScroll: { enabled: infiniteScroll, pageSize },
716
+ scrollToSelected: { enabled: true, ...scrollToSelected },
723
717
  creatable,
724
- clearable,
725
- clearSelectionOnClear,
726
- clearSearchOnClear,
727
- clearAriaLabel,
728
- clearIcon,
729
- trackingEnabled,
730
- trackEvents,
731
- trackStyling,
732
- trackLimitations,
733
- emitDiagnostics,
734
- trackingMaxEntries,
735
- limitationPolicies,
736
- autoMitigateRuntimeModeSwitch,
737
- direction,
738
- resolvedDropdownPlacement,
739
- error,
740
- errorMessage,
741
- required,
742
- internalValue,
743
- isControlled,
744
- areValuesEqual,
745
- applyInstanceConfig
746
- ]);
718
+ styles: stylesConfig,
719
+ clearControl: {
720
+ enabled: clearable,
721
+ clearSelection: clearSelectionOnClear,
722
+ clearSearch: clearSearchOnClear,
723
+ ariaLabel: clearAriaLabel,
724
+ icon: clearIcon
725
+ },
726
+ tracking: {
727
+ enabled: trackingEnabled,
728
+ events: trackEvents,
729
+ styling: trackStyling,
730
+ limitations: trackLimitations,
731
+ emitDiagnostics,
732
+ maxEntries: trackingMaxEntries
733
+ },
734
+ limitations: { policies: limitationPolicies, autoMitigateRuntimeModeSwitch }
735
+ };
736
+ const configCandidate = { component: componentConfig, core: coreConfig };
737
+ const appliedConfigRef = useRef({ hasComponent: false, component: null, hasCore: false, core: void 0 });
738
+ useEffect(() => {
739
+ const element = elementRef.current;
740
+ if (!element || !isElementUsable || !element.updateConfig) return;
741
+ const previous = appliedConfigRef.current;
742
+ const componentChanged = !previous.hasComponent || !areSemanticallyEqual(previous.component, configCandidate.component);
743
+ const coreChanged = !previous.hasCore || !areSemanticallyEqual(previous.core, configCandidate.core);
744
+ if (!componentChanged && !coreChanged) return;
745
+ if (componentChanged) {
746
+ applyInstanceConfig(element, configCandidate.component);
747
+ if (configCandidate.core) applyInstanceConfig(element, configCandidate.core);
748
+ } else if (coreChanged) {
749
+ if (configCandidate.core) {
750
+ applyInstanceConfig(element, configCandidate.core);
751
+ } else {
752
+ applyInstanceConfig(element, configCandidate.component);
753
+ }
754
+ }
755
+ appliedConfigRef.current = {
756
+ hasComponent: true,
757
+ component: configCandidate.component,
758
+ hasCore: true,
759
+ core: configCandidate.core
760
+ };
761
+ });
762
+ const sourceCandidate = groupedItems !== void 0 ? { kind: "grouped", data: groupedItems } : { kind: "items", data: items };
763
+ const synchronizedSourceRef = useRef(null);
747
764
  useEffect(() => {
765
+ var _a2;
748
766
  const element = elementRef.current;
749
767
  if (!element || !isElementUsable || !element.setItems) return;
750
- if (groupedItems) {
751
- element.setGroupedItems(groupedItems);
752
- } else if (items && items.length && items[0] && items[0].group !== void 0) {
753
- const map = /* @__PURE__ */ new Map();
754
- items.forEach((it) => {
755
- const g = it.group ?? "Ungrouped";
756
- if (!map.has(g)) map.set(g, []);
757
- map.get(g).push(it);
758
- });
759
- const groups = Array.from(map.entries()).map(([label, options]) => ({
760
- label,
761
- options
762
- }));
763
- element.setGroupedItems(groups);
768
+ if (areSemanticallyEqual(synchronizedSourceRef.current, sourceCandidate)) return;
769
+ if (sourceCandidate.kind === "grouped") {
770
+ element.setGroupedItems(sourceCandidate.data);
764
771
  } else {
765
- element.setItems(items);
772
+ const sourceItems = sourceCandidate.data;
773
+ if (sourceItems.length > 0 && ((_a2 = sourceItems[0]) == null ? void 0 : _a2.group) !== void 0) {
774
+ const groups = /* @__PURE__ */ new Map();
775
+ sourceItems.forEach((item) => {
776
+ const label = item.group ?? "Ungrouped";
777
+ if (!groups.has(label)) groups.set(label, []);
778
+ groups.get(label).push(item);
779
+ });
780
+ element.setGroupedItems(
781
+ Array.from(groups, ([label, options]) => ({ label, options }))
782
+ );
783
+ } else {
784
+ element.setItems(sourceItems);
785
+ }
766
786
  }
767
- }, [items, groupedItems, isElementUsable]);
787
+ synchronizedSourceRef.current = sourceCandidate;
788
+ });
789
+ useEffect(() => {
790
+ var _a2;
791
+ const element = elementRef.current;
792
+ if (!element || !isElementUsable || isControlled || hasAppliedInitialValueRef.current) return;
793
+ if (internalValue === void 0) return;
794
+ const values = Array.isArray(internalValue) ? internalValue : [internalValue];
795
+ const currentValues = ((_a2 = element.getSelectedValues) == null ? void 0 : _a2.call(element)) || [];
796
+ if (!areValuesEqual(values, currentValues)) element.setSelectedValues(values);
797
+ hasAppliedInitialValueRef.current = true;
798
+ }, [isElementUsable, isControlled, internalValue, areValuesEqual]);
768
799
  useEffect(() => {
769
800
  var _a2;
770
801
  const element = elementRef.current;
@@ -778,99 +809,6 @@ const Select = forwardRef((props, ref) => {
778
809
  }
779
810
  }
780
811
  }, [value, isControlled, isElementUsable, areValuesEqual]);
781
- useEffect(() => {
782
- const element = elementRef.current;
783
- if (!element || !isElementUsable || !element.updateConfig) return;
784
- const config = {
785
- searchable,
786
- placeholder,
787
- virtualize: virtualized,
788
- estimatedItemHeight,
789
- enabled: !disabled,
790
- dropdownPlacement: {
791
- mode: resolvedDropdownPlacement
792
- },
793
- selection: {
794
- ...selectionConfig,
795
- mode: multiple ? "multi" : "single",
796
- maxSelections,
797
- removeButtonIcon: typeof removeButtonIcon === "string" ? removeButtonIcon : void 0,
798
- disabledOptionBehavior,
799
- showSelectedIndicator,
800
- toggleOnTriggerClick
801
- },
802
- direction,
803
- multiSelectDisplay,
804
- infiniteScroll: {
805
- enabled: infiniteScroll,
806
- pageSize
807
- },
808
- scrollToSelected: {
809
- enabled: true,
810
- ...scrollToSelected
811
- },
812
- styles: stylesConfig,
813
- clearControl: {
814
- enabled: clearable,
815
- clearSelection: clearSelectionOnClear,
816
- clearSearch: clearSearchOnClear,
817
- ariaLabel: clearAriaLabel,
818
- icon: clearIcon
819
- },
820
- tracking: {
821
- enabled: trackingEnabled,
822
- events: trackEvents,
823
- styling: trackStyling,
824
- limitations: trackLimitations,
825
- emitDiagnostics,
826
- maxEntries: trackingMaxEntries
827
- },
828
- limitations: {
829
- policies: limitationPolicies,
830
- autoMitigateRuntimeModeSwitch
831
- }
832
- };
833
- applyInstanceConfig(element, config);
834
- if (coreConfig) {
835
- applyInstanceConfig(element, coreConfig);
836
- }
837
- }, [
838
- searchable,
839
- placeholder,
840
- virtualized,
841
- estimatedItemHeight,
842
- disabled,
843
- multiple,
844
- maxSelections,
845
- selectionConfig,
846
- multiSelectDisplay,
847
- scrollToSelected,
848
- stylesConfig,
849
- coreConfig,
850
- removeButtonIcon,
851
- disabledOptionBehavior,
852
- showSelectedIndicator,
853
- toggleOnTriggerClick,
854
- infiniteScroll,
855
- pageSize,
856
- clearable,
857
- clearSelectionOnClear,
858
- clearSearchOnClear,
859
- clearAriaLabel,
860
- clearIcon,
861
- trackingEnabled,
862
- trackEvents,
863
- trackStyling,
864
- trackLimitations,
865
- emitDiagnostics,
866
- trackingMaxEntries,
867
- limitationPolicies,
868
- autoMitigateRuntimeModeSwitch,
869
- direction,
870
- resolvedDropdownPlacement,
871
- isElementUsable,
872
- applyInstanceConfig
873
- ]);
874
812
  useEffect(() => {
875
813
  const element = elementRef.current;
876
814
  const shadow = element == null ? void 0 : element.shadowRoot;
@@ -954,11 +892,6 @@ const Select = forwardRef((props, ref) => {
954
892
  isElementUsable,
955
893
  acceptedProtocol,
956
894
  acknowledgedRequested,
957
- coreConfig,
958
- items,
959
- groupedItems,
960
- value,
961
- defaultValue,
962
895
  loading,
963
896
  consumerAriaBusy,
964
897
  syncHostBusy,