@mui/x-data-grid 6.0.0-alpha.2 → 6.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/CHANGELOG.md +231 -12
  2. package/DataGrid/DataGrid.js +0 -20
  3. package/DataGrid/useDataGridComponent.js +2 -6
  4. package/colDef/gridNumericOperators.d.ts +1 -1
  5. package/colDef/gridSingleSelectOperators.d.ts +1 -1
  6. package/colDef/gridStringOperators.d.ts +1 -1
  7. package/components/cell/GridEditInputCell.js +3 -8
  8. package/components/cell/GridEditSingleSelectCell.js +6 -38
  9. package/components/panel/filterPanel/GridFilterForm.d.ts +12 -0
  10. package/components/panel/filterPanel/GridFilterForm.js +29 -7
  11. package/components/panel/filterPanel/GridFilterPanel.d.ts +12 -0
  12. package/components/panel/filterPanel/GridFilterPanel.js +64 -19
  13. package/hooks/core/pipeProcessing/useGridRegisterPipeApplier.d.ts +1 -1
  14. package/hooks/core/pipeProcessing/useGridRegisterPipeProcessor.d.ts +1 -1
  15. package/hooks/core/strategyProcessing/useGridRegisterStrategyProcessor.d.ts +1 -1
  16. package/hooks/core/useGridInitialization.d.ts +1 -1
  17. package/hooks/core/useGridStateInitialization.d.ts +1 -1
  18. package/hooks/features/columnHeaders/useGridColumnHeaders.d.ts +1 -1
  19. package/hooks/features/{editRows/gridEditRowsSelector.d.ts → editing/gridEditingSelectors.d.ts} +0 -0
  20. package/hooks/features/{editRows/gridEditRowsSelector.js → editing/gridEditingSelectors.js} +0 -0
  21. package/hooks/features/editing/index.d.ts +1 -0
  22. package/hooks/features/editing/index.js +1 -0
  23. package/hooks/features/{editRows/useGridCellEditing.new.d.ts → editing/useGridCellEditing.d.ts} +0 -0
  24. package/hooks/features/{editRows/useGridCellEditing.new.js → editing/useGridCellEditing.js} +21 -6
  25. package/hooks/features/{editRows/useGridEditing.new.d.ts → editing/useGridEditing.d.ts} +0 -0
  26. package/hooks/features/{editRows/useGridEditing.new.js → editing/useGridEditing.js} +3 -3
  27. package/hooks/features/{editRows/useGridRowEditing.new.d.ts → editing/useGridRowEditing.d.ts} +0 -0
  28. package/hooks/features/{editRows/useGridRowEditing.new.js → editing/useGridRowEditing.js} +20 -6
  29. package/hooks/features/index.d.ts +1 -1
  30. package/hooks/features/index.js +1 -1
  31. package/hooks/features/virtualization/useGridVirtualScroller.d.ts +1 -1
  32. package/hooks/features/virtualization/useGridVirtualScroller.js +1 -1
  33. package/hooks/utils/useGridApi.d.ts +1 -1
  34. package/hooks/utils/useGridApiEventHandler.d.ts +2 -2
  35. package/hooks/utils/useGridApiRef.d.ts +1 -1
  36. package/hooks/utils/useGridInitializeState.d.ts +1 -1
  37. package/hooks/utils/useGridNativeEventListener.d.ts +1 -1
  38. package/hooks/utils/useGridSelector.d.ts +1 -1
  39. package/hooks/utils/useGridState.d.ts +1 -1
  40. package/hooks/utils/useGridVisibleRows.d.ts +2 -2
  41. package/index.js +1 -1
  42. package/internals/index.d.ts +1 -2
  43. package/internals/index.js +1 -2
  44. package/legacy/DataGrid/DataGrid.js +0 -20
  45. package/legacy/DataGrid/useDataGridComponent.js +2 -6
  46. package/legacy/components/cell/GridEditInputCell.js +3 -8
  47. package/legacy/components/cell/GridEditSingleSelectCell.js +6 -52
  48. package/legacy/components/panel/filterPanel/GridFilterForm.js +31 -7
  49. package/legacy/components/panel/filterPanel/GridFilterPanel.js +67 -18
  50. package/legacy/hooks/features/{editRows/gridEditRowsSelector.js → editing/gridEditingSelectors.js} +0 -0
  51. package/legacy/hooks/features/editing/index.js +1 -0
  52. package/legacy/hooks/features/{editRows/useGridCellEditing.new.js → editing/useGridCellEditing.js} +21 -6
  53. package/legacy/hooks/features/{editRows/useGridEditing.new.js → editing/useGridEditing.js} +3 -3
  54. package/legacy/hooks/features/{editRows/useGridRowEditing.new.js → editing/useGridRowEditing.js} +20 -6
  55. package/legacy/hooks/features/index.js +1 -1
  56. package/legacy/hooks/features/virtualization/useGridVirtualScroller.js +1 -1
  57. package/legacy/index.js +1 -1
  58. package/legacy/internals/index.js +1 -2
  59. package/legacy/models/params/gridEditCellParams.js +0 -4
  60. package/models/api/gridApiCommon.d.ts +2 -5
  61. package/models/api/gridApiCommunity.d.ts +1 -7
  62. package/models/api/gridEditingApi.d.ts +36 -121
  63. package/models/api/index.d.ts +1 -1
  64. package/models/events/gridEventLookup.d.ts +1 -15
  65. package/models/params/gridEditCellParams.d.ts +4 -17
  66. package/models/params/gridEditCellParams.js +0 -4
  67. package/models/params/gridRowParams.d.ts +4 -4
  68. package/models/props/DataGridProps.d.ts +0 -24
  69. package/modern/DataGrid/DataGrid.js +0 -20
  70. package/modern/DataGrid/useDataGridComponent.js +2 -4
  71. package/modern/components/cell/GridEditInputCell.js +3 -4
  72. package/modern/components/cell/GridEditSingleSelectCell.js +6 -34
  73. package/modern/components/panel/filterPanel/GridFilterForm.js +29 -7
  74. package/modern/components/panel/filterPanel/GridFilterPanel.js +62 -17
  75. package/modern/hooks/features/{editRows/gridEditRowsSelector.js → editing/gridEditingSelectors.js} +0 -0
  76. package/modern/hooks/features/editing/index.js +1 -0
  77. package/modern/hooks/features/{editRows/useGridCellEditing.new.js → editing/useGridCellEditing.js} +21 -6
  78. package/modern/hooks/features/{editRows/useGridEditing.new.js → editing/useGridEditing.js} +3 -3
  79. package/modern/hooks/features/{editRows/useGridRowEditing.new.js → editing/useGridRowEditing.js} +20 -6
  80. package/modern/hooks/features/index.js +1 -1
  81. package/modern/hooks/features/virtualization/useGridVirtualScroller.js +1 -1
  82. package/modern/index.js +1 -1
  83. package/modern/internals/index.js +1 -2
  84. package/modern/models/params/gridEditCellParams.js +0 -4
  85. package/node/DataGrid/DataGrid.js +0 -20
  86. package/node/DataGrid/useDataGridComponent.js +3 -8
  87. package/node/components/cell/GridEditInputCell.js +3 -9
  88. package/node/components/cell/GridEditSingleSelectCell.js +6 -38
  89. package/node/components/panel/filterPanel/GridFilterForm.js +30 -7
  90. package/node/components/panel/filterPanel/GridFilterPanel.js +63 -19
  91. package/node/hooks/features/{editRows/gridEditRowsSelector.js → editing/gridEditingSelectors.js} +0 -0
  92. package/node/hooks/features/{editRows → editing}/index.js +4 -4
  93. package/node/hooks/features/{editRows/useGridCellEditing.new.js → editing/useGridCellEditing.js} +28 -13
  94. package/node/hooks/features/{editRows/useGridEditing.new.js → editing/useGridEditing.js} +4 -4
  95. package/node/hooks/features/{editRows/useGridRowEditing.new.js → editing/useGridRowEditing.js} +28 -14
  96. package/node/hooks/features/index.js +4 -4
  97. package/node/hooks/features/virtualization/useGridVirtualScroller.js +2 -2
  98. package/node/index.js +1 -1
  99. package/node/internals/index.js +3 -17
  100. package/node/models/params/gridEditCellParams.js +0 -3
  101. package/package.json +2 -2
  102. package/hooks/features/editRows/index.d.ts +0 -1
  103. package/hooks/features/editRows/index.js +0 -1
  104. package/hooks/features/editRows/useGridCellEditing.old.d.ts +0 -4
  105. package/hooks/features/editRows/useGridCellEditing.old.js +0 -359
  106. package/hooks/features/editRows/useGridEditing.old.d.ts +0 -11
  107. package/hooks/features/editRows/useGridEditing.old.js +0 -167
  108. package/hooks/features/editRows/useGridRowEditing.old.d.ts +0 -4
  109. package/hooks/features/editRows/useGridRowEditing.old.js +0 -334
  110. package/legacy/hooks/features/editRows/index.js +0 -1
  111. package/legacy/hooks/features/editRows/useGridCellEditing.old.js +0 -411
  112. package/legacy/hooks/features/editRows/useGridEditing.old.js +0 -184
  113. package/legacy/hooks/features/editRows/useGridRowEditing.old.js +0 -505
  114. package/modern/hooks/features/editRows/index.js +0 -1
  115. package/modern/hooks/features/editRows/useGridCellEditing.old.js +0 -355
  116. package/modern/hooks/features/editRows/useGridEditing.old.js +0 -163
  117. package/modern/hooks/features/editRows/useGridRowEditing.old.js +0 -326
  118. package/node/hooks/features/editRows/useGridCellEditing.old.js +0 -380
  119. package/node/hooks/features/editRows/useGridEditing.old.js +0 -193
  120. package/node/hooks/features/editRows/useGridRowEditing.old.js +0 -358
