Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Preserves selected scopes when toggling between scope types #23475

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('FilterScope', () => {
expect(screen.getByRole('tree')).toBeInTheDocument();
expect(
document.querySelectorAll('.ant-tree-checkbox-checked').length,
).toBe(1);
).toBe(4);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const FilterScope: FC<FilterScopeProps> = ({
const [initialFilterScope] = useState(
filterScope || getDefaultScopeValue(chartId, initiallyExcludedCharts),
);
const [lastSpecificScope, setLastSpecificScope] =
useState(initialFilterScope);
const [initialScopingType] = useState(
isScopingAll(initialFilterScope, chartId)
? ScopingType.all
Expand All @@ -78,10 +80,13 @@ const FilterScope: FC<FilterScopeProps> = ({

const onUpdateFormValues = useCallback(
(formValues: any) => {
if (formScopingType === ScopingType.specific) {
setLastSpecificScope(formValues.scope);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer to use useRef for this case (since it won't need to re-render)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Done.

}
updateFormValues(formValues);
setHasScopeBeenModified(true);
},
[updateFormValues],
[formScopingType, updateFormValues],
);

const updateScopes = useCallback(() => {
Expand Down Expand Up @@ -113,12 +118,11 @@ const FilterScope: FC<FilterScopeProps> = ({
>
<Radio.Group
onChange={({ target: { value } }) => {
if (value === ScopingType.all) {
const scope = getDefaultScopeValue(chartId);
updateFormValues({
scope,
});
}
const scope =
value === ScopingType.all
? getDefaultScopeValue(chartId)
: lastSpecificScope;
updateFormValues({ scope });
setHasScopeBeenModified(true);
forceUpdate();
}}
Expand Down