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

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 (161) hide show
  1. package/CHANGELOG.md +209 -0
  2. package/DataGrid/useDataGridProps.js +4 -4
  3. package/components/DataGridVirtualScroller.js +5 -3
  4. package/components/GridPagination.d.ts +43 -1
  5. package/components/GridPagination.js +1 -2
  6. package/components/GridRow.d.ts +1 -4
  7. package/components/GridRow.js +9 -8
  8. package/components/base/GridBody.js +1 -2
  9. package/components/base/GridOverlays.js +52 -12
  10. package/components/cell/GridBooleanCell.js +2 -1
  11. package/components/columnSelection/GridCellCheckboxRenderer.d.ts +2 -2
  12. package/components/columnSelection/GridCellCheckboxRenderer.js +1 -6
  13. package/components/containers/GridOverlay.js +0 -5
  14. package/components/panel/filterPanel/GridFilterForm.js +2 -1
  15. package/components/toolbar/GridToolbarFilterButton.js +8 -4
  16. package/constants/gridClasses.d.ts +8 -0
  17. package/hooks/core/pipeProcessing/gridPipeProcessingApi.d.ts +1 -1
  18. package/hooks/core/strategyProcessing/gridStrategyProcessingApi.d.ts +1 -1
  19. package/hooks/features/columnHeaders/useGridColumnHeaders.js +1 -1
  20. package/hooks/features/editRows/useGridCellEditing.new.js +18 -9
  21. package/hooks/features/editRows/useGridEditing.new.js +3 -2
  22. package/hooks/features/editRows/useGridEditing.old.js +2 -1
  23. package/hooks/features/editRows/useGridRowEditing.new.js +18 -9
  24. package/hooks/features/export/useGridPrintExport.js +31 -17
  25. package/hooks/features/export/utils.js +1 -5
  26. package/hooks/features/filter/gridFilterSelector.js +2 -2
  27. package/hooks/features/filter/useGridFilter.js +6 -6
  28. package/hooks/features/pagination/gridPaginationSelector.js +2 -2
  29. package/hooks/features/rows/gridRowsInterfaces.d.ts +106 -0
  30. package/hooks/features/rows/{gridRowsState.js → gridRowsInterfaces.js} +0 -0
  31. package/hooks/features/rows/gridRowsSelector.d.ts +17 -7
  32. package/hooks/features/rows/gridRowsSelector.js +38 -6
  33. package/hooks/features/rows/gridRowsUtils.d.ts +16 -4
  34. package/hooks/features/rows/gridRowsUtils.js +222 -39
  35. package/hooks/features/rows/index.d.ts +3 -3
  36. package/hooks/features/rows/index.js +2 -2
  37. package/hooks/features/rows/useGridRows.js +161 -124
  38. package/hooks/features/rows/useGridRowsPreProcessors.js +78 -26
  39. package/hooks/features/selection/useGridSelection.js +2 -2
  40. package/hooks/features/sorting/gridSortingSelector.js +9 -4
  41. package/hooks/features/sorting/gridSortingState.d.ts +2 -2
  42. package/hooks/features/sorting/useGridSorting.js +9 -33
  43. package/hooks/features/virtualization/useGridVirtualScroller.d.ts +4 -7
  44. package/hooks/features/virtualization/useGridVirtualScroller.js +11 -17
  45. package/index.js +1 -1
  46. package/internals/index.d.ts +2 -1
  47. package/internals/index.js +1 -0
  48. package/legacy/DataGrid/useDataGridProps.js +4 -4
  49. package/legacy/components/DataGridVirtualScroller.js +5 -3
  50. package/legacy/components/GridPagination.js +1 -2
  51. package/legacy/components/GridRow.js +9 -8
  52. package/legacy/components/base/GridBody.js +1 -2
  53. package/legacy/components/base/GridOverlays.js +54 -12
  54. package/legacy/components/cell/GridBooleanCell.js +2 -1
  55. package/legacy/components/columnSelection/GridCellCheckboxRenderer.js +1 -6
  56. package/legacy/components/containers/GridOverlay.js +0 -5
  57. package/legacy/components/panel/filterPanel/GridFilterForm.js +2 -1
  58. package/legacy/components/toolbar/GridToolbarFilterButton.js +3 -1
  59. package/legacy/hooks/features/columnHeaders/useGridColumnHeaders.js +1 -1
  60. package/legacy/hooks/features/editRows/useGridCellEditing.new.js +18 -9
  61. package/legacy/hooks/features/editRows/useGridEditing.new.js +3 -2
  62. package/legacy/hooks/features/editRows/useGridEditing.old.js +2 -1
  63. package/legacy/hooks/features/editRows/useGridRowEditing.new.js +18 -9
  64. package/legacy/hooks/features/export/useGridPrintExport.js +33 -20
  65. package/legacy/hooks/features/export/utils.js +1 -3
  66. package/legacy/hooks/features/filter/gridFilterSelector.js +2 -2
  67. package/legacy/hooks/features/filter/useGridFilter.js +6 -6
  68. package/legacy/hooks/features/pagination/gridPaginationSelector.js +2 -2
  69. package/legacy/hooks/features/rows/{gridRowsState.js → gridRowsInterfaces.js} +0 -0
  70. package/legacy/hooks/features/rows/gridRowsSelector.js +52 -9
  71. package/legacy/hooks/features/rows/gridRowsUtils.js +238 -46
  72. package/legacy/hooks/features/rows/index.js +2 -2
  73. package/legacy/hooks/features/rows/useGridRows.js +163 -134
  74. package/legacy/hooks/features/rows/useGridRowsPreProcessors.js +81 -26
  75. package/legacy/hooks/features/selection/useGridSelection.js +2 -2
  76. package/legacy/hooks/features/sorting/gridSortingSelector.js +5 -2
  77. package/legacy/hooks/features/sorting/useGridSorting.js +11 -33
  78. package/legacy/hooks/features/virtualization/useGridVirtualScroller.js +11 -17
  79. package/legacy/index.js +1 -1
  80. package/legacy/internals/index.js +1 -0
  81. package/legacy/models/gridFeatureMode.js +1 -4
  82. package/models/api/gridParamsApi.d.ts +2 -2
  83. package/models/api/gridRowApi.d.ts +7 -5
  84. package/models/events/gridEventLookup.d.ts +3 -3
  85. package/models/gridApiCaches.d.ts +1 -1
  86. package/models/gridFeatureMode.d.ts +0 -4
  87. package/models/gridFeatureMode.js +1 -4
  88. package/models/gridRows.d.ts +100 -30
  89. package/models/gridSortModel.d.ts +2 -2
  90. package/models/params/gridCellParams.d.ts +7 -11
  91. package/modern/DataGrid/useDataGridProps.js +4 -4
  92. package/modern/components/DataGridVirtualScroller.js +5 -3
  93. package/modern/components/GridPagination.js +1 -2
  94. package/modern/components/GridRow.js +9 -8
  95. package/modern/components/base/GridBody.js +1 -2
  96. package/modern/components/base/GridOverlays.js +52 -12
  97. package/modern/components/cell/GridBooleanCell.js +2 -1
  98. package/modern/components/columnSelection/GridCellCheckboxRenderer.js +1 -6
  99. package/modern/components/containers/GridOverlay.js +0 -5
  100. package/modern/components/panel/filterPanel/GridFilterForm.js +2 -1
  101. package/modern/components/toolbar/GridToolbarFilterButton.js +1 -1
  102. package/modern/hooks/features/columnHeaders/useGridColumnHeaders.js +1 -1
  103. package/modern/hooks/features/editRows/useGridCellEditing.new.js +18 -9
  104. package/modern/hooks/features/editRows/useGridEditing.new.js +3 -2
  105. package/modern/hooks/features/editRows/useGridEditing.old.js +2 -1
  106. package/modern/hooks/features/editRows/useGridRowEditing.new.js +18 -9
  107. package/modern/hooks/features/export/useGridPrintExport.js +30 -16
  108. package/modern/hooks/features/export/utils.js +1 -1
  109. package/modern/hooks/features/filter/gridFilterSelector.js +2 -2
  110. package/modern/hooks/features/filter/useGridFilter.js +6 -6
  111. package/modern/hooks/features/pagination/gridPaginationSelector.js +2 -2
  112. package/modern/hooks/features/rows/{gridRowsState.js → gridRowsInterfaces.js} +0 -0
  113. package/modern/hooks/features/rows/gridRowsSelector.js +28 -6
  114. package/modern/hooks/features/rows/gridRowsUtils.js +220 -37
  115. package/modern/hooks/features/rows/index.js +2 -2
  116. package/modern/hooks/features/rows/useGridRows.js +158 -121
  117. package/modern/hooks/features/rows/useGridRowsPreProcessors.js +78 -26
  118. package/modern/hooks/features/selection/useGridSelection.js +2 -2
  119. package/modern/hooks/features/sorting/gridSortingSelector.js +3 -2
  120. package/modern/hooks/features/sorting/useGridSorting.js +9 -33
  121. package/modern/hooks/features/virtualization/useGridVirtualScroller.js +10 -16
  122. package/modern/index.js +1 -1
  123. package/modern/internals/index.js +1 -0
  124. package/modern/models/gridFeatureMode.js +1 -4
  125. package/node/DataGrid/useDataGridProps.js +3 -3
  126. package/node/components/DataGridVirtualScroller.js +5 -3
  127. package/node/components/GridPagination.js +1 -2
  128. package/node/components/GridRow.js +9 -7
  129. package/node/components/base/GridBody.js +1 -3
  130. package/node/components/base/GridOverlays.js +57 -12
  131. package/node/components/cell/GridBooleanCell.js +3 -1
  132. package/node/components/columnSelection/GridCellCheckboxRenderer.js +1 -6
  133. package/node/components/containers/GridOverlay.js +0 -5
  134. package/node/components/panel/filterPanel/GridFilterForm.js +2 -1
  135. package/node/components/toolbar/GridToolbarFilterButton.js +8 -4
  136. package/node/hooks/features/columnHeaders/useGridColumnHeaders.js +1 -1
  137. package/node/hooks/features/editRows/useGridCellEditing.new.js +20 -10
  138. package/node/hooks/features/editRows/useGridEditing.new.js +4 -2
  139. package/node/hooks/features/editRows/useGridEditing.old.js +3 -1
  140. package/node/hooks/features/editRows/useGridRowEditing.new.js +20 -10
  141. package/node/hooks/features/export/useGridPrintExport.js +31 -17
  142. package/node/hooks/features/export/utils.js +1 -5
  143. package/node/hooks/features/filter/gridFilterSelector.js +1 -1
  144. package/node/hooks/features/filter/useGridFilter.js +5 -6
  145. package/node/hooks/features/pagination/gridPaginationSelector.js +1 -1
  146. package/node/hooks/features/rows/{gridRowsState.js → gridRowsInterfaces.js} +0 -0
  147. package/node/hooks/features/rows/gridRowsSelector.js +43 -10
  148. package/node/hooks/features/rows/gridRowsUtils.js +239 -40
  149. package/node/hooks/features/rows/index.js +23 -9
  150. package/node/hooks/features/rows/useGridRows.js +161 -122
  151. package/node/hooks/features/rows/useGridRowsPreProcessors.js +81 -26
  152. package/node/hooks/features/selection/useGridSelection.js +2 -2
  153. package/node/hooks/features/sorting/gridSortingSelector.js +9 -4
  154. package/node/hooks/features/sorting/useGridSorting.js +9 -33
  155. package/node/hooks/features/virtualization/useGridVirtualScroller.js +11 -17
  156. package/node/index.js +1 -1
  157. package/node/internals/index.js +14 -2
  158. package/node/models/gridFeatureMode.js +1 -7
  159. package/package.json +1 -1
  160. package/themeAugmentation/props.d.ts +2 -2
  161. package/hooks/features/rows/gridRowsState.d.ts +0 -60
