@smilodon/react 1.11.0 → 1.12.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
@@ -2,6 +2,7 @@
2
2
  import { jsx, Fragment } from "react/jsx-runtime";
3
3
  import React, { forwardRef, useState, useRef, useMemo, useCallback, useEffect, useImperativeHandle } from "react";
4
4
  import { createRoot } from "react-dom/client";
5
+ import { createPortal } from "react-dom";
5
6
  import { configureSelect, resetSelectConfig, selectConfig } from "@smilodon/core";
6
7
  const DEFAULT_DATA_LOADING_CONFIG = {
7
8
  protocol: "legacy",
@@ -25,7 +26,7 @@ function composeBusyToken(consumerValue, externalBusy) {
25
26
  return consumerValue;
26
27
  }
27
28
  const Select = forwardRef((props, ref) => {
28
- var _a, _b;
29
+ var _a, _b, _c;
29
30
  const {
30
31
  items = [],
31
32
  groupedItems,
@@ -50,6 +51,9 @@ const Select = forwardRef((props, ref) => {
50
51
  config: coreConfig,
51
52
  removeButtonIcon,
52
53
  arrowIcon,
54
+ loadingIcon,
55
+ loadingContent,
56
+ emptyStateRenderer,
53
57
  disabledOptionBehavior,
54
58
  showSelectedIndicator = true,
55
59
  toggleOnTriggerClick = true,
@@ -77,6 +81,7 @@ const Select = forwardRef((props, ref) => {
77
81
  onDataRequestCancel,
78
82
  onDataProjection,
79
83
  onDataLoadingConfigError,
84
+ onEmptyStateChange,
80
85
  trackingEnabled = false,
81
86
  trackEvents = true,
82
87
  trackStyling = true,
@@ -118,6 +123,7 @@ const Select = forwardRef((props, ref) => {
118
123
  onDataRequestCancel,
119
124
  onDataProjection,
120
125
  onDataLoadingConfigError,
126
+ onEmptyStateChange,
121
127
  isControlled,
122
128
  multiple
123
129
  });
@@ -133,6 +139,7 @@ const Select = forwardRef((props, ref) => {
133
139
  onDataRequestCancel,
134
140
  onDataProjection,
135
141
  onDataLoadingConfigError,
142
+ onEmptyStateChange,
136
143
  isControlled,
137
144
  multiple
138
145
  };
@@ -279,7 +286,10 @@ const Select = forwardRef((props, ref) => {
279
286
  reactRendererCache.current.clear();
280
287
  }, [!!optionRenderer, !!(customRenderer ?? renderItem), scheduleRootUnmount]);
281
288
  const [isElementReady, setIsElementReady] = useState(false);
282
- const acknowledgedRequested = acceptedProtocol === "acknowledged" || ((_a = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _a.protocol) === "acknowledged";
289
+ const [emptyStateContext, setEmptyStateContext] = useState(null);
290
+ const [removeIconTargets, setRemoveIconTargets] = useState([]);
291
+ const shouldPortalRemoveIcon = multiple && removeButtonIcon != null && typeof removeButtonIcon !== "string" && typeof removeButtonIcon !== "boolean" && ((_a = coreConfig == null ? void 0 : coreConfig.selection) == null ? void 0 : _a.removeButtonIcon) === void 0;
292
+ const acknowledgedRequested = acceptedProtocol === "acknowledged" || ((_b = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _b.protocol) === "acknowledged";
283
293
  const dataLoadingConfigured = (coreConfig == null ? void 0 : coreConfig.dataLoading) !== void 0;
284
294
  const coreFacadeRequired = acknowledgedRequested || dataLoadingConfigured;
285
295
  const isElementUsable = isElementReady && (!acknowledgedRequested || isAcknowledgedQualified) && (!coreFacadeRequired || isCoreModuleLoaded) && !isUnusable;
@@ -386,7 +396,7 @@ const Select = forwardRef((props, ref) => {
386
396
  },
387
397
  [markUnusable, syncHostBusy]
388
398
  );
389
- const requiresAcknowledgedQualification = acceptedProtocol === "acknowledged" || ((_b = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _b.protocol) === "acknowledged";
399
+ const requiresAcknowledgedQualification = acceptedProtocol === "acknowledged" || ((_c = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _c.protocol) === "acknowledged";
390
400
  const qualifyAcknowledgedElement = useCallback(() => {
391
401
  const element = elementRef.current;
392
402
  const module = coreModuleRef.current;
@@ -404,16 +414,17 @@ const Select = forwardRef((props, ref) => {
404
414
  return true;
405
415
  }, [markUnusable]);
406
416
  const subscribeToCoreEvents = useCallback((element) => {
417
+ var _a2;
407
418
  const currentSubscription = activeListenersRef.current;
408
419
  if ((currentSubscription == null ? void 0 : currentSubscription.element) === element) return currentSubscription.dispose;
409
420
  currentSubscription == null ? void 0 : currentSubscription.dispose();
410
421
  const handleSelect = (event) => {
411
- var _a2, _b2;
422
+ var _a3, _b2;
412
423
  const detail = event.detail;
413
- (_b2 = (_a2 = callbackStateRef.current).onSelect) == null ? void 0 : _b2.call(_a2, detail.item, detail.index);
424
+ (_b2 = (_a3 = callbackStateRef.current).onSelect) == null ? void 0 : _b2.call(_a3, detail.item, detail.index);
414
425
  };
415
426
  const handleChange = (event) => {
416
- var _a2;
427
+ var _a3;
417
428
  const { selectedItems, selectedValues } = event.detail;
418
429
  const callbacks = callbackStateRef.current;
419
430
  const values = selectedValues;
@@ -422,19 +433,19 @@ const Select = forwardRef((props, ref) => {
422
433
  if (!callbacks.isControlled) {
423
434
  setInternalValue(callbacks.multiple ? values : nextSingleValue);
424
435
  }
425
- (_a2 = callbacks.onChange) == null ? void 0 : _a2.call(
436
+ (_a3 = callbacks.onChange) == null ? void 0 : _a3.call(
426
437
  callbacks,
427
438
  callbacks.multiple ? values : nextSingleValue,
428
439
  selectedItems
429
440
  );
430
441
  };
431
442
  const handleOpen = () => {
432
- var _a2, _b2;
433
- return (_b2 = (_a2 = callbackStateRef.current).onOpen) == null ? void 0 : _b2.call(_a2);
443
+ var _a3, _b2;
444
+ return (_b2 = (_a3 = callbackStateRef.current).onOpen) == null ? void 0 : _b2.call(_a3);
434
445
  };
435
446
  const handleClose = () => {
436
- var _a2, _b2;
437
- return (_b2 = (_a2 = callbackStateRef.current).onClose) == null ? void 0 : _b2.call(_a2);
447
+ var _a3, _b2;
448
+ return (_b2 = (_a3 = callbackStateRef.current).onClose) == null ? void 0 : _b2.call(_a3);
438
449
  };
439
450
  const handleSearch = (event) => {
440
451
  const { query, results, count, request } = event.detail;
@@ -451,40 +462,46 @@ const Select = forwardRef((props, ref) => {
451
462
  else callback(page);
452
463
  };
453
464
  const handleClear = (event) => {
454
- var _a2, _b2;
465
+ var _a3, _b2;
455
466
  const { clearedSelection, clearedSearch } = event.detail;
456
- (_b2 = (_a2 = callbackStateRef.current).onClear) == null ? void 0 : _b2.call(_a2, { clearedSelection, clearedSearch });
467
+ (_b2 = (_a3 = callbackStateRef.current).onClear) == null ? void 0 : _b2.call(_a3, { clearedSelection, clearedSearch });
457
468
  };
458
469
  const handleDiagnostic = (event) => {
459
- var _a2, _b2;
460
- (_b2 = (_a2 = callbackStateRef.current).onDiagnostic) == null ? void 0 : _b2.call(
461
- _a2,
470
+ var _a3, _b2;
471
+ (_b2 = (_a3 = callbackStateRef.current).onDiagnostic) == null ? void 0 : _b2.call(
472
+ _a3,
462
473
  event.detail
463
474
  );
464
475
  };
465
476
  const handleDataRequestCancel = (event) => {
466
- var _a2, _b2;
467
- (_b2 = (_a2 = callbackStateRef.current).onDataRequestCancel) == null ? void 0 : _b2.call(
468
- _a2,
477
+ var _a3, _b2;
478
+ (_b2 = (_a3 = callbackStateRef.current).onDataRequestCancel) == null ? void 0 : _b2.call(
479
+ _a3,
469
480
  event.detail
470
481
  );
471
482
  };
472
483
  const handleDataProjection = (event) => {
473
- var _a2, _b2;
474
- (_b2 = (_a2 = callbackStateRef.current).onDataProjection) == null ? void 0 : _b2.call(
475
- _a2,
484
+ var _a3, _b2;
485
+ (_b2 = (_a3 = callbackStateRef.current).onDataProjection) == null ? void 0 : _b2.call(
486
+ _a3,
476
487
  event.detail
477
488
  );
478
489
  };
479
490
  const handleDataLoadingConfigError = (event) => {
480
- var _a2, _b2;
491
+ var _a3, _b2;
481
492
  const activeUpdate = configUpdateStackRef.current[configUpdateStackRef.current.length - 1];
482
493
  if (activeUpdate) activeUpdate.sawConfigError = true;
483
- (_b2 = (_a2 = callbackStateRef.current).onDataLoadingConfigError) == null ? void 0 : _b2.call(
484
- _a2,
494
+ (_b2 = (_a3 = callbackStateRef.current).onDataLoadingConfigError) == null ? void 0 : _b2.call(
495
+ _a3,
485
496
  event.detail
486
497
  );
487
498
  };
499
+ const handleEmptyStateChange = (event) => {
500
+ var _a3, _b2;
501
+ const context = event.detail.context;
502
+ setEmptyStateContext(context);
503
+ (_b2 = (_a3 = callbackStateRef.current).onEmptyStateChange) == null ? void 0 : _b2.call(_a3, context);
504
+ };
488
505
  const listeners = [
489
506
  ["select", handleSelect],
490
507
  ["change", handleChange],
@@ -496,14 +513,18 @@ const Select = forwardRef((props, ref) => {
496
513
  ["diagnostic", handleDiagnostic],
497
514
  ["dataRequestCancel", handleDataRequestCancel],
498
515
  ["dataProjection", handleDataProjection],
499
- ["dataLoadingConfigError", handleDataLoadingConfigError]
516
+ ["dataLoadingConfigError", handleDataLoadingConfigError],
517
+ ["emptyStateChange", handleEmptyStateChange]
500
518
  ];
501
519
  for (const [name, listener] of listeners) {
502
520
  element.addEventListener(name, listener);
503
521
  }
522
+ setEmptyStateContext(
523
+ ((_a2 = element.getEmptyStateContext) == null ? void 0 : _a2.call(element)) ?? null
524
+ );
504
525
  const dispose = () => {
505
- var _a2;
506
- if (((_a2 = activeListenersRef.current) == null ? void 0 : _a2.dispose) === dispose) {
526
+ var _a3;
527
+ if (((_a3 = activeListenersRef.current) == null ? void 0 : _a3.dispose) === dispose) {
507
528
  activeListenersRef.current = null;
508
529
  }
509
530
  for (const [name, listener] of listeners) {
@@ -620,7 +641,7 @@ const Select = forwardRef((props, ref) => {
620
641
  ...selectionConfig,
621
642
  mode: multiple ? "multi" : "single",
622
643
  maxSelections,
623
- removeButtonIcon,
644
+ removeButtonIcon: typeof removeButtonIcon === "string" ? removeButtonIcon : void 0,
624
645
  disabledOptionBehavior,
625
646
  showSelectedIndicator,
626
647
  toggleOnTriggerClick
@@ -773,7 +794,7 @@ const Select = forwardRef((props, ref) => {
773
794
  ...selectionConfig,
774
795
  mode: multiple ? "multi" : "single",
775
796
  maxSelections,
776
- removeButtonIcon,
797
+ removeButtonIcon: typeof removeButtonIcon === "string" ? removeButtonIcon : void 0,
777
798
  disabledOptionBehavior,
778
799
  showSelectedIndicator,
779
800
  toggleOnTriggerClick
@@ -850,6 +871,48 @@ const Select = forwardRef((props, ref) => {
850
871
  isElementUsable,
851
872
  applyInstanceConfig
852
873
  ]);
874
+ useEffect(() => {
875
+ const element = elementRef.current;
876
+ const shadow = element == null ? void 0 : element.shadowRoot;
877
+ if (!isElementUsable || !shouldPortalRemoveIcon || !shadow) {
878
+ setRemoveIconTargets((previous) => previous.length ? [] : previous);
879
+ return;
880
+ }
881
+ const observed = /* @__PURE__ */ new Set();
882
+ let currentTargets = [];
883
+ let disposed = false;
884
+ const observer = new MutationObserver(() => refresh());
885
+ const refresh = () => {
886
+ if (disposed) return;
887
+ const optionRoots = Array.from(shadow.querySelectorAll("select-option")).map((option) => option.shadowRoot).filter((root) => root !== null);
888
+ const roots = [shadow, ...optionRoots];
889
+ if (roots.length !== observed.size || roots.some((root) => !observed.has(root))) {
890
+ observer.disconnect();
891
+ observed.clear();
892
+ for (const root of roots) {
893
+ observer.observe(root, { childList: true, subtree: true });
894
+ observed.add(root);
895
+ }
896
+ }
897
+ const targets = [
898
+ ...Array.from(shadow.querySelectorAll('[part="chip-remove-icon"]')),
899
+ ...optionRoots.flatMap(
900
+ (root) => Array.from(root.querySelectorAll('[part="chip-remove-icon"]'))
901
+ )
902
+ ];
903
+ if (targets.length === currentTargets.length && targets.every((target, index) => target === currentTargets[index])) return;
904
+ for (const target of targets) {
905
+ if (!currentTargets.includes(target)) target.replaceChildren();
906
+ }
907
+ currentTargets = targets;
908
+ setRemoveIconTargets(targets);
909
+ };
910
+ refresh();
911
+ return () => {
912
+ disposed = true;
913
+ observer.disconnect();
914
+ };
915
+ }, [isElementUsable, shouldPortalRemoveIcon]);
853
916
  useEffect(() => {
854
917
  const element = elementRef.current;
855
918
  if (!element || !isElementUsable) return;
@@ -941,6 +1004,10 @@ const Select = forwardRef((props, ref) => {
941
1004
  var _a2;
942
1005
  return ((_a2 = elementRef.current) == null ? void 0 : _a2.getSelectedValues()) || [];
943
1006
  },
1007
+ getEmptyStateContext: () => {
1008
+ var _a2, _b2;
1009
+ return ((_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getEmptyStateContext) == null ? void 0 : _b2.call(_a2)) ?? null;
1010
+ },
944
1011
  setItems: (items2) => {
945
1012
  var _a2;
946
1013
  (_a2 = elementRef.current) == null ? void 0 : _a2.setItems(items2);
@@ -977,12 +1044,12 @@ const Select = forwardRef((props, ref) => {
977
1044
  applyInstanceConfig(element, config);
978
1045
  },
979
1046
  getDataLoadingState: () => {
980
- var _a2, _b2, _c;
1047
+ var _a2, _b2, _c2;
981
1048
  const snapshot = (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getDataLoadingState) == null ? void 0 : _b2.call(_a2);
982
1049
  if (snapshot) return snapshot;
983
1050
  return {
984
1051
  status: "idle",
985
- connected: Boolean((_c = elementRef.current) == null ? void 0 : _c.isConnected),
1052
+ connected: Boolean((_c2 = elementRef.current) == null ? void 0 : _c2.isConnected),
986
1053
  busy: false,
987
1054
  projectionPending: false,
988
1055
  queryGeneration: 0,
@@ -1052,6 +1119,7 @@ const Select = forwardRef((props, ref) => {
1052
1119
  onChange
1053
1120
  ]);
1054
1121
  const renderedAriaBusy = acceptedProtocol === "acknowledged" && !isUnusable ? composeBusyToken(consumerAriaBusy, loading) : consumerAriaBusy;
1122
+ const renderedEmptyState = emptyStateContext && emptyStateRenderer ? emptyStateRenderer(emptyStateContext) : null;
1055
1123
  return React.createElement(
1056
1124
  "enhanced-select",
1057
1125
  {
@@ -1071,6 +1139,35 @@ const Select = forwardRef((props, ref) => {
1071
1139
  "aria-hidden": "true"
1072
1140
  },
1073
1141
  arrowIcon
1142
+ ) : null,
1143
+ loadingIcon != null ? React.createElement(
1144
+ "span",
1145
+ {
1146
+ slot: "loading-icon",
1147
+ className: "smilodon-loading-icon",
1148
+ "aria-hidden": "true"
1149
+ },
1150
+ loadingIcon
1151
+ ) : null,
1152
+ loadingContent != null ? React.createElement(
1153
+ "div",
1154
+ {
1155
+ slot: "loading-content",
1156
+ className: "smilodon-loading-content"
1157
+ },
1158
+ loadingContent
1159
+ ) : null,
1160
+ renderedEmptyState != null && typeof renderedEmptyState !== "boolean" ? React.createElement(
1161
+ "div",
1162
+ { slot: "empty-state", className: "smilodon-empty-state" },
1163
+ renderedEmptyState
1164
+ ) : null,
1165
+ shouldPortalRemoveIcon ? removeIconTargets.map(
1166
+ (target, index) => createPortal(
1167
+ React.createElement("span", { "aria-hidden": "true" }, removeButtonIcon),
1168
+ target,
1169
+ index
1170
+ )
1074
1171
  ) : null
1075
1172
  );
1076
1173
  });