Skip to content

Commit 623fd12

Browse files
committed
chore: create catalog filter sidebar state component
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent bbbdafc commit 623fd12

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { useCallback, useState } from 'react';
2+
import {
3+
CatalogFilterSidebarProps,
4+
FilterList,
5+
FilterValues,
6+
StyleProps
7+
} from './CatalogFilterSidebar';
8+
import FilterSection from './FilterSection';
9+
/**
10+
* @component CatalogFilterSidebarState
11+
* @description A functional component that manages the filter state.
12+
* @param {Array} lists - An array of filter sections and its options lists.
13+
* @param {Function} onApplyFilters - A function to apply the filters.
14+
* @param {Object} value - The selected filters.
15+
* @param {Object} styleProps - The style properties for the component.
16+
*/
17+
const CatalogFilterSidebarState: React.FC<{
18+
lists: FilterList[];
19+
onApplyFilters: CatalogFilterSidebarProps['setData'];
20+
value: FilterValues;
21+
styleProps: StyleProps;
22+
}> = ({ lists, onApplyFilters, value, styleProps }) => {
23+
// Generate initial state with all sections open by default
24+
const [openSections, setOpenSections] = useState<Record<string, boolean>>(() => {
25+
const initialOpenSections: Record<string, boolean> = {};
26+
lists.forEach((list) => {
27+
initialOpenSections[list.filterKey] = !!list.defaultOpen;
28+
});
29+
return initialOpenSections;
30+
});
31+
32+
/**
33+
* @function handleSectionToggle
34+
* @description Handles the section toggle event.
35+
* @param {string} filterKey - The name of the filter section.
36+
*/
37+
const handleSectionToggle = useCallback((filterKey: string) => {
38+
setOpenSections((prevOpenSections) => ({
39+
...prevOpenSections,
40+
[filterKey]: !prevOpenSections[filterKey]
41+
}));
42+
}, []);
43+
44+
/**
45+
* @function handleCheckboxChange
46+
* @description Handles the checkbox change event.
47+
* @param {string} filterKey - The name of the filter section.
48+
* @param {string} value - The value of the checkbox.
49+
* @param {boolean} checked - The checked state of the checkbox.
50+
*/
51+
const handleCheckboxChange = useCallback(
52+
(filterKey: string, value: string, checked: boolean) => {
53+
onApplyFilters((prevFilters) => {
54+
const updatedFilters = { ...prevFilters };
55+
const filterList = lists.find((list) => list.filterKey === filterKey);
56+
57+
// default is multi select
58+
if (filterList?.isMultiSelect !== false) {
59+
let currentValues = updatedFilters[filterKey] as string[] | undefined;
60+
61+
if (!Array.isArray(currentValues)) {
62+
currentValues = currentValues ? [currentValues as string] : []; // convert to array;
63+
}
64+
65+
updatedFilters[filterKey] = checked
66+
? [...currentValues, value]
67+
: currentValues.filter((item) => item !== value);
68+
} else {
69+
updatedFilters[filterKey] = checked ? value : '';
70+
}
71+
72+
return updatedFilters;
73+
});
74+
},
75+
[lists, onApplyFilters]
76+
);
77+
78+
return (
79+
<>
80+
{lists.map((list) => (
81+
<FilterSection
82+
key={list.filterKey}
83+
filterKey={list.filterKey}
84+
sectionDisplayName={list.sectionDisplayName}
85+
options={list.options}
86+
filters={value}
87+
openSections={openSections}
88+
onCheckboxChange={handleCheckboxChange}
89+
onSectionToggle={handleSectionToggle}
90+
styleProps={styleProps}
91+
/>
92+
))}
93+
</>
94+
);
95+
};
96+
97+
export default CatalogFilterSidebarState;

0 commit comments

Comments
 (0)