@mui/x-data-grid 7.0.0-beta.6 → 7.0.0

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 (329) hide show
  1. package/CHANGELOG.md +311 -12
  2. package/DataGrid/DataGrid.js +57 -17
  3. package/DataGrid/useDataGridComponent.js +3 -0
  4. package/DataGrid/useDataGridProps.js +6 -7
  5. package/README.md +1 -1
  6. package/components/GridColumnHeaders.d.ts +1 -2
  7. package/components/GridColumnHeaders.js +6 -17
  8. package/components/GridFooter.js +2 -3
  9. package/components/GridHeader.js +1 -2
  10. package/components/GridHeaders.js +1 -4
  11. package/components/GridPagination.d.ts +6 -5
  12. package/components/GridPagination.js +14 -11
  13. package/components/GridRow.d.ts +1 -1
  14. package/components/GridRow.js +17 -36
  15. package/components/GridScrollArea.d.ts +10 -0
  16. package/components/GridScrollArea.js +150 -0
  17. package/components/base/GridFooterPlaceholder.js +1 -2
  18. package/components/base/GridOverlays.js +3 -6
  19. package/components/cell/GridActionsCell.js +4 -6
  20. package/components/cell/GridActionsCellItem.d.ts +8 -25
  21. package/components/cell/GridActionsCellItem.js +8 -5
  22. package/components/cell/GridBooleanCell.d.ts +1 -0
  23. package/components/cell/GridBooleanCell.js +3 -2
  24. package/components/cell/GridCell.d.ts +7 -0
  25. package/components/cell/GridCell.js +18 -11
  26. package/components/cell/GridEditBooleanCell.js +1 -2
  27. package/components/cell/GridEditDateCell.js +2 -3
  28. package/components/cell/GridEditInputCell.js +2 -2
  29. package/components/cell/GridEditSingleSelectCell.js +5 -8
  30. package/components/columnHeaders/ColumnHeaderMenuIcon.js +2 -3
  31. package/components/columnHeaders/GridBaseColumnHeaders.js +1 -0
  32. package/components/columnHeaders/GridColumnGroupHeader.d.ts +5 -0
  33. package/components/columnHeaders/GridColumnGroupHeader.js +22 -13
  34. package/components/columnHeaders/GridColumnHeaderFilterIconButton.js +2 -3
  35. package/components/columnHeaders/GridColumnHeaderItem.d.ts +5 -0
  36. package/components/columnHeaders/GridColumnHeaderItem.js +26 -12
  37. package/components/columnHeaders/GridColumnHeaderSortIcon.js +1 -2
  38. package/components/columnHeaders/GridColumnHeaderTitle.js +2 -3
  39. package/components/columnHeaders/GridGenericColumnHeaderItem.d.ts +1 -0
  40. package/components/columnHeaders/GridGenericColumnHeaderItem.js +7 -6
  41. package/components/columnSelection/GridCellCheckboxRenderer.js +3 -5
  42. package/components/columnSelection/GridHeaderCheckbox.js +1 -2
  43. package/components/columnsManagement/GridColumnsManagement.js +17 -21
  44. package/components/containers/GridRoot.js +3 -3
  45. package/components/containers/GridRootStyles.js +12 -20
  46. package/components/menu/GridMenu.js +4 -6
  47. package/components/menu/columnMenu/GridColumnHeaderMenu.js +1 -1
  48. package/components/menu/columnMenu/menuItems/GridColumnMenuSortItem.js +2 -3
  49. package/components/panel/GridColumnsPanel.js +1 -2
  50. package/components/panel/GridPanel.d.ts +1 -10
  51. package/components/panel/GridPanel.js +1 -2
  52. package/components/panel/GridPreferencesPanel.js +2 -3
  53. package/components/panel/filterPanel/GridFilterForm.js +24 -27
  54. package/components/panel/filterPanel/GridFilterInputBoolean.d.ts +1 -1
  55. package/components/panel/filterPanel/GridFilterInputBoolean.js +6 -7
  56. package/components/panel/filterPanel/GridFilterInputDate.d.ts +1 -1
  57. package/components/panel/filterPanel/GridFilterInputDate.js +3 -4
  58. package/components/panel/filterPanel/GridFilterInputMultipleSingleSelect.js +11 -15
  59. package/components/panel/filterPanel/GridFilterInputMultipleValue.js +10 -14
  60. package/components/panel/filterPanel/GridFilterInputSingleSelect.d.ts +1 -1
  61. package/components/panel/filterPanel/GridFilterInputSingleSelect.js +10 -11
  62. package/components/panel/filterPanel/GridFilterInputValue.d.ts +1 -1
  63. package/components/panel/filterPanel/GridFilterInputValue.js +5 -7
  64. package/components/panel/filterPanel/GridFilterPanel.js +5 -9
  65. package/components/panel/filterPanel/filterPanelUtils.js +1 -1
  66. package/components/toolbar/GridToolbarColumnsButton.js +3 -5
  67. package/components/toolbar/GridToolbarDensitySelector.js +8 -10
  68. package/components/toolbar/GridToolbarExport.js +2 -2
  69. package/components/toolbar/GridToolbarExportContainer.js +3 -5
  70. package/components/toolbar/GridToolbarFilterButton.js +3 -5
  71. package/components/toolbar/GridToolbarQuickFilter.js +4 -5
  72. package/components/virtualization/GridVirtualScrollbar.js +4 -0
  73. package/components/virtualization/GridVirtualScroller.js +6 -1
  74. package/components/virtualization/GridVirtualScrollerContent.js +1 -2
  75. package/components/virtualization/GridVirtualScrollerFiller.js +2 -1
  76. package/components/virtualization/GridVirtualScrollerRenderZone.js +1 -2
  77. package/constants/gridClasses.d.ts +6 -32
  78. package/constants/gridClasses.js +1 -1
  79. package/hooks/core/pipeProcessing/gridPipeProcessingApi.d.ts +5 -1
  80. package/hooks/core/strategyProcessing/useGridStrategyProcessing.js +1 -2
  81. package/hooks/core/useGridApiInitialization.js +4 -6
  82. package/hooks/features/clipboard/useGridClipboard.js +6 -5
  83. package/hooks/features/columnGrouping/gridColumnGroupsSelector.js +4 -16
  84. package/hooks/features/columnGrouping/gridColumnGroupsUtils.js +4 -8
  85. package/hooks/features/columnGrouping/useGridColumnGrouping.js +12 -23
  86. package/hooks/features/columnHeaders/useGridColumnHeaders.d.ts +23 -7
  87. package/hooks/features/columnHeaders/useGridColumnHeaders.js +186 -101
  88. package/hooks/features/columnMenu/useGridColumnMenuSlots.js +2 -2
  89. package/hooks/features/columnResize/columnResizeSelector.d.ts +3 -0
  90. package/hooks/features/columnResize/columnResizeSelector.js +3 -0
  91. package/hooks/features/columnResize/columnResizeState.d.ts +3 -0
  92. package/hooks/features/columnResize/columnResizeState.js +1 -0
  93. package/hooks/features/columnResize/gridColumnResizeApi.d.ts +44 -0
  94. package/hooks/features/columnResize/gridColumnResizeApi.js +10 -0
  95. package/hooks/features/columnResize/index.d.ts +3 -0
  96. package/hooks/features/columnResize/index.js +3 -0
  97. package/hooks/features/columnResize/useGridColumnResize.d.ts +10 -0
  98. package/hooks/features/columnResize/useGridColumnResize.js +553 -0
  99. package/hooks/features/columns/gridColumnsSelector.js +1 -2
  100. package/hooks/features/columns/gridColumnsUtils.d.ts +0 -9
  101. package/hooks/features/columns/gridColumnsUtils.js +5 -22
  102. package/hooks/features/columns/useGridColumnSpanning.js +1 -2
  103. package/hooks/features/columns/useGridColumns.js +11 -19
  104. package/hooks/features/density/densitySelector.d.ts +4 -2
  105. package/hooks/features/density/densitySelector.js +8 -2
  106. package/hooks/features/density/densityState.d.ts +1 -4
  107. package/hooks/features/density/useGridDensity.d.ts +2 -4
  108. package/hooks/features/density/useGridDensity.js +21 -29
  109. package/hooks/features/dimensions/useGridDimensions.js +7 -10
  110. package/hooks/features/editing/useGridCellEditing.js +25 -8
  111. package/hooks/features/editing/useGridEditing.js +1 -2
  112. package/hooks/features/editing/useGridRowEditing.js +4 -6
  113. package/hooks/features/export/serializers/csvSerializer.d.ts +2 -0
  114. package/hooks/features/export/serializers/csvSerializer.js +25 -16
  115. package/hooks/features/export/useGridCsvExport.js +9 -10
  116. package/hooks/features/export/useGridPrintExport.js +17 -22
  117. package/hooks/features/export/utils.js +2 -3
  118. package/hooks/features/filter/gridFilterSelector.js +15 -22
  119. package/hooks/features/filter/gridFilterUtils.js +10 -16
  120. package/hooks/features/filter/useGridFilter.js +9 -15
  121. package/hooks/features/focus/useGridFocus.js +5 -6
  122. package/hooks/features/headerFiltering/gridHeaderFilteringSelectors.d.ts +1 -0
  123. package/hooks/features/headerFiltering/gridHeaderFilteringSelectors.js +3 -0
  124. package/hooks/features/headerFiltering/useGridHeaderFiltering.js +23 -5
  125. package/hooks/features/index.d.ts +1 -0
  126. package/hooks/features/index.js +1 -0
  127. package/hooks/features/keyboardNavigation/useGridKeyboardNavigation.js +1 -2
  128. package/hooks/features/pagination/gridPaginationInterfaces.d.ts +19 -2
  129. package/hooks/features/pagination/gridPaginationSelector.d.ts +5 -0
  130. package/hooks/features/pagination/gridPaginationSelector.js +9 -4
  131. package/hooks/features/pagination/useGridPagination.d.ts +1 -6
  132. package/hooks/features/pagination/useGridPagination.js +9 -158
  133. package/hooks/features/pagination/useGridPaginationModel.d.ts +11 -0
  134. package/hooks/features/pagination/useGridPaginationModel.js +165 -0
  135. package/hooks/features/pagination/useGridRowCount.d.ts +8 -0
  136. package/hooks/features/pagination/useGridRowCount.js +94 -0
  137. package/hooks/features/preferencesPanel/useGridPreferencesPanel.js +7 -12
  138. package/hooks/features/rowSelection/useGridRowSelection.js +11 -16
  139. package/hooks/features/rows/gridRowsSelector.js +11 -19
  140. package/hooks/features/rows/gridRowsUtils.js +7 -9
  141. package/hooks/features/rows/useGridParamsApi.js +1 -1
  142. package/hooks/features/rows/useGridRows.js +4 -13
  143. package/hooks/features/rows/useGridRowsMeta.js +7 -13
  144. package/hooks/features/scroll/useGridScroll.js +3 -4
  145. package/hooks/features/sorting/gridSortingSelector.js +4 -7
  146. package/hooks/features/sorting/useGridSorting.js +8 -14
  147. package/hooks/features/virtualization/gridVirtualizationSelectors.d.ts +4 -4
  148. package/hooks/features/virtualization/gridVirtualizationSelectors.js +0 -2
  149. package/hooks/features/virtualization/useGridVirtualScroller.d.ts +3 -3
  150. package/hooks/features/virtualization/useGridVirtualScroller.js +220 -71
  151. package/hooks/utils/useGridApiEventHandler.js +5 -10
  152. package/hooks/utils/useGridNativeEventListener.js +1 -2
  153. package/hooks/utils/useLazyRef.d.ts +1 -2
  154. package/hooks/utils/useLazyRef.js +1 -11
  155. package/hooks/utils/useOnMount.d.ts +1 -2
  156. package/hooks/utils/useOnMount.js +1 -7
  157. package/hooks/utils/useTimeout.d.ts +1 -11
  158. package/hooks/utils/useTimeout.js +1 -36
  159. package/index.js +1 -1
  160. package/internals/index.d.ts +3 -1
  161. package/internals/index.js +2 -1
  162. package/internals/utils/getPinnedCellOffset.d.ts +3 -0
  163. package/internals/utils/getPinnedCellOffset.js +17 -0
  164. package/internals/utils/useProps.js +1 -2
  165. package/joy/joySlots.js +18 -63
  166. package/locales/jaJP.js +3 -4
  167. package/material/index.js +0 -2
  168. package/models/api/gridApiCommon.d.ts +3 -3
  169. package/models/api/gridCoreApi.d.ts +1 -5
  170. package/models/api/gridRowsMetaApi.d.ts +1 -1
  171. package/models/api/index.d.ts +1 -1
  172. package/models/api/index.js +0 -1
  173. package/models/events/gridEventLookup.d.ts +13 -0
  174. package/models/gridExport.d.ts +6 -0
  175. package/models/gridHeaderFilteringModel.d.ts +1 -0
  176. package/models/gridSlotsComponent.d.ts +0 -5
  177. package/models/gridStateCommunity.d.ts +3 -0
  178. package/models/params/gridScrollParams.d.ts +5 -3
  179. package/models/props/DataGridProps.d.ts +58 -28
  180. package/modern/DataGrid/DataGrid.js +57 -17
  181. package/modern/DataGrid/useDataGridComponent.js +3 -0
  182. package/modern/DataGrid/useDataGridProps.js +6 -7
  183. package/modern/components/GridColumnHeaders.js +6 -17
  184. package/modern/components/GridHeaders.js +1 -4
  185. package/modern/components/GridPagination.js +13 -6
  186. package/modern/components/GridRow.js +4 -19
  187. package/modern/components/GridScrollArea.js +150 -0
  188. package/modern/components/cell/GridActionsCell.js +1 -1
  189. package/modern/components/cell/GridActionsCellItem.js +4 -0
  190. package/modern/components/cell/GridBooleanCell.js +3 -2
  191. package/modern/components/cell/GridCell.js +11 -3
  192. package/modern/components/columnHeaders/GridBaseColumnHeaders.js +1 -0
  193. package/modern/components/columnHeaders/GridColumnGroupHeader.js +18 -8
  194. package/modern/components/columnHeaders/GridColumnHeaderItem.js +21 -6
  195. package/modern/components/columnHeaders/GridGenericColumnHeaderItem.js +6 -5
  196. package/modern/components/containers/GridRoot.js +3 -3
  197. package/modern/components/containers/GridRootStyles.js +12 -20
  198. package/modern/components/panel/filterPanel/GridFilterInputBoolean.js +1 -1
  199. package/modern/components/panel/filterPanel/GridFilterInputDate.js +1 -1
  200. package/modern/components/panel/filterPanel/GridFilterInputSingleSelect.js +1 -1
  201. package/modern/components/panel/filterPanel/GridFilterInputValue.js +1 -1
  202. package/modern/components/toolbar/GridToolbarDensitySelector.js +5 -5
  203. package/modern/components/virtualization/GridVirtualScrollbar.js +4 -0
  204. package/modern/components/virtualization/GridVirtualScroller.js +6 -1
  205. package/modern/components/virtualization/GridVirtualScrollerFiller.js +2 -1
  206. package/modern/constants/gridClasses.js +1 -1
  207. package/modern/hooks/features/clipboard/useGridClipboard.js +4 -2
  208. package/modern/hooks/features/columnHeaders/useGridColumnHeaders.js +184 -97
  209. package/modern/hooks/features/columnResize/columnResizeSelector.js +3 -0
  210. package/modern/hooks/features/columnResize/columnResizeState.js +1 -0
  211. package/modern/hooks/features/columnResize/gridColumnResizeApi.js +10 -0
  212. package/modern/hooks/features/columnResize/index.js +3 -0
  213. package/modern/hooks/features/columnResize/useGridColumnResize.js +553 -0
  214. package/modern/hooks/features/columns/gridColumnsUtils.js +4 -20
  215. package/modern/hooks/features/density/densitySelector.js +8 -2
  216. package/modern/hooks/features/density/useGridDensity.js +21 -29
  217. package/modern/hooks/features/dimensions/useGridDimensions.js +4 -5
  218. package/modern/hooks/features/editing/useGridCellEditing.js +22 -3
  219. package/modern/hooks/features/editing/useGridRowEditing.js +1 -1
  220. package/modern/hooks/features/export/serializers/csvSerializer.js +23 -12
  221. package/modern/hooks/features/export/useGridCsvExport.js +2 -1
  222. package/modern/hooks/features/export/useGridPrintExport.js +8 -7
  223. package/modern/hooks/features/filter/gridFilterUtils.js +1 -1
  224. package/modern/hooks/features/headerFiltering/gridHeaderFilteringSelectors.js +3 -0
  225. package/modern/hooks/features/headerFiltering/useGridHeaderFiltering.js +21 -2
  226. package/modern/hooks/features/index.js +1 -0
  227. package/modern/hooks/features/pagination/gridPaginationSelector.js +8 -2
  228. package/modern/hooks/features/pagination/useGridPagination.js +8 -149
  229. package/modern/hooks/features/pagination/useGridPaginationModel.js +165 -0
  230. package/modern/hooks/features/pagination/useGridRowCount.js +94 -0
  231. package/modern/hooks/features/rowSelection/useGridRowSelection.js +3 -2
  232. package/modern/hooks/features/scroll/useGridScroll.js +1 -1
  233. package/modern/hooks/features/virtualization/gridVirtualizationSelectors.js +0 -2
  234. package/modern/hooks/features/virtualization/useGridVirtualScroller.js +211 -57
  235. package/modern/hooks/utils/useLazyRef.js +1 -11
  236. package/modern/hooks/utils/useOnMount.js +1 -7
  237. package/modern/hooks/utils/useTimeout.js +1 -36
  238. package/modern/index.js +1 -1
  239. package/modern/internals/index.js +2 -1
  240. package/modern/internals/utils/getPinnedCellOffset.js +17 -0
  241. package/modern/joy/joySlots.js +11 -50
  242. package/modern/locales/jaJP.js +3 -4
  243. package/modern/material/index.js +0 -2
  244. package/modern/models/api/index.js +0 -1
  245. package/modern/utils/cellBorderUtils.js +8 -0
  246. package/modern/utils/createSelector.js +1 -1
  247. package/modern/utils/domUtils.js +144 -0
  248. package/modern/utils/keyboardUtils.js +1 -1
  249. package/node/DataGrid/DataGrid.js +57 -17
  250. package/node/DataGrid/useDataGridComponent.js +3 -0
  251. package/node/DataGrid/useDataGridProps.js +6 -7
  252. package/node/components/GridColumnHeaders.js +6 -16
  253. package/node/components/GridHeaders.js +1 -4
  254. package/node/components/GridPagination.js +10 -4
  255. package/node/components/GridRow.js +3 -18
  256. package/node/components/GridScrollArea.js +158 -0
  257. package/node/components/cell/GridActionsCell.js +1 -1
  258. package/node/components/cell/GridActionsCellItem.js +4 -0
  259. package/node/components/cell/GridBooleanCell.js +3 -2
  260. package/node/components/cell/GridCell.js +12 -4
  261. package/node/components/columnHeaders/GridBaseColumnHeaders.js +1 -0
  262. package/node/components/columnHeaders/GridColumnGroupHeader.js +18 -8
  263. package/node/components/columnHeaders/GridColumnHeaderItem.js +21 -6
  264. package/node/components/columnHeaders/GridGenericColumnHeaderItem.js +6 -5
  265. package/node/components/containers/GridRoot.js +2 -2
  266. package/node/components/containers/GridRootStyles.js +12 -20
  267. package/node/components/panel/filterPanel/GridFilterInputBoolean.js +1 -1
  268. package/node/components/panel/filterPanel/GridFilterInputDate.js +1 -1
  269. package/node/components/panel/filterPanel/GridFilterInputSingleSelect.js +1 -1
  270. package/node/components/panel/filterPanel/GridFilterInputValue.js +1 -1
  271. package/node/components/toolbar/GridToolbarDensitySelector.js +4 -4
  272. package/node/components/virtualization/GridVirtualScrollbar.js +4 -0
  273. package/node/components/virtualization/GridVirtualScroller.js +6 -1
  274. package/node/components/virtualization/GridVirtualScrollerFiller.js +2 -1
  275. package/node/constants/gridClasses.js +1 -1
  276. package/node/hooks/features/clipboard/useGridClipboard.js +4 -2
  277. package/node/hooks/features/columnHeaders/useGridColumnHeaders.js +190 -103
  278. package/node/hooks/features/columnResize/columnResizeSelector.js +10 -0
  279. package/node/hooks/features/columnResize/columnResizeState.js +5 -0
  280. package/node/hooks/features/columnResize/gridColumnResizeApi.js +16 -0
  281. package/node/hooks/features/columnResize/index.js +38 -0
  282. package/node/hooks/features/columnResize/useGridColumnResize.js +564 -0
  283. package/node/hooks/features/columns/gridColumnsUtils.js +4 -21
  284. package/node/hooks/features/density/densitySelector.js +9 -3
  285. package/node/hooks/features/density/useGridDensity.js +22 -30
  286. package/node/hooks/features/dimensions/useGridDimensions.js +4 -5
  287. package/node/hooks/features/editing/useGridCellEditing.js +22 -3
  288. package/node/hooks/features/editing/useGridRowEditing.js +1 -1
  289. package/node/hooks/features/export/serializers/csvSerializer.js +23 -12
  290. package/node/hooks/features/export/useGridCsvExport.js +2 -1
  291. package/node/hooks/features/export/useGridPrintExport.js +8 -7
  292. package/node/hooks/features/filter/gridFilterUtils.js +1 -1
  293. package/node/hooks/features/headerFiltering/gridHeaderFilteringSelectors.js +4 -1
  294. package/node/hooks/features/headerFiltering/useGridHeaderFiltering.js +21 -2
  295. package/node/hooks/features/index.js +11 -0
  296. package/node/hooks/features/pagination/gridPaginationSelector.js +8 -2
  297. package/node/hooks/features/pagination/useGridPagination.js +9 -153
  298. package/node/hooks/features/pagination/useGridPaginationModel.js +176 -0
  299. package/node/hooks/features/pagination/useGridRowCount.js +103 -0
  300. package/node/hooks/features/rowSelection/useGridRowSelection.js +2 -1
  301. package/node/hooks/features/scroll/useGridScroll.js +1 -1
  302. package/node/hooks/features/virtualization/gridVirtualizationSelectors.js +0 -2
  303. package/node/hooks/features/virtualization/useGridVirtualScroller.js +211 -57
  304. package/node/hooks/utils/useLazyRef.js +7 -13
  305. package/node/hooks/utils/useOnMount.js +8 -10
  306. package/node/hooks/utils/useTimeout.js +7 -37
  307. package/node/index.js +1 -1
  308. package/node/internals/index.js +27 -8
  309. package/node/internals/utils/getPinnedCellOffset.js +24 -0
  310. package/node/joy/joySlots.js +11 -50
  311. package/node/locales/jaJP.js +3 -4
  312. package/node/material/index.js +0 -2
  313. package/node/models/api/index.js +0 -11
  314. package/node/utils/cellBorderUtils.js +16 -0
  315. package/node/utils/createSelector.js +1 -1
  316. package/node/utils/domUtils.js +155 -0
  317. package/node/utils/keyboardUtils.js +1 -1
  318. package/package.json +4 -4
  319. package/utils/cellBorderUtils.d.ts +3 -0
  320. package/utils/cellBorderUtils.js +8 -0
  321. package/utils/createSelector.js +9 -9
  322. package/utils/domUtils.d.ts +14 -1
  323. package/utils/domUtils.js +144 -0
  324. package/utils/getGridLocalization.js +9 -12
  325. package/utils/keyboardUtils.js +1 -1
  326. package/components/columnHeaders/GridColumnHeadersInner.d.ts +0 -8
  327. package/components/columnHeaders/GridColumnHeadersInner.js +0 -58
  328. package/modern/components/columnHeaders/GridColumnHeadersInner.js +0 -58
  329. package/node/components/columnHeaders/GridColumnHeadersInner.js +0 -67