@@ -1,359 +0,0 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
- import * as React from 'react';
3
- import { useEventCallback } from '@mui/material/utils';
4
- import { useGridApiOptionHandler, useGridApiEventHandler } from '../../utils/useGridApiEventHandler';
5
- import { GridCellModes, GridEditModes } from '../../../models/gridEditRowModel';
6
- import { isKeyboardEvent, isPrintableKey, isCellEnterEditModeKeys, isCellExitEditModeKeys, isCellEditCommitKeys, isDeleteKeys } from '../../../utils/keyboardUtils';
7
- import { useGridLogger } from '../../utils/useGridLogger';
8
- import { gridFocusCellSelector } from '../focus/gridFocusStateSelector';
9
- import { useGridApiMethod } from '../../utils/useGridApiMethod';
10
- import { gridEditRowsStateSelector } from './gridEditRowsSelector';
11
-
12
- function isPromise(promise) {
13
- return typeof promise.then === 'function';
14
- }
15
-
16
- export const useCellEditing = (apiRef, props) => {
17
- var _props$experimentalFe2;
18
-
19
- const logger = useGridLogger(apiRef, 'useGridEditRows');
20
-
21
- const buildCallback = callback => (...args) => {
22
- if (props.editMode === GridEditModes.Cell) {
23
- callback(...args);
24
- }
25
- };
26
-
27
- const setCellMode = React.useCallback((id, field, mode) => {
28
- if (apiRef.current.getCellMode(id, field) === mode) {
29
- return;
30
- }
31
-
32
- logger.debug(`Switching cell id: ${id} field: ${field} to mode: ${mode}`);
33
- apiRef.current.setState(state => {
34
- const newEditRowsState = _extends({}, state.editRows);
35
-
36
- newEditRowsState[id] = _extends({}, newEditRowsState[id]);
37
-
38
- if (mode === GridCellModes.Edit) {
39
- newEditRowsState[id][field] = {
40
- value: apiRef.current.getCellValue(id, field)
41
- };
42
- } else {
43
- delete newEditRowsState[id][field];
44
-
45
- if (!Object.keys(newEditRowsState[id]).length) {
46
- delete newEditRowsState[id];
47
- }
48
- }
49
-
50
- return _extends({}, state, {
51
- editRows: newEditRowsState
52
- });
53
- });
54
- apiRef.current.forceUpdate();
55
- apiRef.current.publishEvent('cellModeChange', apiRef.current.getCellParams(id, field));
56
- }, [apiRef, logger]);
57
- const getCellMode = React.useCallback((id, field) => {
58
- const editRowsState = gridEditRowsStateSelector(apiRef.current.state);
59
- const isEditing = editRowsState[id] && editRowsState[id][field];
60
- return isEditing ? GridCellModes.Edit : GridCellModes.View;
61
- }, [apiRef]); // TODO v6: it should always return a promise
62
-
63
- const commitCellChange = React.useCallback((params, event = {}) => {
64
- var _props$experimentalFe;
65
-
66
- const {
67
- id,
68
- field
69
- } = params;
70
- apiRef.current.unstable_runPendingEditCellValueMutation(id, field);
71
- const model = apiRef.current.getEditRowsModel();
72
-
73
- if (!model[id] || !model[id][field]) {
74
- throw new Error(`MUI: Cell at id: ${id} and field: ${field} is not in edit mode.`);
75
- }
76
-
77
- const editCellProps = model[id][field];
78
- const column = apiRef.current.getColumn(field);
79
- const row = apiRef.current.getRow(id);
80
-
81
- if ((_props$experimentalFe = props.experimentalFeatures) != null && _props$experimentalFe.preventCommitWhileValidating) {
82
- const cellProps = model[id][field];
83
-
84
- if (cellProps.isValidating || cellProps.error) {
85
- return false;
86
- }
87
- }
88
-
89
- const commitParams = _extends({}, params, {
90
- value: editCellProps.value
91
- });
92
-
93
- let hasError = !!editCellProps.error;
94
-
95
- if (!hasError && typeof column.preProcessEditCellProps === 'function') {
96
- const result = column.preProcessEditCellProps({
97
- id,
98
- row,
99
- props: editCellProps
100
- });
101
-
102
- if (isPromise(result)) {
103
- return result.then(newEditCellProps => {
104
- apiRef.current.unstable_setEditCellProps({
105
- id,
106
- field,
107
- props: newEditCellProps
108
- });
109
-
110
- if (newEditCellProps.error) {
111
- return false;
112
- }
113
-
114
- apiRef.current.publishEvent('cellEditCommit', commitParams, event);
115
- return true;
116
- });
117
- }
118
-
119
- apiRef.current.unstable_setEditCellProps({
120
- id,
121
- field,
122
- props: result
123
- });
124
- hasError = !!result.error;
125
- }
126
-
127
- if (!hasError) {
128
- apiRef.current.publishEvent('cellEditCommit', commitParams, event);
129
- return true;
130
- }
131
-
132
- return false;
133
- }, [apiRef, (_props$experimentalFe2 = props.experimentalFeatures) == null ? void 0 : _props$experimentalFe2.preventCommitWhileValidating]);
134
- const setCellEditingEditCellValue = React.useCallback(params => {
135
- const column = apiRef.current.getColumn(params.field);
136
- const row = apiRef.current.getRow(params.id);
137
- return new Promise(resolve => {
138
- let newEditCellProps = {
139
- value: params.value
140
- };
141
- const model = apiRef.current.getEditRowsModel();
142
- const editCellProps = model[params.id][params.field];
143
-
144
- if (typeof column.preProcessEditCellProps !== 'function') {
145
- apiRef.current.unstable_setEditCellProps(_extends({}, params, {
146
- props: newEditCellProps
147
- }));
148
- resolve(true);
149
- return;
150
- } // setEditCellProps runs the value parser and returns the updated props
151
-
152
-
153
- newEditCellProps = apiRef.current.unstable_setEditCellProps(_extends({}, params, {
154
- props: _extends({}, editCellProps, {
155
- isValidating: true
156
- })
157
- }));
158
- Promise.resolve(column.preProcessEditCellProps({
159
- id: params.id,
160
- row,
161
- props: _extends({}, newEditCellProps, {
162
- value: apiRef.current.unstable_parseValue(params.id, params.field, params.value)
163
- })
164
- })).then(newEditCellPropsProcessed => {
165
- apiRef.current.unstable_setEditCellProps(_extends({}, params, {
166
- props: _extends({}, newEditCellPropsProcessed, {
167
- isValidating: false
168
- })
169
- }));
170
- resolve(!newEditCellPropsProcessed.error);
171
- });
172
- });
173
- }, [apiRef]);
174
- const cellEditingApi = {
175
- setCellMode,
176
- getCellMode,
177
- commitCellChange,
178
- unstable_setCellEditingEditCellValue: setCellEditingEditCellValue
179
- };
180
- useGridApiMethod(apiRef, cellEditingApi, 'EditRowApi');
181
- const handleCellKeyDown = React.useCallback(async (params, event) => {
182
- // Wait until IME is settled for Asian languages like Japanese and Chinese
183
- // TODO: `event.which` is depricated but this is a temporary workaround
184
- if (event.which === 229) {
185
- return;
186
- }
187
-
188
- const {
189
- id,
190
- field,
191
- cellMode,
192
- isEditable
193
- } = params;
194
-
195
- if (!isEditable) {
196
- return;
197
- }
198
-
199
- const isEditMode = cellMode === GridCellModes.Edit;
200
- const isModifierKeyPressed = event.ctrlKey || event.metaKey || event.altKey;
201
-
202
- if (!isEditMode && isCellEnterEditModeKeys(event) && !isModifierKeyPressed && !(event.key === ' ' && event.shiftKey)) {
203
- apiRef.current.publishEvent('cellEditStart', params, event);
204
- }
205
-
206
- if (!isEditMode && isDeleteKeys(event.key)) {
207
- apiRef.current.setEditCellValue({
208
- id,
209
- field,
210
- value: ''
211
- });
212
- apiRef.current.commitCellChange({
213
- id,
214
- field
215
- }, event);
216
- apiRef.current.publishEvent('cellEditStop', params, event);
217
- }
218
-
219
- if (isEditMode && isCellEditCommitKeys(event.key)) {
220
- const commitParams = {
221
- id,
222
- field
223
- };
224
- const isValid = await apiRef.current.commitCellChange(commitParams, event);
225
-
226
- if (!isValid) {
227
- return;
228
- }
229
- }
230
-
231
- if (isEditMode && isCellExitEditModeKeys(event.key)) {
232
- apiRef.current.publishEvent('cellEditStop', params, event);
233
- }
234
- }, [apiRef]);
235
- const handleCellDoubleClick = React.useCallback((params, event) => {
236
- if (!params.isEditable) {
237
- return;
238
- }
239
-
240
- apiRef.current.publishEvent('cellEditStart', params, event);
241
- }, [apiRef]);
242
-
243
- const commitPropsAndExit = async (params, event) => {
244
- if (params.cellMode === GridCellModes.View) {
245
- return;
246
- }
247
-
248
- await apiRef.current.commitCellChange(params, event);
249
- apiRef.current.publishEvent('cellEditStop', params, event);
250
- };
251
-
252
- const handleCellFocusOut = useEventCallback((params, event) => {
253
- commitPropsAndExit(params, event);
254
- });
255
- const handleColumnHeaderDragStart = useEventCallback(() => {
256
- const cell = gridFocusCellSelector(apiRef);
257
-
258
- if (!cell) {
259
- return;
260
- }
261
-
262
- const params = apiRef.current.getCellParams(cell.id, cell.field);
263
- commitPropsAndExit(params, {});
264
- });
265
- const handleCellEditStart = React.useCallback((params, event) => {
266
- if (!params.isEditable) {
267
- return;
268
- }
269
-
270
- apiRef.current.setCellMode(params.id, params.field, GridCellModes.Edit);
271
-
272
- if (isKeyboardEvent(event) && isPrintableKey(event)) {
273
- apiRef.current.unstable_setEditCellProps({
274
- id: params.id,
275
- field: params.field,
276
- props: {
277
- value: ''
278
- }
279
- });
280
- }
281
- }, [apiRef]);
282
- const handleCellEditStop = React.useCallback((params, event) => {
283
- apiRef.current.setCellMode(params.id, params.field, GridCellModes.View);
284
-
285
- if (!isKeyboardEvent(event)) {
286
- return;
287
- }
288
-
289
- if (isCellEditCommitKeys(event.key)) {
290
- apiRef.current.publishEvent('cellNavigationKeyDown', params, event);
291
- return;
292
- }
293
-
294
- if (event.key === 'Escape' || isDeleteKeys(event.key)) {
295
- apiRef.current.setCellFocus(params.id, params.field);
296
- }
297
- }, [apiRef]);
298
- const handleCellEditCommit = React.useCallback(params => {
299
- const {
300
- id,
301
- field
302
- } = params;
303
- const model = apiRef.current.getEditRowsModel();
304
- const {
305
- value
306
- } = model[id][field];
307
- logger.debug(`Setting cell id: ${id} field: ${field} to value: ${value == null ? void 0 : value.toString()}`);
308
- const row = apiRef.current.getRow(id);
309
-
310
- if (row) {
311
- const column = apiRef.current.getColumn(params.field);
312
-
313
- let rowUpdate = _extends({}, row, {
314
- [field]: value
315
- });
316
-
317
- if (column.valueSetter) {
318
- rowUpdate = column.valueSetter({
319
- row,
320
- value
321
- });
322
- }
323
-
324
- apiRef.current.updateRows([rowUpdate]);
325
- }
326
- }, [apiRef, logger]);
327
- const handleEditCellPropsChange = React.useCallback(params => {
328
- const row = apiRef.current.getRow(params.id);
329
- const column = apiRef.current.getColumn(params.field);
330
- const editCellProps = column.preProcessEditCellProps ? column.preProcessEditCellProps({
331
- id: params.id,
332
- row,
333
- props: params.props
334
- }) : params.props;
335
-
336
- if (isPromise(editCellProps)) {
337
- editCellProps.then(newEditCellProps => {
338
- apiRef.current.unstable_setEditCellProps(_extends({}, params, {
339
- props: newEditCellProps
340
- }));
341
- });
342
- } else {
343
- apiRef.current.unstable_setEditCellProps(_extends({}, params, {
344
- props: editCellProps
345
- }));
346
- }
347
- }, [apiRef]);
348
- useGridApiEventHandler(apiRef, 'cellKeyDown', buildCallback(handleCellKeyDown));
349
- useGridApiEventHandler(apiRef, 'cellDoubleClick', buildCallback(handleCellDoubleClick));
350
- useGridApiEventHandler(apiRef, 'cellFocusOut', buildCallback(handleCellFocusOut));
351
- useGridApiEventHandler(apiRef, 'columnHeaderDragStart', buildCallback(handleColumnHeaderDragStart));
352
- useGridApiEventHandler(apiRef, 'cellEditStart', buildCallback(handleCellEditStart));
353
- useGridApiEventHandler(apiRef, 'cellEditStop', buildCallback(handleCellEditStop));
354
- useGridApiEventHandler(apiRef, 'cellEditCommit', buildCallback(handleCellEditCommit));
355
- useGridApiEventHandler(apiRef, 'editCellPropsChange', buildCallback(handleEditCellPropsChange));
356
- useGridApiOptionHandler(apiRef, 'cellEditCommit', props.onCellEditCommit);
357
- useGridApiOptionHandler(apiRef, 'cellEditStart', props.onCellEditStart);
358
- useGridApiOptionHandler(apiRef, 'cellEditStop', props.onCellEditStop);
359
- };
@@ -1,11 +0,0 @@
1
- import * as React from 'react';
2
- import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
3
- import { GridApiCommunity } from '../../../models/api/gridApiCommunity';
4
- import { GridStateInitializer } from '../../utils/useGridInitializeState';
5
- export declare const editingStateInitializer: GridStateInitializer;
6
- /**
7
- * @requires useGridFocus - can be after, async only
8
- * @requires useGridParamsApi (method)
9
- * @requires useGridColumns (state)
10
- */
11
- export declare function useGridEditing(apiRef: React.MutableRefObject<GridApiCommunity>, props: Pick<DataGridProcessedProps, 'editRowsModel' | 'onEditRowsModelChange' | 'isCellEditable' | 'onEditCellPropsChange' | 'editMode' | 'onRowEditCommit' | 'onRowEditStart' | 'onRowEditStop' | 'onCellEditCommit' | 'onCellEditStart' | 'onCellEditStop' | 'experimentalFeatures'>): void;
@@ -1,167 +0,0 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
- import * as React from 'react';
3
- import { GridCellModes } from '../../../models/gridEditRowModel';
4
- import { useGridApiEventHandler, useGridApiOptionHandler } from '../../utils/useGridApiEventHandler';
5
- import { useGridApiMethod } from '../../utils/useGridApiMethod';
6
- import { useGridLogger } from '../../utils/useGridLogger';
7
- import { gridEditRowsStateSelector } from './gridEditRowsSelector';
8
- import { useCellEditing } from './useGridCellEditing.old';
9
- import { useGridRowEditing } from './useGridRowEditing.old';
10
- import { isAutoGeneratedRow } from '../rows/gridRowsUtils';
11
- export const editingStateInitializer = state => _extends({}, state, {
12
- editRows: {}
13
- });
14
- /**
15
- * @requires useGridFocus - can be after, async only
16
- * @requires useGridParamsApi (method)
17
- * @requires useGridColumns (state)
18
- */
19
-
20
- export function useGridEditing(apiRef, props) {
21
- var _props$experimentalFe2;
22
-
23
- const logger = useGridLogger(apiRef, 'useGridEditRows');
24
- useCellEditing(apiRef, props);
25
- useGridRowEditing(apiRef, props);
26
- const debounceMap = React.useRef({});
27
- apiRef.current.unstable_registerControlState({
28
- stateId: 'editRows',
29
- propModel: props.editRowsModel,
30
- propOnChange: props.onEditRowsModelChange,
31
- stateSelector: gridEditRowsStateSelector,
32
- changeEvent: 'editRowsModelChange'
33
- });
34
- const isCellEditable = React.useCallback(params => !isAutoGeneratedRow(params.rowNode) && params.rowNode.type !== 'pinnedRow' && !!params.colDef.editable && !!params.colDef.renderEditCell && (!props.isCellEditable || props.isCellEditable(params)), // eslint-disable-next-line react-hooks/exhaustive-deps
35
- [props.isCellEditable]);
36
-
37
- const maybeDebounce = (id, field, debounceMs, callback) => {
38
- if (!debounceMs) {
39
- callback();
40
- return;
41
- }
42
-
43
- if (!debounceMap.current[id]) {
44
- debounceMap.current[id] = {};
45
- }
46
-
47
- if (debounceMap.current[id][field]) {
48
- const [timeout] = debounceMap.current[id][field];
49
- clearTimeout(timeout);
50
- }
51
-
52
- const callbackToRunImmediately = () => {
53
- callback();
54
- const [timeout] = debounceMap.current[id][field];
55
- clearTimeout(timeout);
56
- delete debounceMap.current[id][field];
57
- };
58
-
59
- const timeout = setTimeout(() => {
60
- callback();
61
- delete debounceMap.current[id][field];
62
- }, debounceMs);
63
- debounceMap.current[id][field] = [timeout, callbackToRunImmediately];
64
- };
65
-
66
- const runPendingEditCellValueMutation = React.useCallback((id, field) => {
67
- if (!debounceMap.current[id]) {
68
- return;
69
- }
70
-
71
- if (!field) {
72
- Object.keys(debounceMap.current[id]).forEach(debouncedField => {
73
- const [, callback] = debounceMap.current[id][debouncedField];
74
- callback();
75
- });
76
- } else if (debounceMap.current[id][field]) {
77
- const [, callback] = debounceMap.current[id][field];
78
- callback();
79
- }
80
- }, []);
81
- const setEditCellValue = React.useCallback((params, event = {}) => {
82
- maybeDebounce(params.id, params.field, params.debounceMs, () => {
83
- var _props$experimentalFe;
84
-
85
- if ((_props$experimentalFe = props.experimentalFeatures) != null && _props$experimentalFe.preventCommitWhileValidating) {
86
- if (props.editMode === 'row') {
87
- return apiRef.current.unstable_setRowEditingEditCellValue(params);
88
- }
89
-
90
- return apiRef.current.unstable_setCellEditingEditCellValue(params);
91
- }
92
-
93
- const newParams = {
94
- id: params.id,
95
- field: params.field,
96
- props: {
97
- value: params.value
98
- }
99
- };
100
- return apiRef.current.publishEvent('editCellPropsChange', newParams, event);
101
- });
102
- }, [apiRef, props.editMode, (_props$experimentalFe2 = props.experimentalFeatures) == null ? void 0 : _props$experimentalFe2.preventCommitWhileValidating]);
103
- const parseValue = React.useCallback((id, field, value) => {
104
- const column = apiRef.current.getColumn(field);
105
- return column.valueParser ? column.valueParser(value, apiRef.current.getCellParams(id, field)) : value;
106
- }, [apiRef]);
107
- const setEditCellProps = React.useCallback(params => {
108
- const {
109
- id,
110
- field,
111
- props: editProps
112
- } = params;
113
- logger.debug(`Setting cell props on id: ${id} field: ${field}`);
114
- apiRef.current.setState(state => {
115
- const editRowsModel = _extends({}, state.editRows);
116
-
117
- editRowsModel[id] = _extends({}, state.editRows[id]);
118
- editRowsModel[id][field] = _extends({}, editProps, {
119
- value: parseValue(id, field, editProps.value)
120
- });
121
- return _extends({}, state, {
122
- editRows: editRowsModel
123
- });
124
- });
125
- apiRef.current.forceUpdate();
126
- const editRowsState = gridEditRowsStateSelector(apiRef.current.state);
127
- return editRowsState[id][field];
128
- }, [apiRef, logger, parseValue]);
129
- const setEditRowsModel = React.useCallback(model => {
130
- const currentModel = gridEditRowsStateSelector(apiRef.current.state);
131
-
132
- if (currentModel !== model) {
133
- logger.debug(`Setting editRows model`);
134
- apiRef.current.setState(state => _extends({}, state, {
135
- editRows: model
136
- }));
137
- apiRef.current.forceUpdate();
138
- }
139
- }, [apiRef, logger]);
140
- const getEditRowsModel = React.useCallback(() => gridEditRowsStateSelector(apiRef.current.state), [apiRef]);
141
- const preventTextSelection = React.useCallback((params, event) => {
142
- const isMoreThanOneClick = event.detail > 1;
143
-
144
- if (params.isEditable && params.cellMode === GridCellModes.View && isMoreThanOneClick) {
145
- // If we click more than one time, then we prevent the default behavior of selecting the text cell.
146
- event.preventDefault();
147
- }
148
- }, []);
149
- useGridApiEventHandler(apiRef, 'cellMouseDown', preventTextSelection);
150
- useGridApiOptionHandler(apiRef, 'editCellPropsChange', props.onEditCellPropsChange); // TODO v6: remove, use `preProcessEditCellProps` instead
151
-
152
- const editingSharedApi = {
153
- isCellEditable,
154
- setEditRowsModel,
155
- getEditRowsModel,
156
- setEditCellValue,
157
- unstable_setEditCellProps: setEditCellProps,
158
- unstable_parseValue: parseValue,
159
- unstable_runPendingEditCellValueMutation: runPendingEditCellValueMutation
160
- };
161
- useGridApiMethod(apiRef, editingSharedApi, 'EditRowApi');
162
- React.useEffect(() => {
163
- if (props.editRowsModel !== undefined) {
164
- apiRef.current.setEditRowsModel(props.editRowsModel);
165
- }
166
- }, [apiRef, props.editRowsModel]);
167
- }
@@ -1,4 +0,0 @@
1
- import * as React from 'react';
2
- import { GridApiCommunity } from '../../../models/api/gridApiCommunity';
3
- import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
4
- export declare const useGridRowEditing: (apiRef: React.MutableRefObject<GridApiCommunity>, props: Pick<DataGridProcessedProps, 'editMode' | 'onRowEditCommit' | 'onRowEditStart' | 'onRowEditStop' | 'experimentalFeatures'>) => void;