@@ -1,16 +1,16 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
3
2
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
3
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
4
  import _extends from "@babel/runtime/helpers/esm/extends";
5
5
  import * as React from 'react';
6
6
  import { useGridApiMethod } from '../../utils/useGridApiMethod';
7
7
  import { useGridLogger } from '../../utils/useGridLogger';
8
- import { gridRowCountSelector, gridRowsLookupSelector, gridRowTreeSelector, gridRowIdsSelector, gridRowGroupingNameSelector, gridRowsIdToIdLookupSelector } from './gridRowsSelector';
8
+ import { gridRowCountSelector, gridRowsLookupSelector, gridRowTreeSelector, gridRowGroupingNameSelector, gridRowTreeDepthsSelector, gridDataRowIdsSelector, gridRowsDataRowIdToIdLookupSelector, gridRowMaximumTreeDepthSelector } from './gridRowsSelector';
9
9
  import { GridSignature, useGridApiEventHandler } from '../../utils/useGridApiEventHandler';
10
10
  import { useGridVisibleRows } from '../../utils/useGridVisibleRows';
11
11
  import { gridSortedRowIdsSelector } from '../sorting/gridSortingSelector';
12
12
  import { gridFilteredRowsLookupSelector } from '../filter/gridFilterSelector';
13
- import { getTreeNodeDescendants, createRowsInternalCache, getRowsStateFromCache, getRowIdFromRowModel } from './gridRowsUtils';
13
+ import { getTreeNodeDescendants, createRowsInternalCache, getRowsStateFromCache, isAutoGeneratedRow, GRID_ROOT_GROUP_ID, updateCacheWithNewRows, getTopLevelRowCount, getRowIdFromRowModel } from './gridRowsUtils';
14
14
  import { useGridRegisterPipeApplier } from '../../core/pipeProcessing';
