feat(defaultActive): set correct filter after clicking on clear#642
feat(defaultActive): set correct filter after clicking on clear#642karelhala wants to merge 2 commits into
Conversation
511316f to
de14dae
Compare
nicolethoen
left a comment
There was a problem hiding this comment.
This might benefit from one additional unit test that there is no issue if an invalid defaultActiveFilter is provided.
|
|
||
| useEffect(() => { | ||
| setActiveAttributeMenu(getDefaultTitle()); | ||
| }, [ filterItems, getDefaultTitle ]); |
There was a problem hiding this comment.
This useEffect will reset the active filter anytime filterItems changes. If consumers are passing dynamic children (e.g., showing/hiding
filters based on permissions or feature flags), the user's manual filter selection gets reset even if their currently selected filter is still in
the list.
For example:
<DataViewFilters>
<DataViewTextFilter filterId="name" title="Name" />
{showAdvanced && <DataViewTextFilter filterId="location" title="Location" />}
</DataViewFilters>User selects "Name" → toggles advanced filters → filterItems changes → resets back to default filter.
Consider moving this to mount-only or using an initialization flag so it doesn't fire on every children update.
There was a problem hiding this comment.
Good catch! Updated the useEffect to only reset when the current selection is empty or no longer available in the list:
useEffect(() => {
if (!activeAttributeMenu || !filterItems.some(item => item.title === activeAttributeMenu)) {
setActiveAttributeMenu(getDefaultTitle());
}
}, [ filterItems, getDefaultTitle ]);This way dynamic children (e.g., toggling filters via feature flags) won't reset the user's manual selection as long as their chosen filter is still present.
Description
When user clicks on clear filters the previously selected filter remains selected. This is unintuitive as we are basically clearing the whole filter section. This PR fixes it by activating first filter in the list. There's also an option to provide default filter which will be used when consumer provides it and user clears the filters.
Fixes 513