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

[DataGridPremium] Automatic parents and children selection #13757

Merged
merged 51 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
4c4ecf8
[DataGridPro] Add option to propagate row selection for nested rows
MBilalShafi Jul 8, 2024
899df0f
Merge branch 'master' into propagate-row-selection
MBilalShafi Jul 11, 2024
7fd93e7
Fix autogenerated parents not being affected by nested selection
MBilalShafi Jul 11, 2024
124ddbc
Update docs/data/data-grid/row-grouping/row-grouping.md
MBilalShafi Jul 11, 2024
3e9ad6e
Fix the id 0 not being picked properly for selection
MBilalShafi Jul 11, 2024
c581f04
Update demo to use multi-level
MBilalShafi Jul 11, 2024
4af394b
Reset selection on filtering
MBilalShafi Jul 16, 2024
e9146ce
Add tests
MBilalShafi Jul 16, 2024
361658b
Lint
MBilalShafi Jul 16, 2024
f3124b0
Prettier
MBilalShafi Jul 16, 2024
7e0f475
Tweak the performance a bit
MBilalShafi Jul 16, 2024
c4f4d0c
Lint
MBilalShafi Jul 16, 2024
fbd2d44
Michel's review addressed
MBilalShafi Jul 16, 2024
1f50a41
Minor docs update
MBilalShafi Jul 16, 2024
a328ea6
useDemoData: Temporarily allow higher row size for testing
MBilalShafi Jul 16, 2024
4f03788
Revert a328ea62893b99909a3573c585366b93c6e7a75a
MBilalShafi Jul 16, 2024
0c9fbaa
Make row group fetch use loop instead of recursion
MBilalShafi Jul 16, 2024
27904e4
Desc update
MBilalShafi Jul 18, 2024
42f5728
Romain's suggestion
MBilalShafi Jul 18, 2024
ff2bea1
Use specific selectors and update the row propagation updation logic
MBilalShafi Jul 18, 2024
8d2d059
Fix failing tests
MBilalShafi Jul 18, 2024
b6ca567
Updates
MBilalShafi Jul 18, 2024
a1cf083
Merge branch 'master' into propagate-row-selection
MBilalShafi Jul 24, 2024
5facfd3
Make the auto selection respect the isRowSelectable prop
MBilalShafi Jul 24, 2024
49ed4e0
Make select all checkbox in indeterminate state select all the rows
MBilalShafi Jul 24, 2024
fdab7f0
Merge branch 'master' into propagate-row-selection
MBilalShafi Jul 30, 2024
8e5238e
Introduce selectors with arguments
MBilalShafi Aug 1, 2024
a987418
Revert 8e5238ecfcacf2489899426aa46c19aa36c196ae
MBilalShafi Aug 17, 2024
5adfbec
Merge branch 'master' into propagate-row-selection
MBilalShafi Aug 17, 2024
1cc312d
Revert "Make select all checkbox in indeterminate state select all th…
MBilalShafi Aug 17, 2024
1babd7e
Merge branch 'master' into propagate-row-selection
MBilalShafi Sep 17, 2024
70feadc
propagateRowSelection -> rowSelectionPropagation
MBilalShafi Sep 18, 2024
d0f1c8b
Update
MBilalShafi Sep 24, 2024
c147230
Fix some tests
MBilalShafi Sep 24, 2024
9f789ac
Support indeterminateCheckboxAction prop on group header checkbox
MBilalShafi Sep 25, 2024
ac85050
Recompute selected parents on filtering
MBilalShafi Sep 25, 2024
1aac47d
Update signature of rowSelectionPropagation prop
MBilalShafi Sep 26, 2024
79e1b91
Handle faulty usecase with only auto select parents turned on
MBilalShafi Sep 26, 2024
87d9aef
Some small updates and docs improvement
MBilalShafi Sep 26, 2024
0ccad5b
Housekeeping
MBilalShafi Sep 26, 2024
9ffba58
Fix lint-check
MBilalShafi Sep 26, 2024
4d841c9
Merge branch 'master' into propagate-row-selection
MBilalShafi Sep 28, 2024
0427cf5
Cleanup
MBilalShafi Sep 28, 2024
4cb493a
Add tests
MBilalShafi Sep 28, 2024
c16e7f4
Improve check for id = 0
MBilalShafi Oct 1, 2024
3548c1f
Resolve a few comments
MBilalShafi Oct 2, 2024
201be14
Optimization
MBilalShafi Oct 2, 2024
b044d6f
Fix the failing test
MBilalShafi Oct 2, 2024
e5ee1b2
Update condition
MBilalShafi Oct 2, 2024
6ea605f
Improve documentation as per suggestions
MBilalShafi Oct 3, 2024
87f71d4
Update the docs
MBilalShafi Oct 4, 2024
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
72 changes: 72 additions & 0 deletions docs/data/data-grid/row-grouping/RowGroupingPropagateSelection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as React from 'react';
import {
DataGridPremium,
useGridApiRef,
useKeepGroupedColumnsHidden,
} from '@mui/x-data-grid-premium';
import { useMovieData } from '@mui/x-data-grid-generator';
import FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
import Stack from '@mui/material/Stack';

