Skip to content

Commit

Permalink
[DataGrid] Pass rowId param to processRowUpdate (#14821)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Cherniavskyi <[email protected]>
  • Loading branch information
k-rajat19 and cherniavskii authored Oct 7, 2024
1 parent 7bd05d2 commit 638f576
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion docs/data/data-grid/editing/editing.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ The editable cells have a green background for better visibility.

When the user performs an action to [stop editing](#stop-editing), the `processRowUpdate` callback is triggered.
Use it to send the new values to the server and save them into a database or other storage method.
The callback is called with two arguments:
The callback is called with three arguments:

1. The updated row with the new values returned by the [`valueSetter`](#value-parser-and-value-setter).
2. The original values of the row before editing.
3. An object with additional properties such as `rowId`.

Please note that the `processRowUpdate` must return the row object to update the Data Grid internal state.
The value returned is used later as an argument on a call to `apiRef.current.updateRows`.
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@
"processRowUpdate": {
"type": { "name": "func" },
"signature": {
"type": "function(newRow: R, oldRow: R) => Promise<R> | R",
"describedArgs": ["newRow", "oldRow"],
"type": "function(newRow: R, oldRow: R, params: { rowId: GridRowId }) => Promise<R> | R",
"describedArgs": ["newRow", "oldRow", "params"],
"returned": "Promise<R> | R"
}
},
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@
"processRowUpdate": {
"type": { "name": "func" },
"signature": {
"type": "function(newRow: R, oldRow: R) => Promise<R> | R",
"describedArgs": ["newRow", "oldRow"],
"type": "function(newRow: R, oldRow: R, params: { rowId: GridRowId }) => Promise<R> | R",
"describedArgs": ["newRow", "oldRow", "params"],
"returned": "Promise<R> | R"
}
},
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@
"processRowUpdate": {
"type": { "name": "func" },
"signature": {
"type": "function(newRow: R, oldRow: R) => Promise<R> | R",
"describedArgs": ["newRow", "oldRow"],
"type": "function(newRow: R, oldRow: R, params: { rowId: GridRowId }) => Promise<R> | R",
"describedArgs": ["newRow", "oldRow", "params"],
"returned": "Promise<R> | R"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@
"typeDescriptions": {
"newRow": "Row object with the new values.",
"oldRow": "Row object with the old values.",
"params": "Additional parameters.",
"Promise<R> | R": "The final values to update the row."
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@
"typeDescriptions": {
"newRow": "Row object with the new values.",
"oldRow": "Row object with the old values.",
"params": "Additional parameters.",
"Promise<R> | R": "The final values to update the row."
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@
"typeDescriptions": {
"newRow": "Row object with the new values.",
"oldRow": "Row object with the old values.",
"params": "Additional parameters.",
"Promise<R> | R": "The final values to update the row."
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ DataGridPremiumRaw.propTypes = {
* @template R
* @param {R} newRow Row object with the new values.
* @param {R} oldRow Row object with the old values.
* @param {{ rowId: GridRowId }} params Additional parameters.
* @returns {Promise<R> | R} The final values to update the row.
*/
processRowUpdate: PropTypes.func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class CellValueUpdater {

try {
const oldRow = apiRef.current.getRow(rowId);
const finalRowUpdate = await processRowUpdate(newRow, oldRow);
const finalRowUpdate = await processRowUpdate(newRow, oldRow, { rowId });
this.updateRow(finalRowUpdate);
} catch (error) {
handleError(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ describe('<DataGridPremium /> - Clipboard', () => {
expect(processRowUpdateSpy.args[0]).to.deep.equal([
{ id: 1, firstName: 'John', lastName: 'Doe' },
{ id: 1, firstName: 'Cersei', lastName: 'Lannister' },
{ rowId: '1' },
]);
});

Expand Down Expand Up @@ -725,14 +726,17 @@ describe('<DataGridPremium /> - Clipboard', () => {
[
{ id: 0, currencyPair: '12', price1M: '12' }, // new row
{ id: 0, currencyPair: 'USDGBP', price1M: 1 }, // old row
{ rowId: '0' }, // row id
],
[
{ id: 1, currencyPair: '12', price1M: '12' }, // new row
{ id: 1, currencyPair: 'USDEUR', price1M: 11 }, // old row
{ rowId: '1' }, // row id
],
[
{ id: 2, currencyPair: '12', price1M: '12' }, // new row
{ id: 2, currencyPair: 'GBPEUR', price1M: 21 }, // old row
{ rowId: '2' }, // row id
],
]);
});
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ DataGridProRaw.propTypes = {
* @template R
* @param {R} newRow Row object with the new values.
* @param {R} oldRow Row object with the old values.
* @param {{ rowId: GridRowId }} params Additional parameters.
* @returns {Promise<R> | R} The final values to update the row.
*/
processRowUpdate: PropTypes.func,
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ DataGridRaw.propTypes = {
* @template R
* @param {R} newRow Row object with the new values.
* @param {R} oldRow Row object with the old values.
* @param {{ rowId: GridRowId }} params Additional parameters.
* @returns {Promise<R> | R} The final values to update the row.
*/
processRowUpdate: PropTypes.func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export const useGridCellEditing = (

try {
const row = apiRef.current.getRow(id)!;
Promise.resolve(processRowUpdate(rowUpdate, row))
Promise.resolve(processRowUpdate(rowUpdate, row, { rowId: id }))
.then((finalRowUpdate) => {
apiRef.current.updateRows([finalRowUpdate]);
finishCellEditMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export const useGridRowEditing = (
};

try {
Promise.resolve(processRowUpdate(rowUpdate, row))
Promise.resolve(processRowUpdate(rowUpdate, row, { rowId: id }))
.then((finalRowUpdate) => {
apiRef.current.updateRows([finalRowUpdate]);
finishRowEditMode();
Expand Down
6 changes: 4 additions & 2 deletions packages/x-data-grid/src/models/props/DataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GridFeatureMode } from '../gridFeatureMode';
import { Logger } from '../logger';
import { GridSortDirection, GridSortModel } from '../gridSortModel';
import { GridSlotsComponent } from '../gridSlotsComponent';
import { GridRowIdGetter, GridRowsProp, GridValidRowModel } from '../gridRows';
import { GridRowId, GridRowIdGetter, GridRowsProp, GridValidRowModel } from '../gridRows';
import { GridEventListener } from '../events';
import { GridCallbackDetails, GridLocaleText } from '../api';
import { GridApiCommunity } from '../api/gridApiCommunity';
Expand Down Expand Up @@ -782,14 +782,16 @@ export interface DataGridPropsWithoutDefaultValue<R extends GridValidRowModel =
* For each feature, if the flag is not explicitly set to `true`, the feature will be fully disabled and any property / method call will not have any effect.
*/
experimentalFeatures?: Partial<GridExperimentalFeatures>;
// eslint-disable-next-line jsdoc/require-param
/**
* Callback called before updating a row with new values in the row and cell editing.
* @template R
* @param {R} newRow Row object with the new values.
* @param {R} oldRow Row object with the old values.
* @param {{ rowId: GridRowId }} params Additional parameters.
* @returns {Promise<R> | R} The final values to update the row.
*/
processRowUpdate?: (newRow: R, oldRow: R) => Promise<R> | R;
processRowUpdate?: (newRow: R, oldRow: R, params: { rowId: GridRowId }) => Promise<R> | R;
/**
* Callback called when `processRowUpdate` throws an error or rejects.
* @param {any} error The error thrown.
Expand Down

0 comments on commit 638f576

Please sign in to comment.