@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 +62 -12
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +62 -12
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/molecules/multiple-select-filter/MultipleSelectFilter.js +49 -10
- package/src/components/molecules/multiple-select-filter/__private__/FilterButton.js +3 -1
- package/src/components/molecules/multiple-select-filter/__private__/util.js +13 -0
- package/src/components/atoms/.DS_Store +0 -0
package/package.json
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
59
|
-
|
|
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
|