@smilodon/react 1.10.1 → 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/COMPLETE-GUIDE.md +69 -0
- package/README.md +160 -0
- package/dist/index.cjs +158 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +158 -41
- package/dist/index.js.map +1 -1
- package/dist/types/Select.d.ts +15 -3
- package/dist/types/Select.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +2 -2
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,
|
|
@@ -49,6 +50,10 @@ const Select = forwardRef((props, ref) => {
|
|
|
49
50
|
styles: stylesConfig,
|
|
50
51
|
config: coreConfig,
|
|
51
52
|
removeButtonIcon,
|
|
53
|
+
arrowIcon,
|
|
54
|
+
loadingIcon,
|
|
55
|
+
loadingContent,
|
|
56
|
+
emptyStateRenderer,
|
|
52
57
|
disabledOptionBehavior,
|
|
53
58
|
showSelectedIndicator = true,
|
|
54
59
|
toggleOnTriggerClick = true,
|
|
@@ -76,6 +81,7 @@ const Select = forwardRef((props, ref) => {
|
|
|
76
81
|
onDataRequestCancel,
|
|
77
82
|
onDataProjection,
|
|
78
83
|
onDataLoadingConfigError,
|
|
84
|
+
onEmptyStateChange,
|
|
79
85
|
trackingEnabled = false,
|
|
80
86
|
trackEvents = true,
|
|
81
87
|
trackStyling = true,
|
|
@@ -117,6 +123,7 @@ const Select = forwardRef((props, ref) => {
|
|
|
117
123
|
onDataRequestCancel,
|
|
118
124
|
onDataProjection,
|
|
119
125
|
onDataLoadingConfigError,
|
|
126
|
+
onEmptyStateChange,
|
|
120
127
|
isControlled,
|
|
121
128
|
multiple
|
|
122
129
|
});
|
|
@@ -132,6 +139,7 @@ const Select = forwardRef((props, ref) => {
|
|
|
132
139
|
onDataRequestCancel,
|
|
133
140
|
onDataProjection,
|
|
134
141
|
onDataLoadingConfigError,
|
|
142
|
+
onEmptyStateChange,
|
|
135
143
|
isControlled,
|
|
136
144
|
multiple
|
|
137
145
|
};
|
|
@@ -278,7 +286,10 @@ const Select = forwardRef((props, ref) => {
|
|
|
278
286
|
reactRendererCache.current.clear();
|
|
279
287
|
}, [!!optionRenderer, !!(customRenderer ?? renderItem), scheduleRootUnmount]);
|
|
280
288
|
const [isElementReady, setIsElementReady] = useState(false);
|
|
281
|
-
const
|
|
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";
|
|
282
293
|
const dataLoadingConfigured = (coreConfig == null ? void 0 : coreConfig.dataLoading) !== void 0;
|
|
283
294
|
const coreFacadeRequired = acknowledgedRequested || dataLoadingConfigured;
|
|
284
295
|
const isElementUsable = isElementReady && (!acknowledgedRequested || isAcknowledgedQualified) && (!coreFacadeRequired || isCoreModuleLoaded) && !isUnusable;
|
|
@@ -385,7 +396,7 @@ const Select = forwardRef((props, ref) => {
|
|
|
385
396
|
},
|
|
386
397
|
[markUnusable, syncHostBusy]
|
|
387
398
|
);
|
|
388
|
-
const requiresAcknowledgedQualification = acceptedProtocol === "acknowledged" || ((
|
|
399
|
+
const requiresAcknowledgedQualification = acceptedProtocol === "acknowledged" || ((_c = coreConfig == null ? void 0 : coreConfig.dataLoading) == null ? void 0 : _c.protocol) === "acknowledged";
|
|
389
400
|
const qualifyAcknowledgedElement = useCallback(() => {
|
|
390
401
|
const element = elementRef.current;
|
|
391
402
|
const module = coreModuleRef.current;
|
|
@@ -403,16 +414,17 @@ const Select = forwardRef((props, ref) => {
|
|
|
403
414
|
return true;
|
|
404
415
|
}, [markUnusable]);
|
|
405
416
|
const subscribeToCoreEvents = useCallback((element) => {
|
|
417
|
+
var _a2;
|
|
406
418
|
const currentSubscription = activeListenersRef.current;
|
|
407
419
|
if ((currentSubscription == null ? void 0 : currentSubscription.element) === element) return currentSubscription.dispose;
|
|
408
420
|
currentSubscription == null ? void 0 : currentSubscription.dispose();
|
|
409
421
|
const handleSelect = (event) => {
|
|
410
|
-
var
|
|
422
|
+
var _a3, _b2;
|
|
411
423
|
const detail = event.detail;
|
|
412
|
-
(_b2 = (
|
|
424
|
+
(_b2 = (_a3 = callbackStateRef.current).onSelect) == null ? void 0 : _b2.call(_a3, detail.item, detail.index);
|
|
413
425
|
};
|
|
414
426
|
const handleChange = (event) => {
|
|
415
|
-
var
|
|
427
|
+
var _a3;
|
|
416
428
|
const { selectedItems, selectedValues } = event.detail;
|
|
417
429
|
const callbacks = callbackStateRef.current;
|
|
418
430
|
const values = selectedValues;
|
|
@@ -421,19 +433,19 @@ const Select = forwardRef((props, ref) => {
|
|
|
421
433
|
if (!callbacks.isControlled) {
|
|
422
434
|
setInternalValue(callbacks.multiple ? values : nextSingleValue);
|
|
423
435
|
}
|
|
424
|
-
(
|
|
436
|
+
(_a3 = callbacks.onChange) == null ? void 0 : _a3.call(
|
|
425
437
|
callbacks,
|
|
426
438
|
callbacks.multiple ? values : nextSingleValue,
|
|
427
439
|
selectedItems
|
|
428
440
|
);
|
|
429
441
|
};
|
|
430
442
|
const handleOpen = () => {
|
|
431
|
-
var
|
|
432
|
-
return (_b2 = (
|
|
443
|
+
var _a3, _b2;
|
|
444
|
+
return (_b2 = (_a3 = callbackStateRef.current).onOpen) == null ? void 0 : _b2.call(_a3);
|
|
433
445
|
};
|
|
434
446
|
const handleClose = () => {
|
|
435
|
-
var
|
|
436
|
-
return (_b2 = (
|
|
447
|
+
var _a3, _b2;
|
|
448
|
+
return (_b2 = (_a3 = callbackStateRef.current).onClose) == null ? void 0 : _b2.call(_a3);
|
|
437
449
|
};
|
|
438
450
|
const handleSearch = (event) => {
|
|
439
451
|
const { query, results, count, request } = event.detail;
|
|
@@ -450,40 +462,46 @@ const Select = forwardRef((props, ref) => {
|
|
|
450
462
|
else callback(page);
|
|
451
463
|
};
|
|
452
464
|
const handleClear = (event) => {
|
|
453
|
-
var
|
|
465
|
+
var _a3, _b2;
|
|
454
466
|
const { clearedSelection, clearedSearch } = event.detail;
|
|
455
|
-
(_b2 = (
|
|
467
|
+
(_b2 = (_a3 = callbackStateRef.current).onClear) == null ? void 0 : _b2.call(_a3, { clearedSelection, clearedSearch });
|
|
456
468
|
};
|
|
457
469
|
const handleDiagnostic = (event) => {
|
|
458
|
-
var
|
|
459
|
-
(_b2 = (
|
|
460
|
-
|
|
470
|
+
var _a3, _b2;
|
|
471
|
+
(_b2 = (_a3 = callbackStateRef.current).onDiagnostic) == null ? void 0 : _b2.call(
|
|
472
|
+
_a3,
|
|
461
473
|
event.detail
|
|
462
474
|
);
|
|
463
475
|
};
|
|
464
476
|
const handleDataRequestCancel = (event) => {
|
|
465
|
-
var
|
|
466
|
-
(_b2 = (
|
|
467
|
-
|
|
477
|
+
var _a3, _b2;
|
|
478
|
+
(_b2 = (_a3 = callbackStateRef.current).onDataRequestCancel) == null ? void 0 : _b2.call(
|
|
479
|
+
_a3,
|
|
468
480
|
event.detail
|
|
469
481
|
);
|
|
470
482
|
};
|
|
471
483
|
const handleDataProjection = (event) => {
|
|
472
|
-
var
|
|
473
|
-
(_b2 = (
|
|
474
|
-
|
|
484
|
+
var _a3, _b2;
|
|
485
|
+
(_b2 = (_a3 = callbackStateRef.current).onDataProjection) == null ? void 0 : _b2.call(
|
|
486
|
+
_a3,
|
|
475
487
|
event.detail
|
|
476
488
|
);
|
|
477
489
|
};
|
|
478
490
|
const handleDataLoadingConfigError = (event) => {
|
|
479
|
-
var
|
|
491
|
+
var _a3, _b2;
|
|
480
492
|
const activeUpdate = configUpdateStackRef.current[configUpdateStackRef.current.length - 1];
|
|
481
493
|
if (activeUpdate) activeUpdate.sawConfigError = true;
|
|
482
|
-
(_b2 = (
|
|
483
|
-
|
|
494
|
+
(_b2 = (_a3 = callbackStateRef.current).onDataLoadingConfigError) == null ? void 0 : _b2.call(
|
|
495
|
+
_a3,
|
|
484
496
|
event.detail
|
|
485
497
|
);
|
|
486
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
|
+
};
|
|
487
505
|
const listeners = [
|
|
488
506
|
["select", handleSelect],
|
|
489
507
|
["change", handleChange],
|
|
@@ -495,14 +513,18 @@ const Select = forwardRef((props, ref) => {
|
|
|
495
513
|
["diagnostic", handleDiagnostic],
|
|
496
514
|
["dataRequestCancel", handleDataRequestCancel],
|
|
497
515
|
["dataProjection", handleDataProjection],
|
|
498
|
-
["dataLoadingConfigError", handleDataLoadingConfigError]
|
|
516
|
+
["dataLoadingConfigError", handleDataLoadingConfigError],
|
|
517
|
+
["emptyStateChange", handleEmptyStateChange]
|
|
499
518
|
];
|
|
500
519
|
for (const [name, listener] of listeners) {
|
|
501
520
|
element.addEventListener(name, listener);
|
|
502
521
|
}
|
|
522
|
+
setEmptyStateContext(
|
|
523
|
+
((_a2 = element.getEmptyStateContext) == null ? void 0 : _a2.call(element)) ?? null
|
|
524
|
+
);
|
|
503
525
|
const dispose = () => {
|
|
504
|
-
var
|
|
505
|
-
if (((
|
|
526
|
+
var _a3;
|
|
527
|
+
if (((_a3 = activeListenersRef.current) == null ? void 0 : _a3.dispose) === dispose) {
|
|
506
528
|
activeListenersRef.current = null;
|
|
507
529
|
}
|
|
508
530
|
for (const [name, listener] of listeners) {
|
|
@@ -619,7 +641,7 @@ const Select = forwardRef((props, ref) => {
|
|
|
619
641
|
...selectionConfig,
|
|
620
642
|
mode: multiple ? "multi" : "single",
|
|
621
643
|
maxSelections,
|
|
622
|
-
removeButtonIcon,
|
|
644
|
+
removeButtonIcon: typeof removeButtonIcon === "string" ? removeButtonIcon : void 0,
|
|
623
645
|
disabledOptionBehavior,
|
|
624
646
|
showSelectedIndicator,
|
|
625
647
|
toggleOnTriggerClick
|
|
@@ -772,7 +794,7 @@ const Select = forwardRef((props, ref) => {
|
|
|
772
794
|
...selectionConfig,
|
|
773
795
|
mode: multiple ? "multi" : "single",
|
|
774
796
|
maxSelections,
|
|
775
|
-
removeButtonIcon,
|
|
797
|
+
removeButtonIcon: typeof removeButtonIcon === "string" ? removeButtonIcon : void 0,
|
|
776
798
|
disabledOptionBehavior,
|
|
777
799
|
showSelectedIndicator,
|
|
778
800
|
toggleOnTriggerClick
|
|
@@ -849,6 +871,48 @@ const Select = forwardRef((props, ref) => {
|
|
|
849
871
|
isElementUsable,
|
|
850
872
|
applyInstanceConfig
|
|
851
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]);
|
|
852
916
|
useEffect(() => {
|
|
853
917
|
const element = elementRef.current;
|
|
854
918
|
if (!element || !isElementUsable) return;
|
|
@@ -916,7 +980,13 @@ const Select = forwardRef((props, ref) => {
|
|
|
916
980
|
useImperativeHandle(ref, () => ({
|
|
917
981
|
focus: () => {
|
|
918
982
|
var _a2;
|
|
919
|
-
|
|
983
|
+
const element = elementRef.current;
|
|
984
|
+
const input = (_a2 = element == null ? void 0 : element.shadowRoot) == null ? void 0 : _a2.querySelector("input");
|
|
985
|
+
if (input instanceof HTMLElement) {
|
|
986
|
+
input.focus();
|
|
987
|
+
} else {
|
|
988
|
+
element == null ? void 0 : element.focus();
|
|
989
|
+
}
|
|
920
990
|
},
|
|
921
991
|
open: () => {
|
|
922
992
|
var _a2;
|
|
@@ -934,6 +1004,10 @@ const Select = forwardRef((props, ref) => {
|
|
|
934
1004
|
var _a2;
|
|
935
1005
|
return ((_a2 = elementRef.current) == null ? void 0 : _a2.getSelectedValues()) || [];
|
|
936
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
|
+
},
|
|
937
1011
|
setItems: (items2) => {
|
|
938
1012
|
var _a2;
|
|
939
1013
|
(_a2 = elementRef.current) == null ? void 0 : _a2.setItems(items2);
|
|
@@ -970,12 +1044,12 @@ const Select = forwardRef((props, ref) => {
|
|
|
970
1044
|
applyInstanceConfig(element, config);
|
|
971
1045
|
},
|
|
972
1046
|
getDataLoadingState: () => {
|
|
973
|
-
var _a2, _b2,
|
|
1047
|
+
var _a2, _b2, _c2;
|
|
974
1048
|
const snapshot = (_b2 = (_a2 = elementRef.current) == null ? void 0 : _a2.getDataLoadingState) == null ? void 0 : _b2.call(_a2);
|
|
975
1049
|
if (snapshot) return snapshot;
|
|
976
1050
|
return {
|
|
977
1051
|
status: "idle",
|
|
978
|
-
connected: Boolean((
|
|
1052
|
+
connected: Boolean((_c2 = elementRef.current) == null ? void 0 : _c2.isConnected),
|
|
979
1053
|
busy: false,
|
|
980
1054
|
projectionPending: false,
|
|
981
1055
|
queryGeneration: 0,
|
|
@@ -1045,14 +1119,57 @@ const Select = forwardRef((props, ref) => {
|
|
|
1045
1119
|
onChange
|
|
1046
1120
|
]);
|
|
1047
1121
|
const renderedAriaBusy = acceptedProtocol === "acknowledged" && !isUnusable ? composeBusyToken(consumerAriaBusy, loading) : consumerAriaBusy;
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1122
|
+
const renderedEmptyState = emptyStateContext && emptyStateRenderer ? emptyStateRenderer(emptyStateContext) : null;
|
|
1123
|
+
return React.createElement(
|
|
1124
|
+
"enhanced-select",
|
|
1125
|
+
{
|
|
1126
|
+
ref: elementRef,
|
|
1127
|
+
...hostProps,
|
|
1128
|
+
"aria-busy": renderedAriaBusy,
|
|
1129
|
+
"data-custom-arrow": arrowIcon != null ? "true" : void 0,
|
|
1130
|
+
class: className,
|
|
1131
|
+
dir: direction,
|
|
1132
|
+
style
|
|
1133
|
+
},
|
|
1134
|
+
arrowIcon != null ? React.createElement(
|
|
1135
|
+
"span",
|
|
1136
|
+
{
|
|
1137
|
+
slot: "arrow",
|
|
1138
|
+
className: "smilodon-custom-arrow",
|
|
1139
|
+
"aria-hidden": "true"
|
|
1140
|
+
},
|
|
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
|
+
)
|
|
1171
|
+
) : null
|
|
1172
|
+
);
|
|
1056
1173
|
});
|
|
1057
1174
|
Select.displayName = "Select";
|
|
1058
1175
|
export {
|