export default function RowGroupingPropagateSelection() {
const data = useMovieData();
const apiRef = useGridApiRef();
const [rowSelectionPropagation, setRowSelectionPropagation] = React.useState({
parents: true,
descendants: true,
});

const initialState = useKeepGroupedColumnsHidden({
apiRef,
initialState: {
rowGrouping: {
model: ['company', 'director'],
},
},
});

return (
<div style={{ width: '100%' }}>
<Stack direction="row" spacing={2}>
<FormControlLabel
control={
<Checkbox
checked={rowSelectionPropagation.descendants}
onChange={(event) =>
setRowSelectionPropagation((prev) => ({
...prev,
descendants: event.target.checked,
}))
}
/>
}
label="Auto select descendants"
/>
<FormControlLabel
control={
<Checkbox
checked={rowSelectionPropagation.parents}
onChange={(event) =>
setRowSelectionPropagation((prev) => ({
...prev,
parents: event.target.checked,
}))
}
/>
}
label="Auto select parents"
/>
</Stack>
<div style={{ height: 400 }}>
<DataGridPremium
{...data}
apiRef={apiRef}
initialState={initialState}
checkboxSelection
rowSelectionPropagation={rowSelectionPropagation}
/>
</div>
</div>
);
}
74 changes: 74 additions & 0 deletions docs/data/data-grid/row-grouping/RowGroupingPropagateSelection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as React from 'react';
import {
DataGridPremium,
useGridApiRef,
useKeepGroupedColumnsHidden,
GridRowSelectionPropagation,
} from '@mui/x-data-grid-premium';
import { useMovieData } from '@mui/x-data-grid-generator';
import FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
import Stack from '@mui/material/Stack';

export default function RowGroupingPropagateSelection() {
const data = useMovieData();
const apiRef = useGridApiRef();
const [rowSelectionPropagation, setRowSelectionPropagation] =
React.useState<GridRowSelectionPropagation>({
parents: true,
descendants: true,
});

const initialState = useKeepGroupedColumnsHidden({
apiRef,
initialState: {
rowGrouping: {
model: ['company', 'director'],
},
},
});

return (
<div style={{ width: '100%' }}>
<Stack direction="row" spacing={2}>
<FormControlLabel
control={
<Checkbox
checked={rowSelectionPropagation.descendants}
onChange={(event) =>
setRowSelectionPropagation((prev) => ({
...prev,
descendants: event.target.checked,
}))
}
/>
}
label="Auto select descendants"
/>
<FormControlLabel
control={
<Checkbox
checked={rowSelectionPropagation.parents}
onChange={(event) =>
setRowSelectionPropagation((prev) => ({
...prev,
parents: event.target.checked,
}))
}
/>
}
label="Auto select parents"
/>
</Stack>
<div style={{ height: 400 }}>
<DataGridPremium
{...data}
apiRef={apiRef}
initialState={initialState}
checkboxSelection
rowSelectionPropagation={rowSelectionPropagation}
/>
</div>
</div>
);
}
44 changes: 44 additions & 0 deletions docs/data/data-grid/row-grouping/row-grouping.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,50 @@ In the example below:
If you are dynamically switching the `leafField` or `mainGroupingCriteria`, the sorting and filtering models will not be cleaned up automatically, and the sorting/filtering will not be re-applied.
:::

