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

[DataGrid] Allow to control the indeterminate checkbox behavior #14247

Merged
merged 6 commits into from
Aug 29, 2024
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
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 @@ -181,6 +181,10 @@
},
"default": "false"
},
"indeterminateCheckboxBehavior": {
"type": { "name": "enum", "description": "'deselect'<br>&#124;&nbsp;'select'" },
"default": "\"select\""
},
"initialState": { "type": { "name": "object" } },
"isCellEditable": {
"type": { "name": "func" },
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 @@ -155,6 +155,10 @@
},
"default": "false"
},
"indeterminateCheckboxBehavior": {
"type": { "name": "enum", "description": "'deselect'<br>&#124;&nbsp;'select'" },
"default": "\"select\""
},
"initialState": { "type": { "name": "object" } },
"isCellEditable": {
"type": { "name": "func" },
Expand Down
4 changes: 4 additions & 0 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@
},
"default": "false"
},
"indeterminateCheckboxBehavior": {
"type": { "name": "enum", "description": "'deselect'<br>&#124;&nbsp;'select'" },
"default": "\"select\""
},
"initialState": { "type": { "name": "object" } },
"isCellEditable": {
"type": { "name": "func" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@
"ignoreValueFormatterDuringExport": {
"description": "If <code>true</code>, the Data Grid will not use <code>valueFormatter</code> when exporting to CSV or copying to clipboard. If an object is provided, you can choose to ignore the <code>valueFormatter</code> for CSV export or clipboard export."
},
"indeterminateCheckboxBehavior": {
"description": "If <code>select</code>, a group header checkbox in indeterminate state (like &quot;Select All&quot; checkbox) will select all the rows under it If <code>deselect</code>, it will deselect all the rows under it Works only if <code>checkboxSelection</code> is enabled."
},
"initialState": {
"description": "The initial state of the DataGridPremium. The data in it is set in the state on initialization but isn&#39;t controlled. If one of the data in <code>initialState</code> is also being controlled, then the control state wins."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
"ignoreValueFormatterDuringExport": {
"description": "If <code>true</code>, the Data Grid will not use <code>valueFormatter</code> when exporting to CSV or copying to clipboard. If an object is provided, you can choose to ignore the <code>valueFormatter</code> for CSV export or clipboard export."
},
"indeterminateCheckboxBehavior": {
"description": "If <code>select</code>, a group header checkbox in indeterminate state (like &quot;Select All&quot; checkbox) will select all the rows under it If <code>deselect</code>, it will deselect all the rows under it Works only if <code>checkboxSelection</code> is enabled."
},
"initialState": {
"description": "The initial state of the DataGridPro. The data in it will be set in the state on initialization but will not be controlled. If one of the data in <code>initialState</code> is also being controlled, then the control state wins."
},
Expand Down
3 changes: 3 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@
"ignoreValueFormatterDuringExport": {
"description": "If <code>true</code>, the Data Grid will not use <code>valueFormatter</code> when exporting to CSV or copying to clipboard. If an object is provided, you can choose to ignore the <code>valueFormatter</code> for CSV export or clipboard export."
},
"indeterminateCheckboxBehavior": {
"description": "If <code>select</code>, a group header checkbox in indeterminate state (like &quot;Select All&quot; checkbox) will select all the rows under it If <code>deselect</code>, it will deselect all the rows under it Works only if <code>checkboxSelection</code> is enabled."
},
"initialState": {
"description": "The initial state of the DataGrid. The data in it will be set in the state on initialization but will not be controlled. If one of the data in <code>initialState</code> is also being controlled, then the control state wins."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ DataGridPremiumRaw.propTypes = {
}),
PropTypes.bool,
]),
/**
* If `select`, a group header checkbox in indeterminate state (like "Select All" checkbox)
* will select all the rows under it
* If `deselect`, it will deselect all the rows under it
* Works only if `checkboxSelection` is enabled.
* @default "select"
*/
indeterminateCheckboxBehavior: PropTypes.oneOf(['deselect', 'select']),
/**
* The initial state of the DataGridPremium.
* The data in it is set in the state on initialization but isn't controlled.
Expand Down
8 changes: 8 additions & 0 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ DataGridProRaw.propTypes = {
}),
PropTypes.bool,
]),
/**
* If `select`, a group header checkbox in indeterminate state (like "Select All" checkbox)
* will select all the rows under it
* If `deselect`, it will deselect all the rows under it
* Works only if `checkboxSelection` is enabled.
* @default "select"
*/
indeterminateCheckboxBehavior: PropTypes.oneOf(['deselect', 'select']),
/**
* The initial state of the DataGridPro.
* The data in it will be set in the state on initialization but will not be controlled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('<DataGridPro /> - Row selection', () => {
initialState={{ pagination: { paginationModel: { pageSize: 2 } } }}
pagination
pageSizeOptions={[2]}
indeterminateCheckboxBehavior='deselect'
/>,
);
fireEvent.click(getCell(0, 0).querySelector('input')!);
Expand Down Expand Up @@ -176,6 +177,7 @@ describe('<DataGridPro /> - Row selection', () => {
pagination
checkboxSelectionVisibleOnly
pageSizeOptions={[2]}
indeterminateCheckboxBehavior='deselect'
/>,
);

Expand Down
8 changes: 8 additions & 0 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ DataGridRaw.propTypes = {
}),
PropTypes.bool,
]),
/**
* If `select`, a group header checkbox in indeterminate state (like "Select All" checkbox)
* will select all the rows under it
* If `deselect`, it will deselect all the rows under it
* Works only if `checkboxSelection` is enabled.
* @default "select"
*/
indeterminateCheckboxBehavior: PropTypes.oneOf(['deselect', 'select']),
/**
* The initial state of the DataGrid.
* The data in it will be set in the state on initialization but will not be controlled.
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid/src/DataGrid/useDataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const DATA_GRID_PROPS_DEFAULT_VALUES: DataGridPropsWithDefaultValues = {
hideFooterSelectedRowCount: false,
ignoreDiacritics: false,
ignoreValueFormatterDuringExport: false,
indeterminateCheckboxBehavior: 'select',
MBilalShafi marked this conversation as resolved.
Show resolved Hide resolved
keepColumnPositionIfDraggedOutside: false,
keepNonExistentRowsSelected: false,
loading: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ const GridHeaderCheckbox = React.forwardRef<HTMLButtonElement, GridColumnHeaderP
isChecked ? 'checkboxSelectionUnselectAllRows' : 'checkboxSelectionSelectAllRows',
);

const checked =
rootProps.indeterminateCheckboxBehavior === 'select'
? isChecked && !isIndeterminate
: isChecked;
MBilalShafi marked this conversation as resolved.
Show resolved Hide resolved

return (
<rootProps.slots.baseCheckbox
ref={ref}
indeterminate={isIndeterminate}
checked={isChecked}
checked={checked}
onChange={handleChange}
className={classes.root}
inputProps={{ 'aria-label': label }}
Expand Down
8 changes: 8 additions & 0 deletions packages/x-data-grid/src/models/props/DataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ export interface DataGridPropsWithDefaultValues<R extends GridValidRowModel = an
* @default false
*/
ignoreDiacritics: boolean;
/**
* If `select`, a group header checkbox in indeterminate state (like "Select All" checkbox)
* will select all the rows under it
* If `deselect`, it will deselect all the rows under it
* Works only if `checkboxSelection` is enabled.
* @default "select"
*/
indeterminateCheckboxBehavior: 'select' | 'deselect';
/**
* If `true`, the selection model will retain selected rows that do not exist.
* Useful when using server side pagination and row selections need to be retained
Expand Down
22 changes: 22 additions & 0 deletions packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,28 @@ describe('<DataGrid /> - Row selection', () => {
expect(grid('selectedRowCount')?.textContent).to.equal('1 row selected');
});
});

describe('prop: indeterminateCheckboxBehavior = "select"', () => {
it('should select all the rows when clicking on "Select All" checkbox in indeterminate state', () => {
render(<TestDataGridSelection checkboxSelection indeterminateCheckboxBehavior="select" />);
const selectAllCheckbox = screen.getByRole('checkbox', { name: 'Select all rows' });
fireEvent.click(screen.getAllByRole('checkbox', { name: /select row/i })[0]);
fireEvent.click(selectAllCheckbox);
expect(getSelectedRowIds()).to.deep.equal([0, 1, 2, 3]);
});
});

describe('prop: indeterminateCheckboxBehavior = "deselect"', () => {
it('should deselect all the rows when clicking on "Select All" checkbox in indeterminate state', () => {
render(
<TestDataGridSelection checkboxSelection indeterminateCheckboxBehavior="deselect" />,
);
const selectAllCheckbox = screen.getByRole('checkbox', { name: 'Select all rows' });
fireEvent.click(screen.getAllByRole('checkbox', { name: /select row/i })[0]);
fireEvent.click(selectAllCheckbox);
expect(getSelectedRowIds()).to.deep.equal([]);
});
});
});

describe('prop: checkboxSelection = true (multi selection), with keyboard events', () => {
Expand Down