-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finos#1074 fix state updates issue in useFilterBar hook (finos#1135)
- fixes it by removing the use of useEffect to listen for changes in filters and active indices and then call onApplyFilter. Now I've made the calls to onApplyFilter explicit making it easier to maintain and read. - with this change I've also fixed a bug in filter deletion: Cause: we used to delete an active filter's index from active filter indices array without adjusting the rest of the indices. Required adjustment: if a filter gets deleted from filters array then the index of all the filters on the right are decremented by 1. But we weren't making this adjustment with active indices array. - and have added component tests to prevent this filter deletion bug from being re-introduced.
- Loading branch information
1 parent
b1fe998
commit 190d1bc
Showing
6 changed files
with
236 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
vuu-ui/packages/vuu-filters/src/filter-bar/useFilterState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useCallback, useState } from "react"; | ||
import { Filter } from "@finos/vuu-filter-types"; | ||
import { useControlled } from "@finos/vuu-ui-controls"; | ||
|
||
export interface FilterStateHookProps { | ||
activeFilterIndex: number[]; | ||
applyFilter: (f?: Filter) => void; | ||
defaultFilters?: Filter[]; | ||
filters?: Filter[]; | ||
} | ||
|
||
export function useFilterState({ | ||
activeFilterIndex: activeFilterIdexProp, | ||
applyFilter, | ||
defaultFilters, | ||
filters: filtersProp, | ||
}: FilterStateHookProps) { | ||
const [filters, setFilters] = useControlled<Filter[]>({ | ||
controlled: filtersProp, | ||
default: defaultFilters ?? [], | ||
name: "useFilters", | ||
state: "Filters", | ||
}); | ||
|
||
const [activeIndices, setActiveIndices] = | ||
useState<number[]>(activeFilterIdexProp); | ||
|
||
const onApplyFilter = useCallback( | ||
({ activeIndices, filters }: FilterState) => { | ||
if (activeIndices.length > 0) { | ||
const activeFilters = activeIndices.map((i) => filters[i]); | ||
if (activeFilters.length === 1) { | ||
const [filter] = activeFilters; | ||
applyFilter(filter); | ||
} else { | ||
applyFilter({ op: "and", filters: activeFilters }); | ||
} | ||
} else { | ||
applyFilter(); | ||
} | ||
}, | ||
[applyFilter] | ||
); | ||
|
||
const onFilterStateChange = useCallback( | ||
({ filters, activeIndices }: FilterState) => { | ||
setFilters(filters); | ||
setActiveIndices(activeIndices); | ||
onApplyFilter({ filters, activeIndices }); | ||
}, | ||
[onApplyFilter] | ||
); | ||
|
||
const handleActiveIndicesChange = useCallback( | ||
(indices: number[]) => | ||
onFilterStateChange({ filters, activeIndices: indices }), | ||
[filters, onFilterStateChange] | ||
); | ||
|
||
return { | ||
filterState: { activeIndices, filters }, | ||
onActiveIndicesChange: handleActiveIndicesChange, | ||
onFilterStateChange, | ||
}; | ||
} | ||
|
||
type FilterState = { filters: Filter[]; activeIndices: number[] }; |
Oops, something went wrong.