@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thecb/components",
3
- "version": "11.1.8",
3
+ "version": "11.1.9",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -11,6 +11,7 @@ import FilterDropdown from "./__private__/FilterDropdown";
11
11
  import SearchBox from "./__private__/SearchBox";
12
12
  import FilterableList from "./__private__/FilterableList";
13
13
  import useOutsideClickHook from "../../../hooks/use-outside-click";
14
+ import { mergeOptions } from "./__private__/util";
14
15
 
15
16
  const MultipleSelectFilter = ({
16
17
  actions,
@@ -32,34 +33,71 @@ const MultipleSelectFilter = ({
32
33
  placeholder = "Search",
33
34
  searchable = true,
34
35
  themeValues,
35
- truncateBtnTextWidth = "15rem"
36
+ truncateBtnTextWidth = "15rem",
37
+ activeAppliedOptions
36
38
  }) => {
39
+ // State to manage whether the dropdown is open or closed
37
40
  const [opened, setOpened] = useState(false);
41
+
42
+ // State to manage the currently selected options
38
43
  const [selectedOptions, setSelectedOptions] = useState([]);
39
- const [appliedOptions, setAppliedOptions] = useState([]);
44
+
45
+ // State to manage the applied options, initialized with activeAppliedOptions or an empty array
46
+ const [appliedOptions, setAppliedOptions] = useState(
47
+ activeAppliedOptions || []
48
+ );
49
+
50
+ // State to track whether the user has interacted with the component
51
+ const [hasInteracted, setHasInteracted] = useState(false);
52
+
53
+ // Reference to keep track of the opened state across renders without causing re-renders
40
54
  const openedRef = useRef(opened);
41
55
 
42
- const handleOnClose = () => {
43
- if (openedRef.current) {
44
- setOpened(false);
45
- actions.fields.searchTerm.set("");
46
- }
47
- };
56
+ // Hook to detect clicks outside the component and close the dropdown
48
57
  const containerRef = useOutsideClickHook(() => handleOnClose());
58
+
59
+ // References to various elements within the component
49
60
  const dropdownRef = useRef(null);
50
61
  const filterButtonRef = useRef(null);
51
62
  const applyFilterButtonRef = useRef(null);
63
+
64
+ // IDs for accessibility and identification purposes
52
65
  const filterDropdownID = `${name}-filter-dropdown`;
53
66
  const listGroupID = `${name}-list-group`;
54
67
 
68
+ const handleOnClose = () => {
69
+ if (openedRef.current) {
70
+ setOpened(false);
71
+ actions.fields.searchTerm.set("");
72
+ }
73
+ };
74
+
55
75
  useEffect(() => {
56
76
  openedRef.current = opened;
57
77
  if (!opened) {
58
- onApply(selectedOptions);
59
- setAppliedOptions(selectedOptions);
78
+ if (hasInteracted) {
79
+ onApply(selectedOptions);
80
+ setAppliedOptions(selectedOptions);
81
+ }
82
+ } else {
83
+ setHasInteracted(true);
60
84
  }
61
85
  }, [opened]);
62
86
 
87
+ useEffect(() => {
88
+ // Update the applied options state with the current active applied options,
89
+ // or an empty array if activeAppliedOptions is undefined or null.
90
+ // This ensures that the current applied options are in sync with the parent component.
91
+ setAppliedOptions(activeAppliedOptions || []);
92
+
93
+ // Merge the selected options with the active applied options.
94
+ const mergedSelections = mergeOptions(
95
+ selectedOptions,
96
+ activeAppliedOptions
97
+ );
98
+ setSelectedOptions(mergedSelections);
99
+ }, [activeAppliedOptions]);
100
+
63
101
  useEffect(() => {
64
102
  const handleKeyDown = event => {
65
103
  if (event.key === "Escape") {
@@ -122,6 +160,7 @@ const MultipleSelectFilter = ({
122
160
  filterLabel={filterLabel}
123
161
  selectedOptions={selectedOptions}
124
162
  extraStyles={btnExtraStyles}
163
+ dataAppliedOptions={appliedOptions?.length}
125
164
  ></FilterButton>
126
165
  <FilterDropdown
127
166
  id={filterDropdownID}
@@ -21,7 +21,8 @@ const FilterButton = forwardRef(
21
21
  truncateBtnTextWidth,
22
22
  filterLabel,
23
23
  selectedOptions,
24
- extraStyles
24
+ extraStyles,
25
+ dataAppliedOptions
25
26
  },
26
27
  ref
27
28
  ) => {
@@ -48,6 +49,7 @@ const FilterButton = forwardRef(
48
49
  dataQa={`${name}-filter-button`}
49
50
  extraStyles={extraStyles}
50
51
  aria-label={`${filterLabel} Filter: ${btnTextFilterDescription} ${btnTextItemsDescription}`}
52
+ data-applied-options={dataAppliedOptions}
51
53
  contentOverride
52
54
  >
53
55
  {btnContentOverride ? (
@@ -29,3 +29,16 @@ export const selectOption = (option, selectedOptions, setSelectedOptions) => {
29
29
  setSelectedOptions(moreOptions);
30
30
  }
31
31
  };
32
+
33
+ export const mergeOptions = (selectedOptions, activeOptions) => {
34
+ if (!activeOptions.length) return selectedOptions;
35
+ if (!selectedOptions.length) return activeOptions;
36
+
37
+ const mergedOptions = [...selectedOptions];
38
+ activeOptions.forEach(activeOption => {
39
+ if (!mergedOptions.some(option => option.name === activeOption.name)) {
40
+ mergedOptions.push(activeOption);
41
+ }
42
+ });
43
+ return mergedOptions;
44
+ };
Binary file