@@ -0,0 +1,553 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import * as React from 'react';
3
+ import { unstable_ownerDocument as ownerDocument, unstable_useEventCallback as useEventCallback } from '@mui/utils';
4
+ import { useTheme } from '@mui/material/styles';
5
+ import { findGridCellElementsFromCol, findGridElement, findLeftPinnedCellsAfterCol, findRightPinnedCellsBeforeCol, getFieldFromHeaderElem, findHeaderElementFromField, findGroupHeaderElementsFromField, findGridHeader, findGridCells, findParentElementFromClassName, findLeftPinnedHeadersAfterCol, findRightPinnedHeadersBeforeCol } from '../../../utils/domUtils';
6
+ import { DEFAULT_GRID_AUTOSIZE_OPTIONS } from './gridColumnResizeApi';
7
+ import { gridClasses } from '../../../constants/gridClasses';
8
+ import { useGridApiEventHandler, useGridApiMethod, useGridApiOptionHandler, useGridLogger, useGridNativeEventListener, useGridSelector, useOnMount } from '../../utils';
9
+ import { gridVirtualizationColumnEnabledSelector } from '../virtualization';
10
+ import { createControllablePromise } from '../../../utils/createControllablePromise';
11
+ import { clamp } from '../../../utils/utils';
12
+ import { useTimeout } from '../../utils/useTimeout';
13
+ import { GridPinnedColumnPosition } from '../columns/gridColumnsInterfaces';
14
+ import { gridColumnsStateSelector } from '../columns';
15
+ // TODO: remove support for Safari < 13.
16
+ // https://caniuse.com/#search=touch-action
17
+ //
18
+ // Safari, on iOS, supports touch action since v13.
19
+ // Over 80% of the iOS phones are compatible
20
+ // in August 2020.
21
+ // Utilizing the CSS.supports method to check if touch-action is supported.
22
+ // Since CSS.supports is supported on all but Edge@12 and IE and touch-action
23
+ // is supported on both Edge@12 and IE if CSS.supports is not available that means that
24
+ // touch-action will be supported
25
+ let cachedSupportsTouchActionNone = false;
26
+ function doesSupportTouchActionNone() {
27
+ if (cachedSupportsTouchActionNone === undefined) {
28
+ if (typeof CSS !== 'undefined' && typeof CSS.supports === 'function') {
29
+ cachedSupportsTouchActionNone = CSS.supports('touch-action', 'none');
30
+ } else {
31
+ cachedSupportsTouchActionNone = true;
32
+ }
33
+ }
34
+ return cachedSupportsTouchActionNone;
35
+ }
36
+ function trackFinger(event, currentTouchId) {
37
+ if (currentTouchId !== undefined && event.changedTouches) {
38
+ for (let i = 0; i < event.changedTouches.length; i += 1) {
39
+ const touch = event.changedTouches[i];
40
+ if (touch.identifier === currentTouchId) {
41
+ return {
42
+ x: touch.clientX,
43
+ y: touch.clientY
44
+ };
45
+ }
46
+ }
47
+ return false;
48
+ }
49
+ return {
50
+ x: event.clientX,
51
+ y: event.clientY
52
+ };
53
+ }
54
+ function computeNewWidth(initialOffsetToSeparator, clickX, columnBounds, resizeDirection) {
55
+ let newWidth = initialOffsetToSeparator;
56
+ if (resizeDirection === 'Right') {
57
+ newWidth += clickX - columnBounds.left;
58
+ } else {
59
+ newWidth += columnBounds.right - clickX;
60
+ }
61
+ return newWidth;
62
+ }
63
+ function computeOffsetToSeparator(clickX, columnBounds, resizeDirection) {
64
+ if (resizeDirection === 'Left') {
65
+ return clickX - columnBounds.left;
66
+ }
67
+ return columnBounds.right - clickX;
68
+ }
69
+ function flipResizeDirection(side) {
70
+ if (side === 'Right') {
71
+ return 'Left';
72
+ }
73
+ return 'Right';
74
+ }
75
+ function getResizeDirection(separator, direction) {
76
+ const side = separator.classList.contains(gridClasses['columnSeparator--sideRight']) ? 'Right' : 'Left';
77
+ if (direction === 'rtl') {
78
+ // Resizing logic should be mirrored in the RTL case
79
+ return flipResizeDirection(side);
80
+ }
81
+ return side;
82
+ }
83
+ function preventClick(event) {
84
+ event.preventDefault();
85
+ event.stopImmediatePropagation();
86
+ }
87
+
88
+ /**
89
+ * Checker that returns a promise that resolves when the column virtualization
90
+ * is disabled.
91
+ */
92
+ function useColumnVirtualizationDisabled(apiRef) {
93
+ const promise = React.useRef();
94
+ const selector = () => gridVirtualizationColumnEnabledSelector(apiRef);
95
+ const value = useGridSelector(apiRef, selector);
96
+ React.useEffect(() => {
97
+ if (promise.current && value === false) {
98
+ promise.current.resolve();
99
+ promise.current = undefined;
100
+ }
101
+ });
102
+ const asyncCheck = () => {
103
+ if (!promise.current) {
104
+ if (selector() === false) {
105
+ return Promise.resolve();
106
+ }
107
+ promise.current = createControllablePromise();
108
+ }
109
+ return promise.current;
110
+ };
111
+ return asyncCheck;
112
+ }
113
+
114
+ /**
115
+ * Basic statistical outlier detection, checks if the value is `F * IQR` away from
116
+ * the Q1 and Q3 boundaries. IQR: interquartile range.
117
+ */
118
+ function excludeOutliers(inputValues, factor) {
119
+ if (inputValues.length < 4) {
120
+ return inputValues;
121
+ }
122
+ const values = inputValues.slice();
123
+ values.sort((a, b) => a - b);
124
+ const q1 = values[Math.floor(values.length * 0.25)];
125
+ const q3 = values[Math.floor(values.length * 0.75) - 1];
126
+ const iqr = q3 - q1;
127
+
128
+ // We make a small adjustment if `iqr < 5` for the cases where the IQR is
129
+ // very small (for example zero) due to very close by values in the input data.
130
+ // Otherwise, with an IQR of `0`, anything outside that would be considered
131
+ // an outlier, but it makes more sense visually to allow for this 5px variance
132
+ // rather than showing a cropped cell.
133
+ const deviation = iqr < 5 ? 5 : iqr * factor;
134
+ return values.filter(v => v > q1 - deviation && v < q3 + deviation);
135
+ }
136
+ function extractColumnWidths(apiRef, options, columns) {
137
+ const widthByField = {};
138
+ const root = apiRef.current.rootElementRef.current;
139
+ root.classList.add(gridClasses.autosizing);
140
+ columns.forEach(column => {
141
+ const cells = findGridCells(apiRef.current, column.field);
142
+ const widths = cells.map(cell => {
143
+ return cell.getBoundingClientRect().width ?? 0;
144
+ });
145
+ const filteredWidths = options.includeOutliers ? widths : excludeOutliers(widths, options.outliersFactor);
146
+ if (options.includeHeaders) {
147
+ const header = findGridHeader(apiRef.current, column.field);
148
+ if (header) {
149
+ const title = header.querySelector(`.${gridClasses.columnHeaderTitle}`);
150
+ const content = header.querySelector(`.${gridClasses.columnHeaderTitleContainerContent}`);
151
+ const iconContainer = header.querySelector(`.${gridClasses.iconButtonContainer}`);
152
+ const menuContainer = header.querySelector(`.${gridClasses.menuIcon}`);
153
+ const element = title ?? content;
154
+ const style = window.getComputedStyle(header, null);
155
+ const paddingWidth = parseInt(style.paddingLeft, 10) + parseInt(style.paddingRight, 10);
156
+ const contentWidth = element.scrollWidth + 1;
157
+ const width = contentWidth + paddingWidth + (iconContainer?.clientWidth ?? 0) + (menuContainer?.clientWidth ?? 0);
158
+ filteredWidths.push(width);
159
+ }
160
+ }
161
+ const hasColumnMin = column.minWidth !== -Infinity && column.minWidth !== undefined;
162
+ const hasColumnMax = column.maxWidth !== Infinity && column.maxWidth !== undefined;
163
+ const min = hasColumnMin ? column.minWidth : 0;
164
+ const max = hasColumnMax ? column.maxWidth : Infinity;
165
+ const maxContent = filteredWidths.length === 0 ? 0 : Math.max(...filteredWidths);
166
+ widthByField[column.field] = clamp(maxContent, min, max);
167
+ });
168
+ root.classList.remove(gridClasses.autosizing);
169
+ return widthByField;
170
+ }
171
+ export const columnResizeStateInitializer = state => _extends({}, state, {
172
+ columnResize: {
173
+ resizingColumnField: ''
174
+ }
175
+ });
176
+ /**
177
+ * @requires useGridColumns (method, event)
178
+ * TODO: improve experience for last column
179
+ */
180
+ export const useGridColumnResize = (apiRef, props) => {
181
+ const theme = useTheme();
182
+ const logger = useGridLogger(apiRef, 'useGridColumnResize');
183
+ const colDefRef = React.useRef();
184
+ const previousMouseClickEvent = React.useRef();
185
+ const columnHeaderElementRef = React.useRef();
186
+ const headerFilterElementRef = React.useRef();
187
+ const groupHeaderElementsRef = React.useRef([]);
188
+ const cellElementsRef = React.useRef([]);
189
+ const leftPinnedCellsAfterRef = React.useRef([]);
190
+ const rightPinnedCellsBeforeRef = React.useRef([]);
191
+ const fillerLeftRef = React.useRef();
192
+ const fillerRightRef = React.useRef();
193
+ const leftPinnedHeadersAfterRef = React.useRef([]);
194
+ const rightPinnedHeadersBeforeRef = React.useRef([]);
195
+
196
+ // To improve accessibility, the separator has padding on both sides.
197
+ // Clicking inside the padding area should be treated as a click in the separator.
198
+ // This ref stores the offset between the click and the separator.
199
+ const initialOffsetToSeparator = React.useRef();
200
+ const resizeDirection = React.useRef();
201
+ const stopResizeEventTimeout = useTimeout();
202
+ const touchId = React.useRef();
203
+ const updateWidth = newWidth => {
204
+ logger.debug(`Updating width to ${newWidth} for col ${colDefRef.current.field}`);
205
+ const prevWidth = columnHeaderElementRef.current.offsetWidth;
206
+ const widthDiff = newWidth - prevWidth;
207
+ colDefRef.current.computedWidth = newWidth;
208
+ colDefRef.current.width = newWidth;
209
+ colDefRef.current.flex = 0;
210
+ columnHeaderElementRef.current.style.width = `${newWidth}px`;
211
+ columnHeaderElementRef.current.style.minWidth = `${newWidth}px`;
212
+ columnHeaderElementRef.current.style.maxWidth = `${newWidth}px`;
213
+ const headerFilterElement = headerFilterElementRef.current;
214
+ if (headerFilterElement) {
215
+ headerFilterElement.style.width = `${newWidth}px`;
216
+ headerFilterElement.style.minWidth = `${newWidth}px`;
217
+ headerFilterElement.style.maxWidth = `${newWidth}px`;
218
+ }
219
+ groupHeaderElementsRef.current.forEach(element => {
220
+ const div = element;
221
+ let finalWidth;
222
+ if (div.getAttribute('aria-colspan') === '1') {
223
+ finalWidth = `${newWidth}px`;
224
+ } else {
225
+ // Cell with colspan > 1 cannot be just updated width new width.
226
+ // Instead, we add width diff to the current width.
227
+ finalWidth = `${div.offsetWidth + widthDiff}px`;
228
+ }
229
+ div.style.width = finalWidth;
230
+ div.style.minWidth = finalWidth;
231
+ div.style.maxWidth = finalWidth;
232
+ });
233
+ cellElementsRef.current.forEach(element => {
234
+ const div = element;
235
+ let finalWidth;
236
+ if (div.getAttribute('aria-colspan') === '1') {
237
+ finalWidth = `${newWidth}px`;
238
+ } else {
239
+ // Cell with colspan > 1 cannot be just updated width new width.
240
+ // Instead, we add width diff to the current width.
241
+ finalWidth = `${div.offsetWidth + widthDiff}px`;
242
+ }
243
+ div.style.setProperty('--width', finalWidth);
244
+ });
245
+ const pinnedPosition = apiRef.current.unstable_applyPipeProcessors('isColumnPinned', false, colDefRef.current.field);
246
+ if (pinnedPosition === GridPinnedColumnPosition.LEFT) {
247
+ updateProperty(fillerLeftRef.current, 'width', widthDiff);
248
+ leftPinnedCellsAfterRef.current.forEach(cell => {
249
+ updateProperty(cell, 'left', widthDiff);
250
+ });
251
+ leftPinnedHeadersAfterRef.current.forEach(header => {
252
+ updateProperty(header, 'left', widthDiff);
253
+ });
254
+ }
255
+ if (pinnedPosition === GridPinnedColumnPosition.RIGHT) {
256
+ updateProperty(fillerRightRef.current, 'width', widthDiff);
257
+ rightPinnedCellsBeforeRef.current.forEach(cell => {
258
+ updateProperty(cell, 'right', widthDiff);
259
+ });
260
+ rightPinnedHeadersBeforeRef.current.forEach(header => {
261
+ updateProperty(header, 'right', widthDiff);
262
+ });
263
+ }
264
+ };
265
+ const finishResize = nativeEvent => {
266
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
267
+ stopListening();
268
+
269
+ // Prevent double-clicks from being interpreted as two separate clicks
270
+ if (previousMouseClickEvent.current) {
271
+ const prevEvent = previousMouseClickEvent.current;
272
+ const prevTimeStamp = prevEvent.timeStamp;
273
+ const prevClientX = prevEvent.clientX;
274
+ const prevClientY = prevEvent.clientY;
275
+
276
+ // Check if the current event is part of a double-click
277
+ if (nativeEvent.timeStamp - prevTimeStamp < 300 && nativeEvent.clientX === prevClientX && nativeEvent.clientY === prevClientY) {
278
+ previousMouseClickEvent.current = undefined;
279
+ return;
280
+ }
281
+ }
282
+ if (colDefRef.current) {
283
+ apiRef.current.setColumnWidth(colDefRef.current.field, colDefRef.current.width);
284
+ logger.debug(`Updating col ${colDefRef.current.field} with new width: ${colDefRef.current.width}`);
285
+ }
286
+ stopResizeEventTimeout.start(0, () => {
287
+ apiRef.current.publishEvent('columnResizeStop', null, nativeEvent);
288
+ });
289
+ };
290
+ const storeReferences = (colDef, separator, xStart) => {
291
+ const root = apiRef.current.rootElementRef.current;
292
+ colDefRef.current = colDef;
293
+ columnHeaderElementRef.current = findHeaderElementFromField(apiRef.current.columnHeadersContainerRef.current, colDef.field);
294
+ const headerFilterElement = root.querySelector(`.${gridClasses.headerFilterRow} [data-field="${colDef.field}"]`);
295
+ if (headerFilterElement) {
296
+ headerFilterElementRef.current = headerFilterElement;
297
+ }
298
+ groupHeaderElementsRef.current = findGroupHeaderElementsFromField(apiRef.current.columnHeadersContainerRef?.current, colDef.field);
299
+ cellElementsRef.current = findGridCellElementsFromCol(columnHeaderElementRef.current, apiRef.current);
300
+ fillerLeftRef.current = findGridElement(apiRef.current, 'filler--pinnedLeft');
301
+ fillerRightRef.current = findGridElement(apiRef.current, 'filler--pinnedRight');
302
+ const pinnedPosition = apiRef.current.unstable_applyPipeProcessors('isColumnPinned', false, colDefRef.current.field);
303
+ leftPinnedCellsAfterRef.current = pinnedPosition !== GridPinnedColumnPosition.LEFT ? [] : findLeftPinnedCellsAfterCol(apiRef.current, columnHeaderElementRef.current);
304
+ rightPinnedCellsBeforeRef.current = pinnedPosition !== GridPinnedColumnPosition.RIGHT ? [] : findRightPinnedCellsBeforeCol(apiRef.current, columnHeaderElementRef.current);
305
+ leftPinnedHeadersAfterRef.current = pinnedPosition !== GridPinnedColumnPosition.LEFT ? [] : findLeftPinnedHeadersAfterCol(apiRef.current, columnHeaderElementRef.current);
306
+ rightPinnedHeadersBeforeRef.current = pinnedPosition !== GridPinnedColumnPosition.RIGHT ? [] : findRightPinnedHeadersBeforeCol(apiRef.current, columnHeaderElementRef.current);
307
+ resizeDirection.current = getResizeDirection(separator, theme.direction);
308
+ initialOffsetToSeparator.current = computeOffsetToSeparator(xStart, columnHeaderElementRef.current.getBoundingClientRect(), resizeDirection.current);
309
+ };
310
+ const handleResizeMouseUp = useEventCallback(finishResize);
311
+ const handleResizeMouseMove = useEventCallback(nativeEvent => {
312
+ // Cancel move in case some other element consumed a mouseup event and it was not fired.
313
+ if (nativeEvent.buttons === 0) {
314
+ handleResizeMouseUp(nativeEvent);
315
+ return;
316
+ }
317
+ let newWidth = computeNewWidth(initialOffsetToSeparator.current, nativeEvent.clientX, columnHeaderElementRef.current.getBoundingClientRect(), resizeDirection.current);
318
+ newWidth = clamp(newWidth, colDefRef.current.minWidth, colDefRef.current.maxWidth);
319
+ updateWidth(newWidth);
320
+ const params = {
321
+ element: columnHeaderElementRef.current,
322
+ colDef: colDefRef.current,
323
+ width: newWidth
324
+ };
325
+ apiRef.current.publishEvent('columnResize', params, nativeEvent);
326
+ });
327
+ const handleTouchEnd = useEventCallback(nativeEvent => {
328
+ const finger = trackFinger(nativeEvent, touchId.current);
329
+ if (!finger) {
330
+ return;
331
+ }
332
+ finishResize(nativeEvent);
333
+ });
334
+ const handleTouchMove = useEventCallback(nativeEvent => {
335
+ const finger = trackFinger(nativeEvent, touchId.current);
336
+ if (!finger) {
337
+ return;
338
+ }
339
+
340
+ // Cancel move in case some other element consumed a touchmove event and it was not fired.
341
+ if (nativeEvent.type === 'mousemove' && nativeEvent.buttons === 0) {
342
+ handleTouchEnd(nativeEvent);
343
+ return;
344
+ }
345
+ let newWidth = computeNewWidth(initialOffsetToSeparator.current, finger.x, columnHeaderElementRef.current.getBoundingClientRect(), resizeDirection.current);
346
+ newWidth = clamp(newWidth, colDefRef.current.minWidth, colDefRef.current.maxWidth);
347
+ updateWidth(newWidth);
348
+ const params = {
349
+ element: columnHeaderElementRef.current,
350
+ colDef: colDefRef.current,
351
+ width: newWidth
352
+ };
353
+ apiRef.current.publishEvent('columnResize', params, nativeEvent);
354
+ });
355
+ const handleTouchStart = useEventCallback(event => {
356
+ const cellSeparator = findParentElementFromClassName(event.target, gridClasses['columnSeparator--resizable']);
357
+ // Let the event bubble if the target is not a col separator
358
+ if (!cellSeparator) {
359
+ return;
360
+ }
361
+ // If touch-action: none; is not supported we need to prevent the scroll manually.
362
+ if (!doesSupportTouchActionNone()) {
363
+ event.preventDefault();
364
+ }
365
+ const touch = event.changedTouches[0];
366
+ if (touch != null) {
367
+ // A number that uniquely identifies the current finger in the touch session.
368
+ touchId.current = touch.identifier;
369
+ }
370
+ const columnHeaderElement = findParentElementFromClassName(event.target, gridClasses.columnHeader);
371
+ const field = getFieldFromHeaderElem(columnHeaderElement);
372
+ const colDef = apiRef.current.getColumn(field);
373
+ logger.debug(`Start Resize on col ${colDef.field}`);
374
+ apiRef.current.publishEvent('columnResizeStart', {
375
+ field
376
+ }, event);
377
+ storeReferences(colDef, cellSeparator, touch.clientX);
378
+ const doc = ownerDocument(event.currentTarget);
379
+ doc.addEventListener('touchmove', handleTouchMove);
380
+ doc.addEventListener('touchend', handleTouchEnd);
381
+ });
382
+ const stopListening = React.useCallback(() => {
383
+ const doc = ownerDocument(apiRef.current.rootElementRef.current);
384
+ doc.body.style.removeProperty('cursor');
385
+ doc.removeEventListener('mousemove', handleResizeMouseMove);
386
+ doc.removeEventListener('mouseup', handleResizeMouseUp);
387
+ doc.removeEventListener('touchmove', handleTouchMove);
388
+ doc.removeEventListener('touchend', handleTouchEnd);
389
+ // The click event runs right after the mouseup event, we want to wait until it
390
+ // has been canceled before removing our handler.
391
+ setTimeout(() => {
392
+ doc.removeEventListener('click', preventClick, true);
393
+ }, 100);
394
+ if (columnHeaderElementRef.current) {
395
+ columnHeaderElementRef.current.style.pointerEvents = 'unset';
396
+ }
397
+ }, [apiRef, columnHeaderElementRef, handleResizeMouseMove, handleResizeMouseUp, handleTouchMove, handleTouchEnd]);
398
+ const handleResizeStart = React.useCallback(({
399
+ field
400
+ }) => {
401
+ apiRef.current.setState(state => _extends({}, state, {
402
+ columnResize: _extends({}, state.columnResize, {
403
+ resizingColumnField: field
404
+ })
405
+ }));
406
+ apiRef.current.forceUpdate();
407
+ }, [apiRef]);
408
+ const handleResizeStop = React.useCallback(() => {
409
+ apiRef.current.setState(state => _extends({}, state, {
410
+ columnResize: _extends({}, state.columnResize, {
411
+ resizingColumnField: ''
412
+ })
413
+ }));
414
+ apiRef.current.forceUpdate();
415
+ }, [apiRef]);
416
+ const handleColumnResizeMouseDown = useEventCallback(({
417
+ colDef
418
+ }, event) => {
419
+ // Only handle left clicks
420
+ if (event.button !== 0) {
421
+ return;
422
+ }
423
+
424
+ // Skip if the column isn't resizable
425
+ if (!event.currentTarget.classList.contains(gridClasses['columnSeparator--resizable'])) {
426
+ return;
427
+ }
428
+
429
+ // Avoid text selection
430
+ event.preventDefault();
431
+ logger.debug(`Start Resize on col ${colDef.field}`);
432
+ apiRef.current.publishEvent('columnResizeStart', {
433
+ field: colDef.field
434
+ }, event);
435
+ storeReferences(colDef, event.currentTarget, event.clientX);
436
+ const doc = ownerDocument(apiRef.current.rootElementRef.current);
437
+ doc.body.style.cursor = 'col-resize';
438
+ previousMouseClickEvent.current = event.nativeEvent;
439
+ doc.addEventListener('mousemove', handleResizeMouseMove);
440
+ doc.addEventListener('mouseup', handleResizeMouseUp);
441
+
442
+ // Prevent the click event if we have resized the column.
443
+ // Fixes https://github.com/mui/mui-x/issues/4777
444
+ doc.addEventListener('click', preventClick, true);
445
+ });
446
+ const handleColumnSeparatorDoubleClick = useEventCallback((params, event) => {
447
+ if (props.disableAutosize) {
448
+ return;
449
+ }
450
+
451
+ // Only handle left clicks
452
+ if (event.button !== 0) {
453
+ return;
454
+ }
455
+ const column = apiRef.current.state.columns.lookup[params.field];
456
+ if (column.resizable === false) {
457
+ return;
458
+ }
459
+ apiRef.current.autosizeColumns(_extends({}, props.autosizeOptions, {
460
+ columns: [column.field]
461
+ }));
462
+ });
463
+
464
+ /**
465
+ * API METHODS
466
+ */
467
+
468
+ const columnVirtualizationDisabled = useColumnVirtualizationDisabled(apiRef);
469
+ const isAutosizingRef = React.useRef(false);
470
+ const autosizeColumns = React.useCallback(async userOptions => {
471
+ const root = apiRef.current.rootElementRef?.current;
472
+ if (!root) {
473
+ return;
474
+ }
475
+ if (isAutosizingRef.current) {
476
+ return;
477
+ }
478
+ isAutosizingRef.current = true;
479
+ const state = gridColumnsStateSelector(apiRef.current.state);
480
+ const options = _extends({}, DEFAULT_GRID_AUTOSIZE_OPTIONS, userOptions, {
481
+ columns: userOptions?.columns ?? state.orderedFields
482
+ });
483
+ options.columns = options.columns.filter(c => state.columnVisibilityModel[c] !== false);
484
+ const columns = options.columns.map(c => apiRef.current.state.columns.lookup[c]);
485
+ try {
486
+ apiRef.current.unstable_setColumnVirtualization(false);
487
+ await columnVirtualizationDisabled();
488
+ const widthByField = extractColumnWidths(apiRef, options, columns);
489
+ const newColumns = columns.map(column => _extends({}, column, {
490
+ width: widthByField[column.field],
491
+ computedWidth: widthByField[column.field]
492
+ }));
493
+ if (options.expand) {
494
+ const visibleColumns = state.orderedFields.map(field => state.lookup[field]).filter(c => state.columnVisibilityModel[c.field] !== false);
495
+ const totalWidth = visibleColumns.reduce((total, column) => total + (widthByField[column.field] ?? column.computedWidth ?? column.width), 0);
496
+ const availableWidth = apiRef.current.getRootDimensions().viewportInnerSize.width;
497
+ const remainingWidth = availableWidth - totalWidth;
498
+ if (remainingWidth > 0) {
499
+ const widthPerColumn = remainingWidth / (newColumns.length || 1);
500
+ newColumns.forEach(column => {
501
+ column.width += widthPerColumn;
502
+ column.computedWidth += widthPerColumn;
503
+ });
504
+ }
505
+ }
506
+ apiRef.current.updateColumns(newColumns);
507
+ newColumns.forEach((newColumn, index) => {
508
+ if (newColumn.width !== columns[index].width) {
509
+ const width = newColumn.width;
510
+ apiRef.current.publishEvent('columnWidthChange', {
511
+ element: apiRef.current.getColumnHeaderElement(newColumn.field),
512
+ colDef: newColumn,
513
+ width
514
+ });
515
+ }
516
+ });
517
+ } finally {
518
+ apiRef.current.unstable_setColumnVirtualization(true);
519
+ isAutosizingRef.current = false;
520
+ }
521
+ }, [apiRef, columnVirtualizationDisabled]);
522
+
523
+ /**
524
+ * EFFECTS
525
+ */
526
+
527
+ React.useEffect(() => stopListening, [stopListening]);
528
+ useOnMount(() => {
529
+ if (props.autosizeOnMount) {
530
+ Promise.resolve().then(() => {
531
+ apiRef.current.autosizeColumns(props.autosizeOptions);
532
+ });
533
+ }
534
+ });
535
+ useGridNativeEventListener(apiRef, () => apiRef.current.columnHeadersContainerRef?.current, 'touchstart', handleTouchStart, {
536
+ passive: doesSupportTouchActionNone()
537
+ });
538
+ useGridApiMethod(apiRef, {
539
+ autosizeColumns
540
+ }, 'public');
541
+ useGridApiEventHandler(apiRef, 'columnResizeStop', handleResizeStop);
542
+ useGridApiEventHandler(apiRef, 'columnResizeStart', handleResizeStart);
543
+ useGridApiEventHandler(apiRef, 'columnSeparatorMouseDown', handleColumnResizeMouseDown);
544
+ useGridApiEventHandler(apiRef, 'columnSeparatorDoubleClick', handleColumnSeparatorDoubleClick);
545
+ useGridApiOptionHandler(apiRef, 'columnResize', props.onColumnResize);
546
+ useGridApiOptionHandler(apiRef, 'columnWidthChange', props.onColumnWidthChange);
547
+ };
548
+ function updateProperty(element, property, delta) {
549
+ if (!element) {
550
+ return;
551
+ }
552
+ element.style[property] = `${parseInt(element.style[property], 10) + delta}px`;
553
+ }
@@ -3,6 +3,7 @@ import { DEFAULT_GRID_COL_TYPE_KEY, GRID_STRING_COL_DEF, getGridDefaultColumnTyp
3
3
  import { gridColumnsStateSelector, gridColumnVisibilityModelSelector } from './gridColumnsSelector';
4
4
  import { clamp } from '../../../utils/utils';
5
5
  import { gridDensityFactorSelector } from '../density/densitySelector';
6
+ import { gridHeaderFilteringEnabledSelector } from '../headerFiltering/gridHeaderFilteringSelectors';
6
7
  import { gridColumnGroupsHeaderMaxDepthSelector } from '../columnGrouping/gridColumnGroupsSelector';
7
8
  export const COLUMNS_DIMENSION_PROPERTIES = ['maxWidth', 'minWidth', 'width', 'flex'];
8
9
  const COLUMN_TYPES = getGridDefaultColumnTypes();
@@ -303,27 +304,10 @@ export function getFirstNonSpannedColumnToRender({
303
304
  }
304
305
  return firstNonSpannedColumnToRender;
305
306
  }
306
- export function getFirstColumnIndexToRender({
307
- firstColumnIndex,
308
- minColumnIndex,
309
- columnBuffer,
310
- firstRowToRender,
311
- lastRowToRender,
312
- apiRef,
313
- visibleRows
314
- }) {
315
- const initialFirstColumnToRender = Math.max(firstColumnIndex - columnBuffer, minColumnIndex);
316
- const firstColumnToRender = getFirstNonSpannedColumnToRender({
317
- firstColumnToRender: initialFirstColumnToRender,
318
- apiRef,
319
- firstRowToRender,
320
- lastRowToRender,
321
- visibleRows
322
- });
323
- return firstColumnToRender;
324
- }
325
307
  export function getTotalHeaderHeight(apiRef, headerHeight) {
326
308
  const densityFactor = gridDensityFactorSelector(apiRef);
327
309
  const maxDepth = gridColumnGroupsHeaderMaxDepthSelector(apiRef);
328
- return Math.floor(headerHeight * densityFactor) * ((maxDepth ?? 0) + 1);
310
+ const isHeaderFilteringEnabled = gridHeaderFilteringEnabledSelector(apiRef);
311
+ const multiplicationFactor = isHeaderFilteringEnabled ? 2 : 1;
312
+ return Math.floor(headerHeight * densityFactor) * ((maxDepth ?? 0) + multiplicationFactor);
329
313
  }
@@ -1,4 +1,10 @@
1
1
  import { createSelector } from '../../../utils/createSelector';
2
+ export const COMPACT_DENSITY_FACTOR = 0.7;
3
+ export const COMFORTABLE_DENSITY_FACTOR = 1.3;
4
+ const DENSITY_FACTORS = {
5
+ compact: COMPACT_DENSITY_FACTOR,
6
+ comfortable: COMFORTABLE_DENSITY_FACTOR,
7
+ standard: 1
8
+ };
2
9
  export const gridDensitySelector = state => state.density;
3
- export const gridDensityValueSelector = createSelector(gridDensitySelector, density => density.value);
4
- export const gridDensityFactorSelector = createSelector(gridDensitySelector, density => density.factor);
10
+ export const gridDensityFactorSelector = createSelector(gridDensitySelector, density => DENSITY_FACTORS[density]);
@@ -1,43 +1,35 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import * as React from 'react';
3
+ import useEventCallback from '@mui/utils/useEventCallback';
3
4
  import { useGridLogger } from '../../utils/useGridLogger';
4
5
  import { useGridApiMethod } from '../../utils/useGridApiMethod';
5
6
  import { gridDensitySelector } from './densitySelector';
6
- import { isDeepEqual } from '../../../utils/utils';
7
- export const COMPACT_DENSITY_FACTOR = 0.7;
8
- export const COMFORTABLE_DENSITY_FACTOR = 1.3;
9
- const DENSITY_FACTORS = {
10
- compact: COMPACT_DENSITY_FACTOR,
11
- comfortable: COMFORTABLE_DENSITY_FACTOR,
12
- standard: 1
13
- };
14
7
  export const densityStateInitializer = (state, props) => _extends({}, state, {
15
- density: {
16
- value: props.density,
17
- factor: DENSITY_FACTORS[props.density]
18
- }
8
+ density: props.initialState?.density ?? props.density ?? 'standard'
19
9
  });
20
10
  export const useGridDensity = (apiRef, props) => {
21
11
  const logger = useGridLogger(apiRef, 'useDensity');
22
- const setDensity = React.useCallback(newDensity => {
12
+ apiRef.current.registerControlState({
13
+ stateId: 'density',
14
+ propModel: props.density,
15
+ propOnChange: props.onDensityChange,
16
+ stateSelector: gridDensitySelector,
17
+ changeEvent: 'densityChange'
18
+ });
19
+ const setDensity = useEventCallback(newDensity => {
20
+ const currentDensity = gridDensitySelector(apiRef.current.state);
21
+ if (currentDensity === newDensity) {
22
+ return;
23
+ }
23
24
  logger.debug(`Set grid density to ${newDensity}`);
24
- apiRef.current.setState(state => {
25
- const currentDensityState = gridDensitySelector(state);
26
- const newDensityState = {
27
- value: newDensity,
28
- factor: DENSITY_FACTORS[newDensity]
29
- };
30
- if (isDeepEqual(currentDensityState, newDensityState)) {
31
- return state;
32
- }
33
- return _extends({}, state, {
34
- density: newDensityState
35
- });
36
- });
37
- apiRef.current.forceUpdate();
38
- }, [logger, apiRef]);
25
+ apiRef.current.setState(state => _extends({}, state, {
26
+ density: newDensity
27
+ }));
28
+ });
39
29
  React.useEffect(() => {
40
- apiRef.current.setDensity(props.density);
30
+ if (props.density) {
31
+ apiRef.current.setDensity(props.density);
32
+ }
41
33
  }, [apiRef, props.density]);
42
34
  const densityApi = {
43
35
  setDensity