-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[DataGrid] Select all
checkbox click should select only filtered rows after filter
#1879
Conversation
select all
regression on checkbox selection
@@ -239,4 +242,10 @@ export const useGridSelection = (apiRef: GridApiRef): void => { | |||
}); | |||
forceUpdate(); | |||
}, [apiRef, setGridState, forceUpdate, isRowSelectable]); | |||
|
|||
React.useEffect(() => { | |||
if (totalSelectedRows > Object.keys(visibleRowIds).length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use visibleGridRowCountSelector
Also not sure about the logic here. What if we change rows to another set with the same length?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also not sure about the logic here. What if we change rows to another set with the same length?
So when filtering, is it possible that the rows filtered (visible) are equal to the total rows? Also this logic only runs when the rows are selected and then the filtering is applied, so the visible rows are less after filtering. Would there be a case when the rows are of same length after filtering? I don't think so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use visibleGridRowCountSelector
I need to pass visibleRowIds
below to selectRows
. Should I still select visibleRowCount
for count to replace Object.keys(visibleRowIds).length
? I think we should as the selector will be memoized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So when filtering, is it possible that the rows filtered (visible) are equal to the total rows?
Yes, if all rows satisfy the filter then all rows will be visible.
You can't just check if there're more selected rows than visible rows. The behavior becomes inconsistent if the size of the selection is smaller than the number of visible rows.
I think you need to do the same thing we do when the rows
prop is changed, but with visibleRowIds
.
@DanailH was working in #1864 to fix a bug around this part of the code too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So when filtering, is it possible that the rows filtered (visible) are equal to the total rows?
Yes, if all rows satisfy the filter then all rows will be visible.
Okay, so then the point is that this condition won't satisfy, right? And it would not select rows by deselecting others and selecting visible ones. It would work as it works now in master.
The behavior becomes inconsistent if the size of the selection is smaller than the number of visible rows.
Again, then this effect's condition won't satisfy and selecting rows won't run.
What this effect does?
I will like to give an example:
For grid with 200 rows.
The total selected rows are all 200, after filter the visible rows become 40. The dependencies change, this effect's condition satisfies and now the select rows will deselect others and select only 40. If you add another filter, the rows are now 37, it will select only 37. This is main issue of #1141.
I think you need to do the same thing we do when the
rows
prop is changed, but withvisibleRowIds
.@DanailH was working in #1864 to fix a bug around this part of the code too.
Does this effect #1141? Is this fix you are suggesting for the related issue of #1141? Are we on the main issue and the related comments issues I linked in description?
Also I would suggest once to look at whats happening currently and after the fix for the issues attached in the description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use visibleGridRowCountSelector
@dtassone I used this selector now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, then this effect's condition won't satisfy and selecting rows won't run.
Yeah, but you might have one selected row, adds another filter where this selected row doesn't satisfy it and the previous row will still be selected. If you select all, that means having a larger selection than visible rows, it unselects those rows that are not more visible. That's the inconsistency I noticed.
Does this effect #1141?
No
Is this fix you are suggesting for the related issue of #1141?
Yes, I would keep it simple. The selection should always contain only visible rows. In the future, we can add an option to allow to accumulate selection even when rows become invisible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but you might have one selected row, adds another filter where this selected row doesn't satisfy it and the previous row will still be selected.
Yeah, even I see this behaviour. It is in intermediate state? Yeah but the row selected is not visible.
The selection should always contain only visible rows
Are you referring with pagination, that it should select only the current page visible rows? #605? Maybe not.
packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
Outdated
Show resolved
Hide resolved
packages/grid/_modules_/grid/components/columnHeaders/GridColumnHeaderItem.tsx
Outdated
Show resolved
Hide resolved
packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure about the test coverage? I mean, we add one new test that is passing on HEAD. And a second test is weird. Why does it apply the filter twice? What about the second bug #1141 shouldn't we test it too (checkbox)?
My bad. The test was wrong. The test with the below change if updated will be correct. Pretty much what you did in #1919. This fails in master HEAD. I was setting the filter in initial mounting itself rather than setting it after the render. This leans me to now follow more TDD approach. Thanks. diff --git a/packages/grid/x-grid/src/tests/selection.XGrid.test.tsx b/packages/grid/x-grid/src/tests/selection.XGrid.test.tsx
index ac18ad6b..3bc07b97 100644
--- a/packages/grid/x-grid/src/tests/selection.XGrid.test.tsx
+++ b/packages/grid/x-grid/src/tests/selection.XGrid.test.tsx
@@ -129,20 +129,16 @@ describe('<XGrid /> - Selection', () => {
});
it('should select only filtered rows after filter is applied', () => {
- render(
- <Test
- checkboxSelection
- filterModel={{
- items: [
- {
- columnField: 'brand',
- operatorValue: 'contains',
- value: 'a',
- },
- ],
- }}
- />,
- );
+ render(<Test checkboxSelection />);
+ apiRef.current.setFilterModel({
+ items: [
+ {
+ columnField: 'brand',
+ operatorValue: 'contains',
+ value: 'a',
+ },
+ ],
+ });
const selectAll = screen.getByRole('checkbox', {
name: /select all rows checkbox/i,
});
It is testing the main issue of #1141
Yes we should which I missed. I I think it is The test adequacy is not good. I will rewrite them and add the missed one (checkbox).
Yes. All seemed related to me with |
@ZeeshanTamboli Ahhhhh. Now I get why you added the second filter in the test! Be very careful with what developers write as expected vs. actually. In 50% of the time, we overrule them. What we care about is hearing the pain point. So basically, if all the rows are selected without filters, and we add a filter, then, only the new visible rows are selected. Personally, this behavior has never crossed my mind. I didn't cover it in #1141 (comment). I would personally vote against it because it prevents the end-users to fine-tune their selection. But no strong point of view. cc @mui-org/x It seems that we should break down this PR in smaller chunks. It's overloaded.
|
I would vote against it in the native behavior, as ppl select a row then apply a filter to quickly find their next pick and select it. So I expect that they would want the selection to be aggregated |
Sounds good.
Should I rename this PR and have only memo fix in this? Change the description? Or should we close this PR and create a new one for just memo?
Yes. That's what the useEffect was doing in this PR. We can close this or only do memo fix here after rebase of #1919. We can have discussion in issue #1141. |
@ZeeshanTamboli Done, it's on HEAD #1919
If you could rebase this PR on HEAD, change the title, and focus on this problem: (and only), it would be 👌. One issue at a time.
We won't move forward with this behavior, please remove it from the PR, see: #1879 (comment). |
select all
regression on checkbox selectionSelect all
checkbox should select only filtered rows after filter
Select all
checkbox should select only filtered rows after filterSelect all
checkbox click should select only filtered rows after filter
return { | ||
...state, | ||
visibleRows: { | ||
...state.visibleRows, | ||
visibleRowsLookup, | ||
visibleRows: Object.entries(visibleRowsLookup) | ||
.filter(([, isVisible]) => isVisible) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic of the data grid is not consistent regarding this behavior. Sometimes, we spread extra props even if we override them all, sometimes, we don't. The change seems fair as a systematic approach. It will reduce the cost of future updates. cc @dtassone
Fix point 3. of #1141 (comment)
Closes #1863
Preview: https://deploy-preview-1879--material-ui-x.netlify.app/components/data-grid/