@thecb/components 11.1.8 → 11.1.9

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.js CHANGED
@@ -50740,7 +50740,8 @@ var FilterButton$1 = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
50740
50740
  truncateBtnTextWidth = _ref.truncateBtnTextWidth,
50741
50741
  filterLabel = _ref.filterLabel,
50742
50742
  selectedOptions = _ref.selectedOptions,
50743
- extraStyles = _ref.extraStyles;
50743
+ extraStyles = _ref.extraStyles,
50744
+ dataAppliedOptions = _ref.dataAppliedOptions;
50744
50745
  var btnTextFilterDescription = selectedOptions !== null && selectedOptions !== void 0 && selectedOptions.length ? "".concat(filterLabel ? "".concat(filterLabel, ": ") : "").concat(selectedOptions[0].name) : "".concat(filterLabel ? filterLabel : "");
50745
50746
  var btnTextItemsDescription = selectedOptions !== null && selectedOptions !== void 0 && selectedOptions.length && (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.length) > 1 ? ", +".concat((selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.length) - 1, " More") : "";
50746
50747
  return /*#__PURE__*/React__default.createElement(StyledFilterButton, {
@@ -50758,6 +50759,7 @@ var FilterButton$1 = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
50758
50759
  dataQa: "".concat(name, "-filter-button"),
50759
50760
  extraStyles: extraStyles,
50760
50761
  "aria-label": "".concat(filterLabel, " Filter: ").concat(btnTextFilterDescription, " ").concat(btnTextItemsDescription),
50762
+ "data-applied-options": dataAppliedOptions,
50761
50763
  contentOverride: true
50762
50764
  }, btnContentOverride ? btnContentOverride : /*#__PURE__*/React__default.createElement(Center, {
50763
50765
  as: "span",
@@ -50942,6 +50944,19 @@ var selectOption = function selectOption(option, selectedOptions, setSelectedOpt
50942
50944
  setSelectedOptions(moreOptions);
50943
50945
  }
50944
50946
  };
50947
+ var mergeOptions = function mergeOptions(selectedOptions, activeOptions) {
50948
+ if (!activeOptions.length) return selectedOptions;
50949
+ if (!selectedOptions.length) return activeOptions;
50950
+ var mergedOptions = _toConsumableArray(selectedOptions);
50951
+ activeOptions.forEach(function (activeOption) {
50952
+ if (!mergedOptions.some(function (option) {
50953
+ return option.name === activeOption.name;
50954
+ })) {
50955
+ mergedOptions.push(activeOption);
50956
+ }
50957
+ });
50958
+ return mergedOptions;
50959
+ };
50945
50960
 
50946
50961
  var useKeyboardNavigation = function useKeyboardNavigation(_ref) {
50947
50962
  var options = _ref.options,
@@ -51142,41 +51157,75 @@ var MultipleSelectFilter = function MultipleSelectFilter(_ref) {
51142
51157
  searchable = _ref$searchable === void 0 ? true : _ref$searchable,
51143
51158
  themeValues = _ref.themeValues,
51144
51159
  _ref$truncateBtnTextW = _ref.truncateBtnTextWidth,
51145
- truncateBtnTextWidth = _ref$truncateBtnTextW === void 0 ? "15rem" : _ref$truncateBtnTextW;
51160
+ truncateBtnTextWidth = _ref$truncateBtnTextW === void 0 ? "15rem" : _ref$truncateBtnTextW,
51161
+ activeAppliedOptions = _ref.activeAppliedOptions;
51162
+ // State to manage whether the dropdown is open or closed
51146
51163
  var _useState = React.useState(false),
51147
51164
  _useState2 = _slicedToArray(_useState, 2),
51148
51165
  opened = _useState2[0],
51149
51166
  setOpened = _useState2[1];
51167
+
51168
+ // State to manage the currently selected options
51150
51169
  var _useState3 = React.useState([]),
51151
51170
  _useState4 = _slicedToArray(_useState3, 2),
51152
51171
  selectedOptions = _useState4[0],
51153
51172
  setSelectedOptions = _useState4[1];
51154
- var _useState5 = React.useState([]),
51173
+
51174
+ // State to manage the applied options, initialized with activeAppliedOptions or an empty array
51175
+ var _useState5 = React.useState(activeAppliedOptions || []),
51155
51176
  _useState6 = _slicedToArray(_useState5, 2),
51156
51177
  appliedOptions = _useState6[0],
51157
51178
  setAppliedOptions = _useState6[1];
51179
+
51180
+ // State to track whether the user has interacted with the component
51181
+ var _useState7 = React.useState(false),
51182
+ _useState8 = _slicedToArray(_useState7, 2),
51183
+ hasInteracted = _useState8[0],
51184
+ setHasInteracted = _useState8[1];
51185
+
51186
+ // Reference to keep track of the opened state across renders without causing re-renders
51158
51187
  var openedRef = React.useRef(opened);
51159
- var handleOnClose = function handleOnClose() {
51160
- if (openedRef.current) {
51161
- setOpened(false);
51162
- actions.fields.searchTerm.set("");
51163
- }
51164
- };
51188
+
51189
+ // Hook to detect clicks outside the component and close the dropdown
51165
51190
  var containerRef = useOutsideClickHook(function () {
51166
51191
  return handleOnClose();
51167
51192
  });
51193
+
51194
+ // References to various elements within the component
51168
51195
  var dropdownRef = React.useRef(null);
51169
51196
  var filterButtonRef = React.useRef(null);
51170
51197
  var applyFilterButtonRef = React.useRef(null);
51198
+
51199
+ // IDs for accessibility and identification purposes
51171
51200
  var filterDropdownID = "".concat(name, "-filter-dropdown");
51172
51201
  var listGroupID = "".concat(name, "-list-group");
51202
+ var handleOnClose = function handleOnClose() {
51203
+ if (openedRef.current) {
51204
+ setOpened(false);
51205
+ actions.fields.searchTerm.set("");
51206
+ }
51207
+ };
51173
51208
  React.useEffect(function () {
51174
51209
  openedRef.current = opened;
51175
51210
  if (!opened) {
51176
- onApply(selectedOptions);
51177
- setAppliedOptions(selectedOptions);
51211
+ if (hasInteracted) {
51212
+ onApply(selectedOptions);
51213
+ setAppliedOptions(selectedOptions);
51214
+ }
51215
+ } else {
51216
+ setHasInteracted(true);
51178
51217
  }
51179
51218
  }, [opened]);
51219
+ React.useEffect(function () {
51220
+ // Update the applied options state with the current active applied options,
51221
+ // or an empty array if activeAppliedOptions is undefined or null.
51222
+ // This ensures that the current applied options are in sync with the parent component.
51223
+ setAppliedOptions(activeAppliedOptions || []);
51224
+
51225
+ // Merge the selected options with the active applied options.
51226
+ var mergedSelections = mergeOptions(selectedOptions, activeAppliedOptions);
51227
+ setSelectedOptions(mergedSelections);
51228
+ }, [activeAppliedOptions]);
51180
51229
  React.useEffect(function () {
51181
51230
  var handleKeyDown = function handleKeyDown(event) {
51182
51231
  if (event.key === "Escape") {
@@ -51218,7 +51267,8 @@ var MultipleSelectFilter = function MultipleSelectFilter(_ref) {
51218
51267
  truncateBtnTextWidth: truncateBtnTextWidth,
51219
51268
  filterLabel: filterLabel,
51220
51269
  selectedOptions: selectedOptions,
51221
- extraStyles: btnExtraStyles
51270
+ extraStyles: btnExtraStyles,
51271
+ dataAppliedOptions: appliedOptions === null || appliedOptions === void 0 ? void 0 : appliedOptions.length
51222
51272
  }), /*#__PURE__*/React__default.createElement(FilterDropdown, {
51223
51273
  id: filterDropdownID,
51224
51274
  ref: dropdownRef,