-
Notifications
You must be signed in to change notification settings - Fork 0
/
useGridRowEditing.new.ts
695 lines (591 loc) · 24.1 KB
/
useGridRowEditing.new.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
import * as React from 'react';
import { unstable_useEventCallback as useEventCallback } from '@mui/utils';
import {
useGridApiEventHandler,
useGridApiOptionHandler,
GridSignature,
} from '../../utils/useGridApiEventHandler';
import { GridEventListener } from '../../../models/events/gridEventListener';
import {
GridEditModes,
GridRowModes,
GridEditingState,
GridEditCellProps,
GridEditRowProps,
} from '../../../models/gridEditRowModel';
import { GridApiCommunity } from '../../../models/api/gridApiCommunity';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import {
GridNewRowEditingApi,
GridNewEditingSharedApi,
GridStopRowEditModeParams,
GridStartRowEditModeParams,
GridRowModesModel,
GridRowModesModelProps,
} from '../../../models/api/gridEditingApi';
import { useGridApiMethod } from '../../utils/useGridApiMethod';
import { gridEditRowsStateSelector } from './gridEditRowsSelector';
import { GridRowId } from '../../../models/gridRows';
import { isPrintableKey } from '../../../utils/keyboardUtils';
import { gridColumnFieldsSelector } from '../columns/gridColumnsSelector';
import { GridCellParams } from '../../../models/params/gridCellParams';
import { buildWarning } from '../../../utils/warning';
import { gridRowsIdToIdLookupSelector } from '../rows/gridRowsSelector';
import { deepClone } from '../../../utils/utils';
import {
GridRowEditStopParams,
GridRowEditStartParams,
GridRowEditStopReasons,
GridRowEditStartReasons,
} from '../../../models/params/gridRowParams';
const missingOnProcessRowUpdateErrorWarning = buildWarning(
[
'MUI: A call to `processRowUpdate` threw an error which was not handled because `onProcessRowUpdateError` is missing.',
'To handle the error pass a callback to the `onProcessRowUpdateError` prop, e.g. `<DataGrid onProcessRowUpdateError={(error) => ...} />`.',
'For more detail, see http://mui.com/components/data-grid/editing/#persistence.',
],
'error',
);
export const useGridRowEditing = (
apiRef: React.MutableRefObject<GridApiCommunity>,
props: Pick<
DataGridProcessedProps,
| 'editMode'
| 'processRowUpdate'
| 'onRowEditStart'
| 'onRowEditStop'
| 'onProcessRowUpdateError'
| 'rowModesModel'
| 'onRowModesModelChange'
| 'signature'
| 'disableIgnoreModificationsIfProcessingProps'
>,
) => {
const [rowModesModel, setRowModesModel] = React.useState<GridRowModesModel>({});
const rowModesModelRef = React.useRef(rowModesModel);
const prevRowModesModel = React.useRef<GridRowModesModel>({});
const focusTimeout = React.useRef<any>(null);
const nextFocusedCell = React.useRef<GridCellParams | null>(null);
const {
processRowUpdate,
onProcessRowUpdateError,
rowModesModel: rowModesModelProp,
onRowModesModelChange,
signature,
} = props;
const runIfEditModeIsRow =
<Args extends any[]>(callback: (...args: Args) => void) =>
(...args: Args) => {
if (props.editMode === GridEditModes.Row) {
callback(...args);
}
};
const throwIfNotEditable = React.useCallback(
(id: GridRowId, field: string) => {
const params = apiRef.current.getCellParams(id, field);
if (!apiRef.current.isCellEditable(params)) {
throw new Error(`MUI: The cell with id=${id} and field=${field} is not editable.`);
}
},
[apiRef],
);
const throwIfNotInMode = React.useCallback(
(id: GridRowId, mode: GridRowModes) => {
if (apiRef.current.getRowMode(id) !== mode) {
throw new Error(`MUI: The row with id=${id} is not in ${mode} mode.`);
}
},
[apiRef],
);
const handleCellDoubleClick = React.useCallback<GridEventListener<'cellDoubleClick'>>(
(params, event) => {
if (!params.isEditable) {
return;
}
if (apiRef.current.getRowMode(params.id) === GridRowModes.Edit) {
return;
}
const rowParams = apiRef.current.getRowParams(params.id);
const newParams: GridRowEditStartParams = {
...rowParams,
field: params.field,
reason: GridRowEditStartReasons.cellDoubleClick,
};
apiRef.current.publishEvent('rowEditStart', newParams, event);
},
[apiRef],
);
const handleCellFocusIn = React.useCallback<GridEventListener<'cellFocusIn'>>((params) => {
nextFocusedCell.current = params;
}, []);
const handleCellFocusOut = React.useCallback<GridEventListener<'cellFocusOut'>>(
(params, event) => {
if (!params.isEditable) {
return;
}
if (apiRef.current.getRowMode(params.id) === GridRowModes.View) {
return;
}
// The mechanism to detect if we can stop editing a row is different from
// the cell editing. Instead of triggering it when clicking outside a cell,
// we must check if another cell in the same row was not clicked. To achieve
// that, first we keep track of all cells that gained focus. When a cell loses
// focus we check if the next cell that received focus is from a different row.
nextFocusedCell.current = null;
focusTimeout.current = setTimeout(() => {
focusTimeout.current = null;
if (nextFocusedCell.current?.id !== params.id) {
// The row might have been deleted during the click
if (!apiRef.current.getRow(params.id)) {
return;
}
// The row may already changed its mode
if (apiRef.current.getRowMode(params.id) === GridRowModes.View) {
return;
}
const rowParams = apiRef.current.getRowParams(params.id);
const newParams = {
...rowParams,
field: params.field,
reason: GridRowEditStopReasons.rowFocusOut,
};
apiRef.current.publishEvent('rowEditStop', newParams, event);
}
});
},
[apiRef],
);
React.useEffect(() => {
return () => {
clearTimeout(focusTimeout.current);
};
}, []);
const handleCellKeyDown = React.useCallback<GridEventListener<'cellKeyDown'>>(
(params, event) => {
if (params.cellMode === GridRowModes.Edit) {
// Wait until IME is settled for Asian languages like Japanese and Chinese
// TODO: `event.which` is depricated but this is a temporary workaround
if (event.which === 229) {
return;
}
let reason: GridRowEditStopReasons | undefined;
if (event.key === 'Escape') {
reason = GridRowEditStopReasons.escapeKeyDown;
} else if (event.key === 'Enter') {
reason = GridRowEditStopReasons.enterKeyDown;
} else if (event.key === 'Tab') {
const columnFields = gridColumnFieldsSelector(apiRef).filter((field) =>
apiRef.current.isCellEditable(apiRef.current.getCellParams(params.id, field)),
);
if (event.shiftKey) {
if (params.field === columnFields[0]) {
// Exit if user pressed Shift+Tab on the first field
reason = GridRowEditStopReasons.shiftTabKeyDown;
}
} else if (params.field === columnFields[columnFields.length - 1]) {
// Exit if user pressed Tab on the last field
reason = GridRowEditStopReasons.tabKeyDown;
}
if (reason) {
event.preventDefault(); // Prevent going to the next element in the tab sequence
}
}
if (reason) {
const rowParams = apiRef.current.getRowParams(params.id);
const newParams: GridRowEditStopParams = {
...rowParams,
reason,
field: params.field,
};
apiRef.current.publishEvent('rowEditStop', newParams, event);
}
} else if (params.isEditable) {
let reason: GridRowEditStartReasons | undefined;
if (event.key === ' ' && event.shiftKey) {
return; // Shift + Space is used to select the row
}
if (isPrintableKey(event)) {
reason = GridRowEditStartReasons.printableKeyDown;
} else if ((event.ctrlKey || event.metaKey) && event.key === 'v') {
reason = GridRowEditStartReasons.printableKeyDown;
} else if (event.key === 'Enter') {
reason = GridRowEditStartReasons.enterKeyDown;
} else if (event.key === 'Delete' || event.key === 'Backspace') {
// Delete on Windows, Backspace on macOS
reason = GridRowEditStartReasons.deleteKeyDown;
}
if (reason) {
const rowParams = apiRef.current.getRowParams(params.id);
const newParams: GridRowEditStartParams = { ...rowParams, field: params.field, reason };
apiRef.current.publishEvent('rowEditStart', newParams, event);
}
}
},
[apiRef],
);
const handleRowEditStart = React.useCallback<GridEventListener<'rowEditStart'>>(
(params) => {
const { id, field, reason } = params;
const startRowEditModeParams: GridStartRowEditModeParams = { id, fieldToFocus: field };
if (
reason === GridRowEditStartReasons.deleteKeyDown ||
reason === GridRowEditStartReasons.printableKeyDown
) {
startRowEditModeParams.deleteValue = !!field;
}
apiRef.current.startRowEditMode(startRowEditModeParams);
},
[apiRef],
);
const handleRowEditStop = React.useCallback<GridEventListener<'rowEditStop'>>(
(params) => {
const { id, reason, field } = params;
apiRef.current.unstable_runPendingEditCellValueMutation(id);
let cellToFocusAfter: GridStopRowEditModeParams['cellToFocusAfter'];
if (reason === GridRowEditStopReasons.enterKeyDown) {
cellToFocusAfter = 'below';
} else if (reason === GridRowEditStopReasons.tabKeyDown) {
cellToFocusAfter = 'right';
} else if (reason === GridRowEditStopReasons.shiftTabKeyDown) {
cellToFocusAfter = 'left';
}
let ignoreModifications = reason === 'escapeKeyDown';
const editingState = gridEditRowsStateSelector(apiRef.current.state);
if (!ignoreModifications && !props.disableIgnoreModificationsIfProcessingProps) {
// The user wants to stop editing the cell but we can't wait for the props to be processed.
// In this case, discard the modifications if any field is processing its props.
ignoreModifications = Object.values(editingState[id]).some((fieldProps) => {
return fieldProps.isProcessingProps;
});
}
apiRef.current.stopRowEditMode({ id, ignoreModifications, field, cellToFocusAfter });
},
[apiRef, props.disableIgnoreModificationsIfProcessingProps],
);
useGridApiEventHandler(apiRef, 'cellDoubleClick', runIfEditModeIsRow(handleCellDoubleClick));
useGridApiEventHandler(apiRef, 'cellFocusIn', runIfEditModeIsRow(handleCellFocusIn));
useGridApiEventHandler(apiRef, 'cellFocusOut', runIfEditModeIsRow(handleCellFocusOut));
useGridApiEventHandler(apiRef, 'cellKeyDown', runIfEditModeIsRow(handleCellKeyDown));
useGridApiEventHandler(apiRef, 'rowEditStart', runIfEditModeIsRow(handleRowEditStart));
useGridApiEventHandler(apiRef, 'rowEditStop', runIfEditModeIsRow(handleRowEditStop));
useGridApiOptionHandler(apiRef, 'rowEditStart', props.onRowEditStart);
useGridApiOptionHandler(apiRef, 'rowEditStop', props.onRowEditStop);
const getRowMode = React.useCallback<GridNewRowEditingApi['getRowMode']>(
(id) => {
if (props.editMode === GridEditModes.Cell) {
return GridRowModes.View;
}
const editingState = gridEditRowsStateSelector(apiRef.current.state);
const isEditing = editingState[id] && Object.keys(editingState[id]).length > 0;
return isEditing ? GridRowModes.Edit : GridRowModes.View;
},
[apiRef, props.editMode],
);
const updateRowModesModel = useEventCallback((newModel: GridRowModesModel) => {
const isNewModelDifferentFromProp = newModel !== props.rowModesModel;
if (onRowModesModelChange && isNewModelDifferentFromProp) {
const details = signature === GridSignature.DataGridPro ? { api: apiRef.current } : {};
onRowModesModelChange(newModel, details);
}
if (props.rowModesModel && isNewModelDifferentFromProp) {
return; // The prop always win
}
setRowModesModel(newModel);
rowModesModelRef.current = newModel;
apiRef.current.publishEvent('rowModesModelChange', newModel);
});
const updateRowInRowModesModel = React.useCallback(
(id: GridRowId, newProps: GridRowModesModelProps | null) => {
const newModel = { ...rowModesModelRef.current };
if (newProps !== null) {
newModel[id] = { ...newProps };
} else {
delete newModel[id];
}
updateRowModesModel(newModel);
},
[updateRowModesModel],
);
const updateOrDeleteRowState = React.useCallback(
(id: GridRowId, newProps: GridEditRowProps | null) => {
apiRef.current.setState((state) => {
const newEditingState: GridEditingState = { ...state.editRows };
if (newProps !== null) {
newEditingState[id] = newProps;
} else {
delete newEditingState[id];
}
return { ...state, editRows: newEditingState };
});
apiRef.current.forceUpdate();
},
[apiRef],
);
const updateOrDeleteFieldState = React.useCallback(
(id: GridRowId, field: string, newProps: GridEditCellProps | null) => {
apiRef.current.setState((state) => {
const newEditingState: GridEditingState = { ...state.editRows };
if (newProps !== null) {
newEditingState[id] = { ...newEditingState[id], [field]: { ...newProps } };
} else {
delete newEditingState[id][field];
if (Object.keys(newEditingState[id]).length === 0) {
delete newEditingState[id];
}
}
return { ...state, editRows: newEditingState };
});
apiRef.current.forceUpdate();
},
[apiRef],
);
const startRowEditMode = React.useCallback<GridNewRowEditingApi['startRowEditMode']>(
(params) => {
const { id, ...other } = params;
throwIfNotInMode(id, GridRowModes.View);
updateRowInRowModesModel(id, { mode: GridRowModes.Edit, ...other });
},
[throwIfNotInMode, updateRowInRowModesModel],
);
const updateStateToStartRowEditMode = useEventCallback<[GridStartRowEditModeParams], void>(
(params) => {
const { id, fieldToFocus, deleteValue } = params;
const columnFields = gridColumnFieldsSelector(apiRef);
const newProps = columnFields.reduce<Record<string, GridEditCellProps>>((acc, field) => {
const cellParams = apiRef.current.getCellParams(id, field);
if (!cellParams.isEditable) {
return acc;
}
const shouldDeleteValue = deleteValue && fieldToFocus === field;
acc[field] = {
value: shouldDeleteValue ? '' : apiRef.current.getCellValue(id, field),
error: false,
isProcessingProps: false,
};
return acc;
}, {});
updateOrDeleteRowState(id, newProps);
if (fieldToFocus) {
apiRef.current.setCellFocus(id, fieldToFocus);
}
},
) as GridNewRowEditingApi['stopRowEditMode'];
const stopRowEditMode = React.useCallback<GridNewRowEditingApi['stopRowEditMode']>(
(params) => {
const { id, ...other } = params;
throwIfNotInMode(id, GridRowModes.Edit);
updateRowInRowModesModel(id, { mode: GridRowModes.View, ...other });
},
[throwIfNotInMode, updateRowInRowModesModel],
);
const updateStateToStopRowEditMode = useEventCallback<[GridStopRowEditModeParams], void>(
(params) => {
const { id, ignoreModifications, field: focusedField, cellToFocusAfter = 'none' } = params;
apiRef.current.unstable_runPendingEditCellValueMutation(id);
const finishRowEditMode = () => {
if (cellToFocusAfter !== 'none' && focusedField) {
apiRef.current.unstable_moveFocusToRelativeCell(id, focusedField, cellToFocusAfter);
}
updateOrDeleteRowState(id, null);
updateRowInRowModesModel(id, null);
};
if (ignoreModifications) {
finishRowEditMode();
return;
}
const editingState = gridEditRowsStateSelector(apiRef.current.state);
const row = apiRef.current.getRow(id)!;
const isSomeFieldProcessingProps = Object.values(editingState[id]).some(
(fieldProps) => fieldProps.isProcessingProps,
);
if (isSomeFieldProcessingProps) {
prevRowModesModel.current[id].mode = GridRowModes.Edit;
return;
}
const hasSomeFieldWithError = Object.values(editingState[id]).some(
(fieldProps) => fieldProps.error,
);
if (hasSomeFieldWithError) {
prevRowModesModel.current[id].mode = GridRowModes.Edit;
// Revert the mode in the rowModesModel prop back to "edit"
updateRowInRowModesModel(id, { mode: GridRowModes.Edit });
return;
}
const rowUpdate = apiRef.current.unstable_getRowWithUpdatedValuesFromRowEditing(id);
if (processRowUpdate) {
const handleError = (errorThrown: any) => {
prevRowModesModel.current[id].mode = GridRowModes.Edit;
// Revert the mode in the rowModesModel prop back to "edit"
updateRowInRowModesModel(id, { mode: GridRowModes.Edit });
if (onProcessRowUpdateError) {
onProcessRowUpdateError(errorThrown);
} else {
missingOnProcessRowUpdateErrorWarning();
}
};
try {
Promise.resolve(processRowUpdate(rowUpdate, row))
.then((finalRowUpdate) => {
apiRef.current.updateRows([finalRowUpdate]);
finishRowEditMode();
})
.catch(handleError);
} catch (errorThrown) {
handleError(errorThrown);
}
} else {
apiRef.current.updateRows([rowUpdate]);
finishRowEditMode();
}
},
) as GridNewRowEditingApi['startRowEditMode'];
const setRowEditingEditCellValue = React.useCallback<
GridNewRowEditingApi['unstable_setRowEditingEditCellValue']
>(
(params) => {
const { id, field, value, debounceMs, unstable_skipValueParser: skipValueParser } = params;
throwIfNotEditable(id, field);
const column = apiRef.current.getColumn(field);
const row = apiRef.current.getRow(id)!;
let parsedValue = value;
if (column.valueParser && !skipValueParser) {
parsedValue = column.valueParser(value, apiRef.current.getCellParams(id, field));
}
let editingState = gridEditRowsStateSelector(apiRef.current.state);
let newProps: GridEditCellProps = {
...editingState[id][field],
value: parsedValue,
changeReason: debounceMs ? 'debouncedSetEditCellValue' : 'setEditCellValue',
};
if (!column.preProcessEditCellProps) {
updateOrDeleteFieldState(id, field, newProps);
}
return new Promise((resolve) => {
const promises: Promise<void>[] = [];
if (column.preProcessEditCellProps) {
const hasChanged = newProps.value !== editingState[id][field].value;
newProps = { ...newProps, isProcessingProps: true };
updateOrDeleteFieldState(id, field, newProps);
const { [field]: ignoredField, ...otherFieldsProps } = editingState[id];
const promise = Promise.resolve(
column.preProcessEditCellProps({
id,
row,
props: newProps,
hasChanged,
otherFieldsProps,
}),
).then((processedProps) => {
// Check again if the row is in edit mode because the user may have
// discarded the changes while the props were being processed.
if (apiRef.current.getRowMode(id) === GridRowModes.View) {
resolve(false);
return;
}
editingState = gridEditRowsStateSelector(apiRef.current.state);
processedProps = { ...processedProps, isProcessingProps: false };
// We don't reuse the value from the props pre-processing because when the
// promise resolves it may be already outdated. The only exception to this rule
// is when there's no pre-processing.
processedProps.value = column.preProcessEditCellProps
? editingState[id][field].value
: parsedValue;
updateOrDeleteFieldState(id, field, processedProps);
});
promises.push(promise);
}
Object.entries(editingState[id]).forEach(([thisField, fieldProps]) => {
if (thisField === field) {
return;
}
const fieldColumn = apiRef.current.getColumn(thisField);
if (!fieldColumn.preProcessEditCellProps) {
return;
}
fieldProps = { ...fieldProps, isProcessingProps: true };
updateOrDeleteFieldState(id, thisField, fieldProps);
editingState = gridEditRowsStateSelector(apiRef.current.state);
const { [thisField]: ignoredField, ...otherFieldsProps } = editingState[id];
const promise = Promise.resolve(
fieldColumn.preProcessEditCellProps({
id,
row,
props: fieldProps,
hasChanged: false,
otherFieldsProps,
}),
).then((processedProps) => {
// Check again if the row is in edit mode because the user may have
// discarded the changes while the props were being processed.
if (apiRef.current.getRowMode(id) === GridRowModes.View) {
resolve(false);
return;
}
processedProps = { ...processedProps, isProcessingProps: false };
updateOrDeleteFieldState(id, thisField, processedProps);
});
promises.push(promise);
});
Promise.all(promises).then(() => {
if (apiRef.current.getRowMode(id) === GridRowModes.Edit) {
editingState = gridEditRowsStateSelector(apiRef.current.state);
resolve(!editingState[id][field].error);
} else {
resolve(false);
}
});
});
},
[apiRef, throwIfNotEditable, updateOrDeleteFieldState],
);
const getRowWithUpdatedValuesFromRowEditing = React.useCallback<
GridNewRowEditingApi['unstable_getRowWithUpdatedValuesFromRowEditing']
>(
(id) => {
const editingState = gridEditRowsStateSelector(apiRef.current.state);
const row = apiRef.current.getRow(id);
let rowUpdate = { ...row };
Object.entries(editingState[id]).forEach(([field, fieldProps]) => {
const column = apiRef.current.getColumn(field);
if (column.valueSetter) {
rowUpdate = column.valueSetter({
value: fieldProps.value,
row: rowUpdate,
});
} else {
rowUpdate[field] = fieldProps.value;
}
});
return rowUpdate;
},
[apiRef],
);
const editingApi: Omit<GridNewRowEditingApi, keyof GridNewEditingSharedApi> = {
getRowMode,
startRowEditMode,
stopRowEditMode,
unstable_setRowEditingEditCellValue: setRowEditingEditCellValue,
unstable_getRowWithUpdatedValuesFromRowEditing: getRowWithUpdatedValuesFromRowEditing,
};
useGridApiMethod(apiRef, editingApi, 'EditingApi');
React.useEffect(() => {
if (rowModesModelProp) {
updateRowModesModel(rowModesModelProp);
}
}, [rowModesModelProp, updateRowModesModel]);
React.useEffect(() => {
const idToIdLookup = gridRowsIdToIdLookupSelector(apiRef);
// Update the ref here because updateStateToStopRowEditMode may change it later
const copyOfPrevRowModesModel = prevRowModesModel.current;
prevRowModesModel.current = deepClone(rowModesModel); // Do a deep-clone because the attributes might be changed later
Object.entries(rowModesModel).forEach(([id, params]) => {
const prevMode = copyOfPrevRowModesModel[id]?.mode || GridRowModes.View;
const originalId = idToIdLookup[id] ?? id;
if (params.mode === GridRowModes.Edit && prevMode === GridRowModes.View) {
updateStateToStartRowEditMode({ id: originalId, ...params });
} else if (params.mode === GridRowModes.View && prevMode === GridRowModes.Edit) {
updateStateToStopRowEditMode({ id: originalId, ...params });
}
});
}, [apiRef, rowModesModel, updateStateToStartRowEditMode, updateStateToStopRowEditMode]);
};