diff --git a/packages/filter/src/lib/components/quickFilter/QuickFilter.tsx b/packages/filter/src/lib/components/quickFilter/QuickFilter.tsx index 2e086b49..23878f62 100644 --- a/packages/filter/src/lib/components/quickFilter/QuickFilter.tsx +++ b/packages/filter/src/lib/components/quickFilter/QuickFilter.tsx @@ -42,11 +42,18 @@ export function QuickFilter(): JSX.Element { type QuickFilterReadyProps = { groups: IFilterGroup[]; }; + +type FilterGroups = { + groupName: string; + isVisible: boolean; +}; + const QuickFilterReady = ({ groups }: QuickFilterReadyProps) => { const { query, setUncheckedValues, uncheckedValues } = useFilterContext(); const [isFilterExpanded, setIsFilterExpanded] = useState(false); - const [allFilterGroups, setFilterOrder] = useState(groups.map((s) => s.name)); - const [visibleFilterGroups, setVisibleFilterGroups] = useState(groups.map((s) => s.name)); + const [allFilterGroups, setFilterOrder] = useState( + groups.map((s) => ({ groupName: s.name, isVisible: true })) + ); const [filterGroupOpen, setFilterGroupOpen] = useState(null); const handleExpandFilterGroup = (groupName: string) => filterGroupOpen === groupName ? setFilterGroupOpen(null) : setFilterGroupOpen(groupName); @@ -102,14 +109,7 @@ const QuickFilterReady = ({ groups }: QuickFilterReadyProps) => { )} - {isFilterExpanded && ( - - )} + {isFilterExpanded && } clearActiveFilters()}> s.values).flat().length === 0} /> @@ -123,7 +123,11 @@ const QuickFilterReady = ({ groups }: QuickFilterReadyProps) => { {isFilterExpanded && ( groups.find((s) => s.name === x)).filter(Boolean) as IFilterGroup[]} + groups={ + allFilterGroups + .map((x) => groups.find((s) => s.name === x.groupName && x.isVisible === true)) + .filter(Boolean) as IFilterGroup[] + } /> )} diff --git a/packages/filter/src/lib/components/toggleHideFilterPopover/ToggleHideFilterPopover.tsx b/packages/filter/src/lib/components/toggleHideFilterPopover/ToggleHideFilterPopover.tsx index 56c1ad26..b8dd2245 100644 --- a/packages/filter/src/lib/components/toggleHideFilterPopover/ToggleHideFilterPopover.tsx +++ b/packages/filter/src/lib/components/toggleHideFilterPopover/ToggleHideFilterPopover.tsx @@ -5,35 +5,46 @@ import { ReactSortable } from 'react-sortablejs'; import { StyledButton, StyledItemWrapper, StyledPopoverList } from './toggleHideFilterPopover.styles'; interface ShowHideFilterButtonProps { - allFilters: string[]; - visibleFilters: string[]; - setFilterOrder: (val: string[]) => void; - setVisibleFilters: (val: string[]) => void; + allFilters: FilterType[]; + setFilterOrder: (val: FilterType[]) => void; } -export const ToggleHideFilterPopover = ({ - setVisibleFilters, - setFilterOrder, - visibleFilters, - allFilters, -}: ShowHideFilterButtonProps): JSX.Element => { +type FilterType = { + groupName: string; + isVisible: boolean; +}; + +export const ToggleHideFilterPopover = ({ allFilters, setFilterOrder }: ShowHideFilterButtonProps): JSX.Element => { const [isOpen, setIsOpen] = useState(false); const ref = useRef(null); + const DraggableHandleSelector = 'globalDraggableHandle'; + + const listRef = useRef(allFilters.map((s) => ({ id: s.groupName, item: s.groupName }))); - const listRef = useRef(allFilters.map((s) => ({ id: s, item: s }))); + const updateListOrder = () => { + const updatedFilterOrder = listRef.current + .filter((refItem) => allFilters.some((filter) => filter.groupName === refItem.item)) + .map((refItem) => { + const correspondingFilter = allFilters.find((filter) => filter.groupName === refItem.item); + return { + groupName: refItem.item, + isVisible: correspondingFilter ? correspondingFilter.isVisible : true, + } as FilterType; + }); - const handleChange = (val: string) => { - if (visibleFilters.includes(val)) { - setVisibleFilters([...visibleFilters.filter((s) => s !== val)]); - } else { - setVisibleFilters([...visibleFilters, val]); - } + setFilterOrder(updatedFilterOrder); }; - const DraggableHandleSelector = 'globalDraggableHandle'; - const updateList = () => { - setVisibleFilters(listRef.current.map((s) => s.item).filter((s) => visibleFilters.includes(s))); - setFilterOrder(listRef.current.map((s) => s.item)); + const handleCheckboxClick = (val: string) => { + const updatedFilters = allFilters.map((filter) => { + if (filter.groupName === val) { + return { ...filter, isVisible: !filter.isVisible }; + } + + return filter; + }); + + setFilterOrder(updatedFilters); }; return ( @@ -59,15 +70,15 @@ export const ToggleHideFilterPopover = ({ setList={(e) => { listRef.current = e; }} - onEnd={updateList} + onEnd={updateListOrder} > {listRef.current.map(({ item }) => ( filter.groupName === item && filter.isVisible === true)} onChange={() => { - handleChange(item); + handleCheckboxClick(item); }} />
{item}