15
15
  export var rowsStateInitializer = function rowsStateInitializer(state, props, apiRef) {
16
16
  apiRef.current.unstable_caches.rows = createRowsInternalCache({
@@ -21,9 +21,10 @@ export var rowsStateInitializer = function rowsStateInitializer(state, props, ap
21
21
  return _extends({}, state, {
22
22
  rows: getRowsStateFromCache({
23
23
  apiRef: apiRef,
24
- previousTree: null,
25
24
  rowCountProp: props.rowCount,
26
- loadingProp: props.loading
25
+ loadingProp: props.loading,
26
+ previousTree: null,
27
+ previousTreeDepths: null
27
28
  })
28
29
  });
29
30
  };
@@ -41,18 +42,32 @@ export var useGridRows = function useGridRows(apiRef, props) {
41
42
  var lastUpdateMs = React.useRef(Date.now());
42
43
  var timeout = React.useRef(null);
43
44
  var getRow = React.useCallback(function (id) {
44
- var _ref;
45
+ var model = gridRowsLookupSelector(apiRef)[id];
46
+
47
+ if (model) {
48
+ return model;
49
+ }
50
+
51
+ var node = apiRef.current.getRowNode(id);
52
+
53
+ if (node && isAutoGeneratedRow(node)) {
54
+ // TODO rows v6: Is it the best approach ?
55
+ return {};
56
+ }
45
57
 
46
- return (_ref = gridRowsLookupSelector(apiRef)[id]) != null ? _ref : null;
58
+ return null;
47
59
  }, [apiRef]);
48
60
  var lookup = React.useMemo(function () {
49
- return currentPage.rows.reduce(function (acc, _ref2, index) {
50
- var id = _ref2.id;
61
+ return currentPage.rows.reduce(function (acc, _ref, index) {
62
+ var id = _ref.id;
51
63
  acc[id] = index;
52
64
  return acc;
53
65
  }, {});
54
66
  }, [currentPage.rows]);
55
- var throttledRowsChange = React.useCallback(function (newCache, throttle) {
67
+ var throttledRowsChange = React.useCallback(function (_ref2) {
68
+ var cache = _ref2.cache,
69
+ throttle = _ref2.throttle;
70
+
56
71
  var run = function run() {
57
72
  timeout.current = null;
58
73
  lastUpdateMs.current = Date.now();
@@ -60,9 +75,10 @@ export var useGridRows = function useGridRows(apiRef, props) {
60
75
  return _extends({}, state, {
61
76
  rows: getRowsStateFromCache({
62
77
  apiRef: apiRef,
63
- previousTree: gridRowTreeSelector(apiRef),
64
78
  rowCountProp: props.rowCount,
65
- loadingProp: props.loading
79
+ loadingProp: props.loading,
80
+ previousTree: gridRowTreeSelector(apiRef),
81
+ previousTreeDepths: gridRowTreeDepthsSelector(apiRef)
66
82
  })
67
83
  });
68
84
  });
@@ -75,7 +91,7 @@ export var useGridRows = function useGridRows(apiRef, props) {
75
91
  timeout.current = null;
76
92
  }
77
93
 
78
- apiRef.current.unstable_caches.rows = newCache;
94
+ apiRef.current.unstable_caches.rows = cache;
79
95
 
80
96
  if (!throttle) {
81
97
  run();
@@ -97,79 +113,45 @@ export var useGridRows = function useGridRows(apiRef, props) {
97
113
 
98
114
  var setRows = React.useCallback(function (rows) {
99
115
  logger.debug("Updating all rows, new length ".concat(rows.length));
100
- throttledRowsChange(createRowsInternalCache({
101
- rows: rows,
102
- getRowId: props.getRowId,
103
- loading: props.loading
104
- }), true);
116
+ throttledRowsChange({
117
+ cache: createRowsInternalCache({
118
+ rows: rows,
119
+ getRowId: props.getRowId,
120
+ loading: props.loading
121
+ }),
122
+ throttle: true
123
+ });
105
124
  }, [logger, props.getRowId, props.loading, throttledRowsChange]);
106
125
  var updateRows = React.useCallback(function (updates) {
107
126
  if (props.signature === GridSignature.DataGrid && updates.length > 1) {
108
127
  // TODO: Add test with direct call to `apiRef.current.updateRows` in DataGrid after enabling the `apiRef` on the free plan.
109
128
  throw new Error(["MUI: You can't update several rows at once in `apiRef.current.updateRows` on the DataGrid.", 'You need to upgrade to DataGridPro or DataGridPremium component to unlock this feature.'].join('\n'));
110
- } // we remove duplicate updates. A server can batch updates, and send several updates for the same row in one fn call.
111
-
112
-
113
- var uniqueUpdates = new Map();
114
- updates.forEach(function (update) {
115
- var id = getRowIdFromRowModel(update, props.getRowId, 'A row was provided without id when calling updateRows():');
129
+ }
116
130
 
117
- if (uniqueUpdates.has(id)) {
118
- uniqueUpdates.set(id, _extends({}, uniqueUpdates.get(id), update));
119
- } else {
120
- uniqueUpdates.set(id, update);
121
- }
131
+ var cache = updateCacheWithNewRows({
132
+ updates: updates,
133
+ getRowId: props.getRowId,
134
+ previousCache: apiRef.current.unstable_caches.rows
122
135
  });
123
- var deletedRowIds = [];
124
- var prevCache = apiRef.current.unstable_caches.rows;
125
- var newCache = {
126
- rowsBeforePartialUpdates: prevCache.rowsBeforePartialUpdates,
127
- loadingPropBeforePartialUpdates: prevCache.loadingPropBeforePartialUpdates,
128
- idRowsLookup: _extends({}, prevCache.idRowsLookup),
129
- idToIdLookup: _extends({}, prevCache.idToIdLookup),
130
- ids: _toConsumableArray(prevCache.ids)
131
- };
132
- uniqueUpdates.forEach(function (partialRow, id) {
133
- // eslint-disable-next-line no-underscore-dangle
134
- if (partialRow._action === 'delete') {
135
- delete newCache.idRowsLookup[id];
136
- delete newCache.idToIdLookup[id];
137
- deletedRowIds.push(id);
138
- return;
139
- }
140
-
141
- var oldRow = apiRef.current.getRow(id);
142
-
143
- if (!oldRow) {
144
- newCache.idRowsLookup[id] = partialRow;
145
- newCache.idToIdLookup[id] = id;
146
- newCache.ids.push(id);
147
- return;
148
- }
149
-
150
- newCache.idRowsLookup[id] = _extends({}, apiRef.current.getRow(id), partialRow);
136
+ throttledRowsChange({
137
+ cache: cache,
138
+ throttle: true
151
139
  });
152
-
153
- if (deletedRowIds.length > 0) {
154
- newCache.ids = newCache.ids.filter(function (id) {
155
- return !deletedRowIds.includes(id);
156
- });
157
- }
158
-
159
- throttledRowsChange(newCache, true);
160
140
  }, [props.signature, props.getRowId, throttledRowsChange, apiRef]);
161
141
  var getRowModels = React.useCallback(function () {
162
- var allRows = gridRowIdsSelector(apiRef);
142
+ var dataRows = gridDataRowIdsSelector(apiRef);
163
143
  var idRowsLookup = gridRowsLookupSelector(apiRef);
164
- return new Map(allRows.map(function (id) {
165
- return [id, idRowsLookup[id]];
144
+ return new Map(dataRows.map(function (id) {
145
+ var _idRowsLookup$id;
146
+
147
+ return [id, (_idRowsLookup$id = idRowsLookup[id]) != null ? _idRowsLookup$id : {}];
166
148
  }));
167
149
  }, [apiRef]);
168
150
  var getRowsCount = React.useCallback(function () {
169
151
  return gridRowCountSelector(apiRef);
170
152
  }, [apiRef]);
171
153
  var getAllRowIds = React.useCallback(function () {
172
- return gridRowIdsSelector(apiRef);
154
+ return gridDataRowIdsSelector(apiRef);
173
155
  }, [apiRef]);
174
156
  var getRowIndexRelativeToVisibleRows = React.useCallback(function (id) {
175
157
  return lookup[id];
@@ -181,6 +163,10 @@ export var useGridRows = function useGridRows(apiRef, props) {
181
163
  throw new Error("MUI: No row with id #".concat(id, " found"));
182
164
  }
183
165
 
166
+ if (currentNode.type !== 'group') {
167
+ throw new Error('MUI: Only group nodes can be expanded or collapsed');
168
+ }
169
+
184
170
  var newNode = _extends({}, currentNode, {
185
171
  childrenExpanded: isExpanded
186
172
  });
@@ -196,16 +182,16 @@ export var useGridRows = function useGridRows(apiRef, props) {
196
182
  apiRef.current.publishEvent('rowExpansionChange', newNode);
197
183
  }, [apiRef]);
198
184
  var getRowNode = React.useCallback(function (id) {
199
- var _gridRowTreeSelector$;
185
+ var _ref3;
200
186
 
201
- return (_gridRowTreeSelector$ = gridRowTreeSelector(apiRef)[id]) != null ? _gridRowTreeSelector$ : null;
187
+ return (_ref3 = gridRowTreeSelector(apiRef)[id]) != null ? _ref3 : null;
202
188
  }, [apiRef]);
203
- var getRowGroupChildren = React.useCallback(function (_ref3) {
204
- var _ref3$skipAutoGenerat = _ref3.skipAutoGeneratedRows,
205
- skipAutoGeneratedRows = _ref3$skipAutoGenerat === void 0 ? true : _ref3$skipAutoGenerat,
206
- groupId = _ref3.groupId,
207
- applySorting = _ref3.applySorting,
208
- applyFiltering = _ref3.applyFiltering;
189
+ var getRowGroupChildren = React.useCallback(function (_ref4) {
190
+ var _ref4$skipAutoGenerat = _ref4.skipAutoGeneratedRows,
191
+ skipAutoGeneratedRows = _ref4$skipAutoGenerat === void 0 ? true : _ref4$skipAutoGenerat,
192
+ groupId = _ref4.groupId,
193
+ applySorting = _ref4.applySorting,
194
+ applyFiltering = _ref4.applyFiltering;
209
195
  var tree = gridRowTreeSelector(apiRef);
210
196
  var children;
211
197
 
@@ -224,9 +210,8 @@ export var useGridRows = function useGridRows(apiRef, props) {
224
210
 
225
211
  for (var index = startIndex; index < sortedRowIds.length && tree[sortedRowIds[index]].depth > groupNode.depth; index += 1) {
226
212
  var id = sortedRowIds[index];
227
- var node = tree[id];
228
213
 
229
- if (!skipAutoGeneratedRows || !node.isAutoGenerated) {
214
+ if (!skipAutoGeneratedRows || !isAutoGeneratedRow(tree[id])) {
230
215
  children.push(id);
231
216
  }
232
217
  }
@@ -244,24 +229,41 @@ export var useGridRows = function useGridRows(apiRef, props) {
244
229
  return children;
245
230
  }, [apiRef]);
246
231
  var setRowIndex = React.useCallback(function (rowId, targetIndex) {
247
- var allRows = gridRowIdsSelector(apiRef);
248
- var oldIndex = allRows.findIndex(function (row) {
249
- return row === rowId;
250
- });
232
+ var node = apiRef.current.getRowNode(rowId);
251
233
 
252
- if (oldIndex === -1 || oldIndex === targetIndex) {
253
- return;
234
+ if (!node) {
235
+ throw new Error("MUI: No row with id #".concat(rowId, " found"));
254
236
  }
255
237
 
256
- logger.debug("Moving row ".concat(rowId, " to index ").concat(targetIndex));
238
+ if (node.parent !== GRID_ROOT_GROUP_ID) {
239
+ throw new Error("MUI: The row reordering do not support reordering of grouped rows yet");
240
+ }
257
241
 
258
- var updatedRows = _toConsumableArray(allRows);
242
+ if (node.type !== 'leaf') {
243
+ throw new Error("MUI: The row reordering do not support reordering of footer or grouping rows");
244
+ }
259
245
 
260
- updatedRows.splice(targetIndex, 0, updatedRows.splice(oldIndex, 1)[0]);
261
246
  apiRef.current.setState(function (state) {
247
+ var group = gridRowTreeSelector(state, apiRef.current.instanceId)[GRID_ROOT_GROUP_ID];
248
+ var allRows = group.children;
249
+ var oldIndex = allRows.findIndex(function (row) {
250
+ return row === rowId;
251
+ });
252
+
253
+ if (oldIndex === -1 || oldIndex === targetIndex) {
254
+ return state;
255
+ }
256
+
257
+ logger.debug("Moving row ".concat(rowId, " to index ").concat(targetIndex));
258
+
259
+ var updatedRows = _toConsumableArray(allRows);
260
+
261
+ updatedRows.splice(targetIndex, 0, updatedRows.splice(oldIndex, 1)[0]);
262
262
  return _extends({}, state, {
263
263
  rows: _extends({}, state.rows, {
264
- ids: updatedRows
264
+ tree: _extends({}, state.rows.tree, _defineProperty({}, GRID_ROOT_GROUP_ID, _extends({}, group, {
265
+ children: updatedRows
266
+ })))
265
267
  })
266
268
  });
267
269
  });
@@ -276,55 +278,59 @@ export var useGridRows = function useGridRows(apiRef, props) {
276
278
  return;
277
279
  }
278
280
 
279
- var allRows = gridRowIdsSelector(apiRef);
281
+ var treeDepth = gridRowMaximumTreeDepthSelector(apiRef);
280
282
 
281
- var updatedRows = _toConsumableArray(allRows);
283
+ if (treeDepth > 1) {
284
+ throw new Error('`apiRef.current.unstable_replaceRows` is not compatible with tree data and row grouping');
285
+ }
282
286
 
283
- var idRowsLookup = gridRowsLookupSelector(apiRef);
284
- var idToIdLookup = gridRowsIdToIdLookupSelector(apiRef);
285
- var tree = gridRowTreeSelector(apiRef);
287
+ var tree = _extends({}, gridRowTreeSelector(apiRef));
286
288
 
287
- var updatedIdRowsLookup = _extends({}, idRowsLookup);
289
+ var dataRowIdToModelLookup = _extends({}, gridRowsLookupSelector(apiRef));
288
290
 
289
- var updatedIdToIdLookup = _extends({}, idToIdLookup);
291
+ var dataRowIdToIdLookup = _extends({}, gridRowsDataRowIdToIdLookupSelector(apiRef));
290
292
 
291
- var updatedTree = _extends({}, tree);
293
+ var rootGroup = tree[GRID_ROOT_GROUP_ID];
292
294
 
293
- var newRowEntries = newRows.map(function (newRowModel) {
294
- var rowId = getRowIdFromRowModel(newRowModel, props.getRowId, 'A row was provided without id when calling replaceRows().');
295
- return {
296
- id: rowId,
297
- model: newRowModel
298
- };
299
- });
300
- newRowEntries.forEach(function (row, index) {
301
- var _updatedRows$splice = updatedRows.splice(firstRowToRender + index, 1, row.id),
302
- _updatedRows$splice2 = _slicedToArray(_updatedRows$splice, 1),
303
- replacedRowId = _updatedRows$splice2[0];
304
-
305
- delete updatedIdRowsLookup[replacedRowId];
306
- delete updatedIdToIdLookup[replacedRowId];
307
- delete updatedTree[replacedRowId];
308
- });
309
- newRowEntries.forEach(function (row) {
295
+ var rootGroupChildren = _toConsumableArray(rootGroup.children);
296
+
297
+ for (var i = 0; i < newRows.length; i += 1) {
298
+ var rowModel = newRows[i];
299
+ var rowId = getRowIdFromRowModel(rowModel, props.getRowId, 'A row was provided without id when calling replaceRows().');
300
+
301
+ var _rootGroupChildren$sp = rootGroupChildren.splice(firstRowToRender + i, 1, rowId),
302
+ _rootGroupChildren$sp2 = _slicedToArray(_rootGroupChildren$sp, 1),
303
+ replacedRowId = _rootGroupChildren$sp2[0];
304
+
305
+ delete dataRowIdToModelLookup[replacedRowId];
306
+ delete dataRowIdToIdLookup[replacedRowId];
307
+ delete tree[replacedRowId];
310
308
  var rowTreeNodeConfig = {
311
- id: row.id,
312
- parent: null,
309
+ id: rowId,
313
310
  depth: 0,
314
- groupingKey: null,
315
- groupingField: null
311
+ parent: GRID_ROOT_GROUP_ID,
312
+ type: 'leaf',
313
+ groupingKey: null
316
314
  };
317
- updatedIdRowsLookup[row.id] = row.model;
318
- updatedIdToIdLookup[row.id] = row.id;
319
- updatedTree[row.id] = rowTreeNodeConfig;
315
+ dataRowIdToModelLookup[rowId] = rowModel;
316
+ dataRowIdToIdLookup[rowId] = rowId;
317
+ tree[rowId] = rowTreeNodeConfig;
318
+ }
319
+
320
+ tree[GRID_ROOT_GROUP_ID] = _extends({}, rootGroup, {
321
+ children: rootGroupChildren
322
+ }); // Removes potential remaining skeleton rows from the dataRowIds.
323
+
324
+ var dataRowIds = rootGroupChildren.filter(function (childId) {
325
+ return tree[childId].type === 'leaf';
320
326
  });
321
327
  apiRef.current.setState(function (state) {
322
328
  return _extends({}, state, {
323
329
  rows: _extends({}, state.rows, {
324
- idRowsLookup: updatedIdRowsLookup,
325
- idToIdLookup: updatedIdToIdLookup,
326
- tree: updatedTree,
327
- ids: updatedRows
330
+ dataRowIdToModelLookup: dataRowIdToModelLookup,
331
+ dataRowIdToIdLookup: dataRowIdToIdLookup,
332
+ dataRowIds: dataRowIds,
333
+ tree: tree
328
334
  })
329
335
  });
330
336
  });
@@ -355,7 +361,12 @@ export var useGridRows = function useGridRows(apiRef, props) {
355
361
  if (apiRef.current.unstable_caches.rows.rowsBeforePartialUpdates === props.rows) {
356
362
  // The `props.rows` did not change since the last row grouping
357
363
  // We can use the current rows cache which contains the partial updates done recently.
358
- cache = apiRef.current.unstable_caches.rows;
364
+ cache = _extends({}, apiRef.current.unstable_caches.rows, {
365
+ updates: {
366
+ type: 'full',
367
+ rows: gridDataRowIdsSelector(apiRef)
368
+ }
369
+ });
359
370
  } else {
360
371
  // The `props.rows` has changed since the last row grouping
361
372
  // We must use the new `props.rows` on the new grouping
@@ -367,7 +378,10 @@ export var useGridRows = function useGridRows(apiRef, props) {
367
378
  });
368
379
  }
369
380
 
370
- throttledRowsChange(cache, false);
381
+ throttledRowsChange({
382
+ cache: cache,
383
+ throttle: false
384
+ });
371
385
  }, [logger, apiRef, props.rows, props.getRowId, props.loading, throttledRowsChange]);
372
386
  var handleStrategyProcessorChange = React.useCallback(function (methodName) {
373
387
  if (methodName === 'rowTreeCreation') {
@@ -389,13 +403,25 @@ export var useGridRows = function useGridRows(apiRef, props) {
389
403
 
390
404
  var applyHydrateRowsProcessor = React.useCallback(function () {
391
405
  apiRef.current.setState(function (state) {
406
+ var response = apiRef.current.unstable_applyPipeProcessors('hydrateRows', {
407
+ tree: gridRowTreeSelector(state, apiRef.current.instanceId),
408
+ treeDepths: gridRowTreeDepthsSelector(state, apiRef.current.instanceId),
409
+ dataRowIds: gridDataRowIdsSelector(state, apiRef.current.instanceId),
410
+ dataRowIdToModelLookup: gridRowsLookupSelector(state, apiRef.current.instanceId),
411
+ dataRowIdToIdLookup: gridRowsDataRowIdToIdLookupSelector(state, apiRef.current.instanceId)
412
+ });
392
413
  return _extends({}, state, {
393
- rows: _extends({}, state.rows, apiRef.current.unstable_applyPipeProcessors('hydrateRows', state.rows.groupingResponseBeforeRowHydration))
414
+ rows: _extends({}, state.rows, response, {
415
+ totalTopLevelRowCount: getTopLevelRowCount({
416
+ tree: response.tree,
417
+ rowCountProp: props.rowCount
418
+ })
419
+ })
394
420
  });
395
421
  });
396
422
  apiRef.current.publishEvent('rowsSet');
397
423
  apiRef.current.forceUpdate();
398
- }, [apiRef]);
424
+ }, [apiRef, props.rowCount]);
399
425
  useGridRegisterPipeApplier(apiRef, 'hydrateRows', applyHydrateRowsProcessor);
400
426
  useGridApiMethod(apiRef, rowApi, 'GridRowApi');
401
427
  /**
@@ -439,10 +465,13 @@ export var useGridRows = function useGridRows(apiRef, props) {
439
465
  }
440
466
 
441
467
  logger.debug("Updating all rows, new length ".concat(props.rows.length));
442
- throttledRowsChange(createRowsInternalCache({
443
- rows: props.rows,
444
- getRowId: props.getRowId,
445
- loading: props.loading
446
- }), false);
468
+ throttledRowsChange({
469
+ cache: createRowsInternalCache({
470
+ rows: props.rows,
471
+ getRowId: props.getRowId,
472
+ loading: props.loading
473
+ }),
474
+ throttle: false
475
+ });
447
476
  }, [props.rows, props.rowCount, props.getRowId, props.loading, logger, throttledRowsChange, apiRef]);
448
477
  };
@@ -1,39 +1,94 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
3
+ import _extends from "@babel/runtime/helpers/esm/extends";
1
4
  import { GRID_DEFAULT_STRATEGY, useGridRegisterStrategyProcessor } from '../../core/strategyProcessing';
5
+ import { buildRootGroup, GRID_ROOT_GROUP_ID } from './gridRowsUtils';
2
6
 
3
- var flatRowTreeCreationMethod = function flatRowTreeCreationMethod(_ref) {
4
- var ids = _ref.ids,
5
- idRowsLookup = _ref.idRowsLookup,
6
- idToIdLookup = _ref.idToIdLookup,
7
- previousTree = _ref.previousTree;
8
- var tree = {};
9
-
10
- for (var i = 0; i < ids.length; i += 1) {
11
- var rowId = ids[i];
12
-
13
- if (previousTree && previousTree[rowId] && previousTree[rowId].depth === 0 && previousTree[rowId].parent == null && // pinned row can be unpinned
14
- !previousTree[rowId].isPinned) {
15
- tree[rowId] = previousTree[rowId];
16
- } else {
17
- tree[rowId] = {
18
- id: rowId,
19
- depth: 0,
20
- parent: null,
21
- groupingKey: '',
22
- groupingField: null
23
- };
24
- }
7
+ var createFlatRowTree = function createFlatRowTree(rows) {
8
+ var tree = _defineProperty({}, GRID_ROOT_GROUP_ID, _extends({}, buildRootGroup(), {
9
+ children: rows
10
+ }));
11
+
12
+ for (var i = 0; i < rows.length; i += 1) {
13
+ var rowId = rows[i];
14
+ tree[rowId] = {
15
+ id: rowId,
16
+ depth: 0,
17
+ parent: GRID_ROOT_GROUP_ID,
18
+ type: 'leaf',
19
+ groupingKey: null
20
+ };
25
21
  }
26
22
 
27
23
  return {
28
24
  groupingName: GRID_DEFAULT_STRATEGY,
29
25
  tree: tree,
30
- treeDepth: 1,
31
- idRowsLookup: idRowsLookup,
32
- idToIdLookup: idToIdLookup,
33
- ids: ids
26
+ treeDepths: {
27
+ 0: rows.length
28
+ },
29
+ dataRowIds: rows
34
30
  };
35
31
  };
36
32
 
33
+ var updateFlatRowTree = function updateFlatRowTree(_ref) {
34
+ var previousTree = _ref.previousTree,
35
+ actions = _ref.actions;
36
+
37
+ var tree = _extends({}, previousTree);
38
+
39
+ var idsToRemoveFromRootGroup = {};
40
+
41
+ for (var i = 0; i < actions.remove.length; i += 1) {
42
+ var idToDelete = actions.remove[i];
43
+ idsToRemoveFromRootGroup[idToDelete] = true;
44
+ delete tree[idToDelete];
45
+ }
46
+
47
+ for (var _i = 0; _i < actions.insert.length; _i += 1) {
48
+ var idToInsert = actions.insert[_i];
49
+ tree[idToInsert] = {
50
+ id: idToInsert,
51
+ depth: 0,
52
+ parent: GRID_ROOT_GROUP_ID,
53
+ type: 'leaf',
54
+ groupingKey: null
55
+ };
56
+ } // TODO rows v6: Support row unpinning
57
+
58
+
59
+ var rootGroup = tree[GRID_ROOT_GROUP_ID];
60
+ var rootGroupChildren = [].concat(_toConsumableArray(rootGroup.children), _toConsumableArray(actions.insert));
61
+
62
+ if (Object.values(idsToRemoveFromRootGroup).length) {
63
+ rootGroupChildren = rootGroupChildren.filter(function (id) {
64
+ return !idsToRemoveFromRootGroup[id];
65
+ });
66
+ }
67
+
68
+ tree[GRID_ROOT_GROUP_ID] = _extends({}, rootGroup, {
69
+ children: rootGroupChildren
70
+ });
71
+ return {
72
+ groupingName: GRID_DEFAULT_STRATEGY,
73
+ tree: tree,
74
+ treeDepths: {
75
+ 0: rootGroupChildren.length
76
+ },
77
+ dataRowIds: rootGroupChildren
78
+ };
79
+ };
80
+
81
+ var flatRowTreeCreationMethod = function flatRowTreeCreationMethod(params) {
82
+ if (params.updates.type === 'full') {
83
+ return createFlatRowTree(params.updates.rows);
84
+ }
85
+
86
+ return updateFlatRowTree({
87
+ previousTree: params.previousTree,
88
+ actions: params.updates.actions
89
+ });
90
+ };
91
+
37
92
  export var useGridRowsPreProcessors = function useGridRowsPreProcessors(apiRef) {
38
93
  useGridRegisterStrategyProcessor(apiRef, GRID_DEFAULT_STRATEGY, 'rowTreeCreation', flatRowTreeCreationMethod);
39
94
  };
@@ -126,7 +126,7 @@ export var useGridSelection = function useGridSelection(apiRef, props) {
126
126
 
127
127
  var rowNode = apiRef.current.getRowNode(id);
128
128
 
129
- if ((rowNode == null ? void 0 : rowNode.position) === 'footer' || rowNode != null && rowNode.isPinned) {
129
+ if ((rowNode == null ? void 0 : rowNode.type) === 'footer' || (rowNode == null ? void 0 : rowNode.type) === 'pinnedRow') {
130
130
  return false;
131
131
  }
132
132
 
@@ -296,7 +296,7 @@ export var useGridSelection = function useGridSelection(apiRef, props) {
296
296
  }
297
297
  }
298
298
 
299
- if (params.rowNode.isPinned) {
299
+ if (params.rowNode.type === 'pinnedRow') {
300
300
  return;
301
301
  }
302
302
 
@@ -21,11 +21,14 @@ export var gridSortedRowIdsSelector = createSelector(gridSortingStateSelector, f
21
21
  * @category Sorting
22
22
  */
23
23
 
24
- export var gridSortedRowEntriesSelector = createSelector(gridSortedRowIdsSelector, gridRowsLookupSelector, function (sortedIds, idRowsLookup) {
24
+ export var gridSortedRowEntriesSelector = createSelector(gridSortedRowIdsSelector, gridRowsLookupSelector, // TODO rows v6: Is this the best approach ?
25
+ function (sortedIds, idRowsLookup) {
25
26
  return sortedIds.map(function (id) {
27
+ var _idRowsLookup$id;
28
+
26
29
  return {
27
30
  id: id,
28
- model: idRowsLookup[id]
31
+ model: (_idRowsLookup$id = idRowsLookup[id]) != null ? _idRowsLookup$id : {}
29
32
  };
30
33
  });
31
34
  });