## Automatic parents and children selection

By default, selecting a parent row does not select its children.
You can override this behavior by using the `rowSelectionPropagation` prop.

Here's how it's structured:

```ts
type GridRowSelectionPropagation = {
descendants?: boolean; // default: false
parents?: boolean; // default: false
};
```

When `rowSelectionPropagation.descendants` is set to `true`.

- Selecting a parent would auto-select all its filtered descendants.
- Deselecting a parent row would auto-deselect all its filtered descendants.

When `rowSelectionPropagation.parents` is set to `true`.

- Selecting all the filtered descendants of a parent would auto-select the parent.
- Deselecting a descendant of a selected parent would auto-deselect the parent.

The example below demonstrates the usage of the `rowSelectionPropagation` prop.

{{"demo": "RowGroupingPropagateSelection.js", "bg": "inline", "defaultCodeOpen": false}}
cherniavskii marked this conversation as resolved.
Show resolved Hide resolved

:::info
The row selection propagation also affects the "Select all" checkbox like any other group checkbox.
:::

:::info
The selected rows that do not pass the filtering criteria are automatically deselected when the filter is applied. Row selection propagation is not applied to the unfiltered rows.
:::

:::warning
If `props.disableMultipleRowSelection` is set to `true`, the row selection propagation doesn't apply.
:::

:::warning
Row selection propagation is a client-side feature and is not supported with the [server-side data source](/x/react-data-grid/server-side-data/).
:::

arminmeh marked this conversation as resolved.
Show resolved Hide resolved
## Get the rows in a group

You can use the `apiRef.current.getRowGroupChildren` method to get the id of all rows contained in a group.
Expand Down
4 changes: 4 additions & 0 deletions docs/data/data-grid/tree-data/tree-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ If you want to access the grouping column field, for instance, to use it with co

Same behavior as for the [Row grouping](/x/react-data-grid/row-grouping/#group-expansion).

## Automatic parents and children selection

Same behavior as for the [Row grouping](/x/react-data-grid/row-grouping/#automatic-parents-and-children-selection).

## Gaps in the tree

If some entries are missing to build the full tree, the `DataGridPro` will automatically create rows to fill those gaps.
Expand Down
4 changes: 4 additions & 0 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@
"description": "Array&lt;number<br>&#124;&nbsp;string&gt;<br>&#124;&nbsp;number<br>&#124;&nbsp;string"
}
},
"rowSelectionPropagation": {
"type": { "name": "shape", "description": "{ descendants?: bool, parents?: bool }" },
"default": "{ parents: false, descendants: false }"
},
"rowsLoadingMode": {
"type": { "name": "enum", "description": "'client'<br>&#124;&nbsp;'server'" }
},
Expand Down
4 changes: 4 additions & 0 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@
"description": "Array&lt;number<br>&#124;&nbsp;string&gt;<br>&#124;&nbsp;number<br>&#124;&nbsp;string"
}
},
"rowSelectionPropagation": {
"type": { "name": "shape", "description": "{ descendants?: bool, parents?: bool }" },
"default": "{ parents: false, descendants: false }"
},
"rowsLoadingMode": {
"type": { "name": "enum", "description": "'client'<br>&#124;&nbsp;'server'" }
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"groupingColDef": { "description": "The grouping column used by the tree data." },
"headerFilterHeight": { "description": "Override the height of the header filters." },
"headerFilters": {
"description": "If <code>true</code>, enables the data grid filtering on header feature."
"description": "If <code>true</code>, the header filters feature is enabled."
},
"hideFooter": { "description": "If <code>true</code>, the footer component is hidden." },
"hideFooterPagination": {
Expand Down Expand Up @@ -610,6 +610,9 @@
"rows": { "description": "Set of rows of type GridRowsProp." },
"rowSelection": { "description": "If <code>false</code>, the row selection mode is disabled." },
"rowSelectionModel": { "description": "Sets the row selection model of the Data Grid." },
"rowSelectionPropagation": {
"description": "When <code>rowSelectionPropagation.descendants</code> is set to <code>true</code>. - Selecting a parent will auto-select all its filtered descendants. - Deselecting a parent will auto-deselect all its filtered descendants.<br>When <code>rowSelectionPropagation.parents=true</code> - Selecting all descendants of a parent would auto-select it. - Deselecting a descendant of a selected parent would deselect the parent.<br>Works with tree data and row grouping on the client-side only."
},
"rowsLoadingMode": {
"description": "Loading rows can be processed on the server or client-side. Set it to &#39;client&#39; if you would like enable infnite loading. Set it to &#39;server&#39; if you would like to enable lazy loading. * @default &quot;client&quot;"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
"groupingColDef": { "description": "The grouping column used by the tree data." },
"headerFilterHeight": { "description": "Override the height of the header filters." },
"headerFilters": {
"description": "If <code>true</code>, enables the data grid filtering on header feature."
"description": "If <code>true</code>, the header filters feature is enabled."
},
"hideFooter": { "description": "If <code>true</code>, the footer component is hidden." },
"hideFooterPagination": {
Expand Down Expand Up @@ -552,6 +552,9 @@
"rows": { "description": "Set of rows of type GridRowsProp." },
"rowSelection": { "description": "If <code>false</code>, the row selection mode is disabled." },
"rowSelectionModel": { "description": "Sets the row selection model of the Data Grid." },
"rowSelectionPropagation": {
"description": "When <code>rowSelectionPropagation.descendants</code> is set to <code>true</code>. - Selecting a parent will auto-select all its filtered descendants. - Deselecting a parent will auto-deselect all its filtered descendants.<br>When <code>rowSelectionPropagation.parents=true</code> - Selecting all descendants of a parent would auto-select it. - Deselecting a descendant of a selected parent would deselect the parent.<br>Works with tree data and row grouping on the client-side only."
},
"rowsLoadingMode": {
"description": "Loading rows can be processed on the server or client-side. Set it to &#39;client&#39; if you would like enable infnite loading. Set it to &#39;server&#39; if you would like to enable lazy loading. * @default &quot;client&quot;"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ DataGridPremiumRaw.propTypes = {
*/
headerFilterHeight: PropTypes.number,
/**
* If `true`, enables the data grid filtering on header feature.
* If `true`, the header filters feature is enabled.
* @default false
*/
headerFilters: PropTypes.bool,
Expand Down Expand Up @@ -985,6 +985,22 @@ DataGridPremiumRaw.propTypes = {
PropTypes.number,
PropTypes.string,
]),
/**
* When `rowSelectionPropagation.descendants` is set to `true`.
* - Selecting a parent will auto-select all its filtered descendants.
* - Deselecting a parent will auto-deselect all its filtered descendants.
*
* When `rowSelectionPropagation.parents=true`
* - Selecting all descendants of a parent would auto-select it.
* - Deselecting a descendant of a selected parent would deselect the parent.
*
* Works with tree data and row grouping on the client-side only.
* @default { parents: false, descendants: false }
*/
rowSelectionPropagation: PropTypes.shape({
descendants: PropTypes.bool,
parents: PropTypes.bool,
}),
/**
* Loading rows can be processed on the server or client-side.
* Set it to 'client' if you would like enable infnite loading.
Expand Down
Loading