@mui/x-data-grid-premium 6.0.0-alpha.11 → 6.0.0-alpha.12

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 (60) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/DataGridPremium/DataGridPremium.js +22 -7
  3. package/DataGridPremium/useDataGridPremiumComponent.js +3 -0
  4. package/DataGridPremium/useDataGridPremiumProps.js +1 -0
  5. package/hooks/features/aggregation/gridAggregationSelectors.d.ts +9 -0
  6. package/hooks/features/aggregation/gridAggregationSelectors.js +11 -0
  7. package/hooks/features/aggregation/useGridAggregationPreProcessors.js +5 -4
  8. package/hooks/features/cellSelection/gridCellSelectionInterfaces.d.ts +40 -0
  9. package/hooks/features/cellSelection/gridCellSelectionInterfaces.js +1 -0
  10. package/hooks/features/cellSelection/gridCellSelectionSelector.d.ts +2 -0
  11. package/hooks/features/cellSelection/gridCellSelectionSelector.js +1 -0
  12. package/hooks/features/cellSelection/index.d.ts +1 -0
  13. package/hooks/features/cellSelection/index.js +1 -0
  14. package/hooks/features/cellSelection/useGridCellSelection.d.ts +6 -0
  15. package/hooks/features/cellSelection/useGridCellSelection.js +295 -0
  16. package/hooks/features/index.d.ts +1 -0
  17. package/hooks/features/index.js +2 -1
  18. package/index.js +1 -1
  19. package/legacy/DataGridPremium/DataGridPremium.js +22 -7
  20. package/legacy/DataGridPremium/useDataGridPremiumComponent.js +3 -0
  21. package/legacy/DataGridPremium/useDataGridPremiumProps.js +1 -0
  22. package/legacy/hooks/features/aggregation/gridAggregationSelectors.js +11 -0
  23. package/legacy/hooks/features/aggregation/useGridAggregationPreProcessors.js +5 -2
  24. package/legacy/hooks/features/cellSelection/gridCellSelectionInterfaces.js +1 -0
  25. package/legacy/hooks/features/cellSelection/gridCellSelectionSelector.js +3 -0
  26. package/legacy/hooks/features/cellSelection/index.js +1 -0
  27. package/legacy/hooks/features/cellSelection/useGridCellSelection.js +287 -0
  28. package/legacy/hooks/features/index.js +2 -1
  29. package/legacy/index.js +1 -1
  30. package/legacy/utils/releaseInfo.js +1 -1
  31. package/models/dataGridPremiumProps.d.ts +17 -1
  32. package/models/gridApiPremium.d.ts +3 -2
  33. package/models/gridStatePremium.d.ts +3 -1
  34. package/modern/DataGridPremium/DataGridPremium.js +22 -7
  35. package/modern/DataGridPremium/useDataGridPremiumComponent.js +3 -0
  36. package/modern/DataGridPremium/useDataGridPremiumProps.js +1 -0
  37. package/modern/hooks/features/aggregation/gridAggregationSelectors.js +11 -0
  38. package/modern/hooks/features/aggregation/useGridAggregationPreProcessors.js +5 -4
  39. package/modern/hooks/features/cellSelection/gridCellSelectionInterfaces.js +1 -0
  40. package/modern/hooks/features/cellSelection/gridCellSelectionSelector.js +1 -0
  41. package/modern/hooks/features/cellSelection/index.js +1 -0
  42. package/modern/hooks/features/cellSelection/useGridCellSelection.js +290 -0
  43. package/modern/hooks/features/index.js +2 -1
  44. package/modern/index.js +1 -1
  45. package/modern/utils/releaseInfo.js +1 -1
  46. package/node/DataGridPremium/DataGridPremium.js +21 -6
  47. package/node/DataGridPremium/useDataGridPremiumComponent.js +3 -0
  48. package/node/DataGridPremium/useDataGridPremiumProps.js +1 -0
  49. package/node/hooks/features/aggregation/gridAggregationSelectors.js +11 -0
  50. package/node/hooks/features/aggregation/useGridAggregationPreProcessors.js +5 -4
  51. package/node/hooks/features/cellSelection/gridCellSelectionInterfaces.js +5 -0
  52. package/node/hooks/features/cellSelection/gridCellSelectionSelector.js +8 -0
  53. package/node/hooks/features/cellSelection/index.js +16 -0
  54. package/node/hooks/features/cellSelection/useGridCellSelection.js +301 -0
  55. package/node/hooks/features/index.js +11 -0
  56. package/node/index.js +1 -1
  57. package/node/utils/releaseInfo.js +1 -1
  58. package/package.json +4 -4
  59. package/typeOverloads/modules.d.ts +7 -1
  60. package/utils/releaseInfo.js +1 -1
package/CHANGELOG.md CHANGED
@@ -3,6 +3,80 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 6.0.0-alpha.12
7
+
8
+ _Dec 16, 2022_
9
+
10
+ We'd like to offer a big thanks to the 6 contributors who made this release possible. Here are some highlights ✨:
11
+
12
+ - 🚀 The `apiRef` prop is now available in the `@mui/x-data-grid` package:
13
+
14
+ ```tsx
15
+ const apiRef = useGridApiRef();
16
+
17
+ return (
18
+ <DataGrid apiRef={apiRef} {...other} />
19
+ )
20
+ ```
21
+
22
+ See [the documentation](https://next.mui.com/x/react-data-grid/api-object/) for more information.
23
+
24
+ - 🎁 The `DataGridPremium` now supports cell selection:
25
+
26
+ ```tsx
27
+ <DataGridPremium unstable_cellSelection />
28
+ ```
29
+
30
+ See [the documentation](https://next.mui.com/x/react-data-grid/selection/#cell-selection) for more information
31
+
32
+ - 🌍 Support the Right To Left orientation on the fields components
33
+ - 📚 Documentation improvements
34
+ - 🐞 Bugfixes
35
+
36
+ ### `@mui/x-data-grid@v6.0.0-alpha.12` / `@mui/x-data-grid-pro@v6.0.0-alpha.12` / `@mui/x-data-grid-premium@v6.0.0-alpha.12`
37
+
38
+ #### Breaking changes
39
+
40
+ - The `showCellRightBorder` was renamed to `showCellVerticalBorder`
41
+ - The `showColumnRightBorder` was renamed to `showColumnVerticalBorder`
42
+ - The `.MuiDataGrid-withBorder` CSS class was renamed to `.MuiDataGrid-withBorderColor` and it only sets `border-color` CSS property now.
43
+ - The following undocumented properties from `apiRef` were removed: `footerRef`, `headerRef`, `columnHeadersElementRef`, `columnHeadersContainerElementRef`
44
+ - The `GridHeaderPlaceholder` component was removed.
45
+ - The `MAX_PAGE_SIZE` constant was removed.
46
+ - The `useGridScrollFn` hook was removed.
47
+
48
+ #### Changes
49
+
50
+ - [DataGrid] Display sort column menu items as per `sortingOrder` prop (#7180) @MBilalShafi
51
+ - [DataGrid] Support `apiRef` in Community package (#6773) @cherniavskii
52
+ - [DataGridPremium] Add support for cell selection (#6567) @m4theushw
53
+ - [DataGridPremium] Use separate cache for aggregation columns pre-processor (#7142) @m4theushw
54
+ - [DataGridPro] Fix missing border in right-pinned columns (#4197) @cherniavskii
55
+ - [DataGridPro] Fix wrong border color on skeleton cells (#7202) @cherniavskii
56
+
57
+ ### `@mui/x-date-pickers@v6.0.0-alpha.12` / `@mui/x-date-pickers-pro@v6.0.0-alpha.12`
58
+
59
+ #### Changes
60
+
61
+ - [fields] Fix bug introduced by RTL in single input range fields (#7189) @alexfauquette
62
+ - [fields] Support RTL out of the box (#6715) @alexfauquette
63
+ - [pickers] Clean `autoFocus` behavior on fields and new pickers (#7153) @flaviendelangle
64
+ - [pickers] Fix label on the new range pickers (#7210) @flaviendelangle
65
+ - [pickers] Fix wrong component name on `StaticNextDateTime` (#7187) @flaviendelangle
66
+
67
+ ### Docs
68
+
69
+ - [docs] Add docs section about field placeholders' localization (#7139) @flaviendelangle
70
+ - [docs] Create a `DemoGrid` component to unify demos with several components (#7057) @flaviendelangle
71
+ - [docs] Document aggregation selectors (#7148) @cherniavskii
72
+ - [docs] Fix 301 links to demo pages in API pages (#7197) @oliviertassinari
73
+ - [docs] Fix errors and warning in demos (#7209) @LukasTy
74
+ - [docs] Use `DemoContainer` and `DemoItem` on every picker demo (#7149) @flaviendelangle
75
+
76
+ ### Core
77
+
78
+ - [core] Fix broken test (#7179) @flaviendelangle
79
+
6
80
  ## 6.0.0-alpha.11
7
81
 
8
82
  _Dec 8, 2022_
@@ -1301,6 +1375,60 @@ You can find more information about the new api, including how to set those tran
1301
1375
  - [test] Skip tests for column pinning and dynamic row height (#5997) @m4theushw
1302
1376
  - [website] Improve security header @oliviertassinari
1303
1377
 
1378
+ ## 5.17.16
1379
+
1380
+ _Dec 16, 2022_
1381
+
1382
+ We'd like to offer a big thanks to the 4 contributors who made this release possible. Here are some highlights ✨:
1383
+
1384
+ - 🐞 Bugfixes
1385
+
1386
+ ### `@mui/x-data-grid@v5.17.16` / `@mui/x-data-grid-pro@v5.17.16` / `@mui/x-data-grid-premium@v5.17.16`
1387
+
1388
+ #### Changes
1389
+
1390
+ - [DataGrid] Display sort column menu items as per `sortingOrder` prop (#7125) @hanbin9775
1391
+ - [DataGrid] Fix flickering on mount (#7155) @cherniavskii
1392
+ - [DataGridPremium] Use separate cache for aggregation columns pre-processor (#7174) @m4theushw
1393
+
1394
+ ### `@mui/x-date-pickers@v5.0.11` / `@mui/x-date-pickers-pro@v5.0.11`
1395
+
1396
+ #### Changes
1397
+
1398
+ - [DateTimePicker] Update export pattern (#7172) @kealjones-wk
1399
+
1400
+ ### Docs
1401
+
1402
+ - [docs] Document aggregation selectors (#7151) @cherniavskii
1403
+
1404
+ ## 5.17.15
1405
+
1406
+ _Dec 8, 2022_
1407
+
1408
+ We'd like to offer a big thanks to the 4 contributors who made this release possible. Here are some highlights ✨:
1409
+
1410
+ - ✨ Fix lazy-loading not working in `DataGridPremium` (#7130) @m4theushw
1411
+ - 🐞 Bugfixes
1412
+
1413
+ ### `@mui/x-data-grid@v5.17.15` / `@mui/x-data-grid-pro@v5.17.15` / `@mui/x-data-grid-premium@v5.17.15`
1414
+
1415
+ #### Changes
1416
+
1417
+ - [DataGridPremium] Add support for lazy-loading (#7130) @m4theushw
1418
+ - [DataGridPremium] Pass `groupId` to the aggregation function (#7143) @m4theushw
1419
+
1420
+ ### `@mui/x-date-pickers@v5.0.10` / `@mui/x-date-pickers-pro@v5.0.10`
1421
+
1422
+ #### Changes
1423
+
1424
+ - [pickers] Initialize date without time when selecting year or month (#7136) @LukasTy
1425
+
1426
+ ### Docs
1427
+
1428
+ - [docs] Fix the nested import on the api pages (#7134) @flaviendelangle
1429
+ - [docs] Keep track of the localization completion (#7099) @alexfauquette
1430
+ - [docs] Update localization doc to use existing locale (#7104) @LukasTy
1431
+
1304
1432
  ## 5.17.14
1305
1433
 
1306
1434
  _Dec 1, 2022_
@@ -2,7 +2,7 @@ import * as React from 'react';
2
2
  import PropTypes from 'prop-types';
3
3
  import { useLicenseVerifier, Watermark } from '@mui/x-license-pro';
4
4
  import { chainPropTypes } from '@mui/utils';
5
- import { GridBody, GridErrorHandler, GridFooterPlaceholder, GridHeaderPlaceholder, GridRoot, GridContextProvider } from '@mui/x-data-grid-pro';
5
+ import { GridBody, GridErrorHandler, GridFooterPlaceholder, GridHeader, GridRoot, GridContextProvider } from '@mui/x-data-grid-pro';
6
6
  import { DataGridProVirtualScroller, DataGridProColumnHeaders } from '@mui/x-data-grid-pro/internals';
7
7
  import { useDataGridPremiumComponent } from './useDataGridPremiumComponent';
8
8
  import { useDataGridPremiumProps } from './useDataGridPremiumProps';
@@ -23,7 +23,7 @@ const DataGridPremiumRaw = /*#__PURE__*/React.forwardRef(function DataGridPremiu
23
23
  sx: props.sx,
24
24
  ref: ref,
25
25
  children: /*#__PURE__*/_jsxs(GridErrorHandler, {
26
- children: [/*#__PURE__*/_jsx(GridHeaderPlaceholder, {}), /*#__PURE__*/_jsx(GridBody, {
26
+ children: [/*#__PURE__*/_jsx(GridHeader, {}), /*#__PURE__*/_jsx(GridBody, {
27
27
  ColumnHeadersComponent: DataGridProColumnHeaders,
28
28
  VirtualScrollerComponent: DataGridProVirtualScroller,
29
29
  children: /*#__PURE__*/_jsx(Watermark, {
@@ -58,7 +58,7 @@ process.env.NODE_ENV !== "production" ? DataGridPremiumRaw.propTypes = {
58
58
  */
59
59
  aggregationRowsScope: PropTypes.oneOf(['all', 'filtered']),
60
60
  /**
61
- * The ref object that allows grid manipulation. Can be instantiated with [[useGridApiRef()]].
61
+ * The ref object that allows grid manipulation. Can be instantiated with `useGridApiRef()`.
62
62
  */
63
63
  apiRef: PropTypes.shape({
64
64
  current: PropTypes.object.isRequired
@@ -865,15 +865,15 @@ process.env.NODE_ENV !== "production" ? DataGridPremiumRaw.propTypes = {
865
865
  */
866
866
  scrollEndThreshold: PropTypes.number,
867
867
  /**
868
- * If `true`, the right border of the cells are displayed.
868
+ * If `true`, the vertical borders of the cells are displayed.
869
869
  * @default false
870
870
  */
871
- showCellRightBorder: PropTypes.bool,
871
+ showCellVerticalBorder: PropTypes.bool,
872
872
  /**
873
873
  * If `true`, the right border of the column headers are displayed.
874
874
  * @default false
875
875
  */
876
- showColumnRightBorder: PropTypes.bool,
876
+ showColumnVerticalBorder: PropTypes.bool,
877
877
  /**
878
878
  * Sorting can be processed on the server or client-side.
879
879
  * Set it to 'client' if you would like to handle sorting on the client-side.
@@ -907,5 +907,20 @@ process.env.NODE_ENV !== "production" ? DataGridPremiumRaw.propTypes = {
907
907
  * If `true`, the rows will be gathered in a tree structure according to the `getTreeDataPath` prop.
908
908
  * @default false
909
909
  */
910
- treeData: PropTypes.bool
910
+ treeData: PropTypes.bool,
911
+ /**
912
+ * If `true`, the cell selection mode is enabled.
913
+ * @default false
914
+ */
915
+ unstable_cellSelection: PropTypes.bool,
916
+ /**
917
+ * Set the cell selection model of the grid.
918
+ */
919
+ unstable_cellSelectionModel: PropTypes.object,
920
+ /**
921
+ * Callback fired when the selection state of one or multiple cells changes.
922
+ * @param {GridCellSelectionModel} cellSelectionModel Object in the shape of [[GridCellSelectionModel]] containg the selected cells.
923
+ * @param {GridCallbackDetails} details Additional details for this callback.
924
+ */
925
+ unstable_onCellSelectionModelChange: PropTypes.func
911
926
  } : void 0;
@@ -5,6 +5,7 @@ import { useGridAggregationPreProcessors } from '../hooks/features/aggregation/u
5
5
  import { useGridRowGrouping, rowGroupingStateInitializer } from '../hooks/features/rowGrouping/useGridRowGrouping';
6
6
  import { useGridRowGroupingPreProcessors } from '../hooks/features/rowGrouping/useGridRowGroupingPreProcessors';
7
7
  import { useGridExcelExport } from '../hooks/features/export/useGridExcelExport';
8
+ import { cellSelectionStateInitializer, useGridCellSelection } from '../hooks/features/cellSelection/useGridCellSelection';
8
9
  export const useDataGridPremiumComponent = (inputApiRef, props) => {
9
10
  const privateApiRef = useGridInitialization(inputApiRef, props);
10
11
 
@@ -30,6 +31,7 @@ export const useDataGridPremiumComponent = (inputApiRef, props) => {
30
31
  useGridInitializeState(rowGroupingStateInitializer, privateApiRef, props);
31
32
  useGridInitializeState(aggregationStateInitializer, privateApiRef, props);
32
33
  useGridInitializeState(rowSelectionStateInitializer, privateApiRef, props);
34
+ useGridInitializeState(cellSelectionStateInitializer, privateApiRef, props);
33
35
  useGridInitializeState(detailPanelStateInitializer, privateApiRef, props);
34
36
  useGridInitializeState(columnPinningStateInitializer, privateApiRef, props);
35
37
  useGridInitializeState(columnsStateInitializer, privateApiRef, props);
@@ -52,6 +54,7 @@ export const useDataGridPremiumComponent = (inputApiRef, props) => {
52
54
  useGridAggregation(privateApiRef, props);
53
55
  useGridKeyboardNavigation(privateApiRef, props);
54
56
  useGridRowSelection(privateApiRef, props);
57
+ useGridCellSelection(privateApiRef, props);
55
58
  useGridColumnPinning(privateApiRef, props);
56
59
  useGridRowPinning(privateApiRef, props);
57
60
  useGridColumns(privateApiRef, props);
@@ -8,6 +8,7 @@ import { GRID_AGGREGATION_FUNCTIONS } from '../hooks/features/aggregation';
8
8
  * The default values of `DataGridPremiumPropsWithDefaultValue` to inject in the props of DataGridPremium.
9
9
  */
10
10
  export const DATA_GRID_PREMIUM_PROPS_DEFAULT_VALUES = _extends({}, DATA_GRID_PRO_PROPS_DEFAULT_VALUES, {
11
+ unstable_cellSelection: false,
11
12
  disableAggregation: false,
12
13
  disableRowGrouping: false,
13
14
  rowGroupingColumnMode: 'single',
@@ -1,4 +1,13 @@
1
1
  import { GridStatePremium } from '../../../models/gridStatePremium';
2
2
  export declare const gridAggregationStateSelector: (state: GridStatePremium) => import("./gridAggregationInterfaces").GridAggregationState;
3
+ /**
4
+ * Get the aggregation model, containing the aggregation function of each column.
5
+ * If a column is not in the model, it is not aggregated.
6
+ * @category Aggregation
7
+ */
3
8
  export declare const gridAggregationModelSelector: import("@mui/x-data-grid").OutputSelector<GridStatePremium, import("./gridAggregationInterfaces").GridAggregationModel>;
9
+ /**
10
+ * Get the aggregation results as a lookup.
11
+ * @category Aggregation
12
+ */
4
13
  export declare const gridAggregationLookupSelector: import("@mui/x-data-grid").OutputSelector<GridStatePremium, import("./gridAggregationInterfaces").GridAggregationLookup>;
@@ -1,4 +1,15 @@
1
1
  import { createSelector } from '@mui/x-data-grid-pro/internals';
2
2
  export const gridAggregationStateSelector = state => state.aggregation;
3
+
4
+ /**
5
+ * Get the aggregation model, containing the aggregation function of each column.
6
+ * If a column is not in the model, it is not aggregated.
7
+ * @category Aggregation
8
+ */
3
9
  export const gridAggregationModelSelector = createSelector(gridAggregationStateSelector, aggregationState => aggregationState.model);
10
+
11
+ /**
12
+ * Get the aggregation results as a lookup.
13
+ * @category Aggregation
14
+ */
4
15
  export const gridAggregationLookupSelector = createSelector(gridAggregationStateSelector, aggregationState => aggregationState.lookup);
@@ -14,10 +14,10 @@ function Divider() {
14
14
  });
15
15
  }
16
16
  export const useGridAggregationPreProcessors = (apiRef, props) => {
17
+ // apiRef.current.caches.aggregation.rulesOnLastColumnHydration is not used because by the time
18
+ // that the pre-processor is called it will already have been updated with the current rules.
19
+ const rulesOnLastColumnHydration = React.useRef({});
17
20
  const updateAggregatedColumns = React.useCallback(columnsState => {
18
- const {
19
- rulesOnLastColumnHydration
20
- } = apiRef.current.caches.aggregation;
21
21
  const aggregationRules = props.disableAggregation ? {} : getAggregationRules({
22
22
  columnsLookup: columnsState.lookup,
23
23
  aggregationModel: gridAggregationModelSelector(apiRef),
@@ -25,7 +25,7 @@ export const useGridAggregationPreProcessors = (apiRef, props) => {
25
25
  });
26
26
  columnsState.orderedFields.forEach(field => {
27
27
  const shouldHaveAggregationValue = !!aggregationRules[field];
28
- const haveAggregationColumnValue = !!rulesOnLastColumnHydration[field];
28
+ const haveAggregationColumnValue = !!rulesOnLastColumnHydration.current[field];
29
29
  let column = columnsState.lookup[field];
30
30
  if (haveAggregationColumnValue) {
31
31
  column = unwrapColumnFromAggregation({
@@ -41,6 +41,7 @@ export const useGridAggregationPreProcessors = (apiRef, props) => {
41
41
  }
42
42
  columnsState.lookup[field] = column;
43
43
  });
44
+ rulesOnLastColumnHydration.current = aggregationRules;
44
45
  return columnsState;
45
46
  }, [apiRef, props.aggregationFunctions, props.disableAggregation]);
46
47
  const addGroupFooterRows = React.useCallback(value => {
@@ -0,0 +1,40 @@
1
+ import { GridCellCoordinates, GridColDef, GridRowId } from '@mui/x-data-grid-pro';
2
+ export type GridCellSelectionModel = Record<GridRowId, Record<GridColDef['field'], boolean>>;
3
+ /**
4
+ * The cell selection API interface that is available in the grid [[apiRef]].
5
+ */
6
+ export interface GridCellSelectionApi {
7
+ /**
8
+ * Determines if a cell is selected or not.
9
+ * @param {GridRowId} id The id of the row.
10
+ * @param {GridColDef['field']} field The field.
11
+ * @returns {boolean} A boolean indicating if the cell is selected.
12
+ */
13
+ unstable_isCellSelected: (id: GridRowId, field: GridColDef['field']) => boolean;
14
+ /**
15
+ * Returns an object containing the selection state of the cells.
16
+ * The keys of the object correpond to the row IDs.
17
+ * The value of each key is another object whose keys are the fields and values are the selection state.
18
+ * @returns {GridCellSelectionModel} Object containing the selection state of the cells
19
+ */
20
+ unstable_getCellSelectionModel: () => GridCellSelectionModel;
21
+ /**
22
+ * Updates the selected cells to be those passed to the `newModel` argument.
23
+ * Any cell already selected will be unselected.
24
+ * @param {GridCellSelectionModel} newModel The cells to select.
25
+ */
26
+ unstable_setCellSelectionModel: (newModel: GridCellSelectionModel) => void;
27
+ /**
28
+ * Selects all cells that are inside the range given by `start` and `end` coordinates.
29
+ * @param {GridCellCoordinates} start Object containg the row ID and field of the first cell to select.
30
+ * @param {GridCellCoordinates} end Object containg the row ID and field of the last cell to select.
31
+ * @param {boolean} keepOtherSelected Whether to keep current selected cells or discard. Default is false.
32
+ */
33
+ unstable_selectCellRange: (start: GridCellCoordinates, end: GridCellCoordinates, keepOtherSelected?: boolean) => void;
34
+ /**
35
+ * Returns an array containg only the selected cells.
36
+ * Each item is an object with the ID and field of the cell.
37
+ * @returns {GridCellCoordinates[]} Array with the selected cells.
38
+ */
39
+ unstable_getSelectedCellsAsArray: () => GridCellCoordinates[];
40
+ }
@@ -0,0 +1,2 @@
1
+ import { GridStatePremium } from '../../../models/gridStatePremium';
2
+ export declare const gridCellSelectionStateSelector: (state: GridStatePremium) => import("./gridCellSelectionInterfaces").GridCellSelectionModel;
@@ -0,0 +1 @@
1
+ export const gridCellSelectionStateSelector = state => state.cellSelection;
@@ -0,0 +1 @@
1
+ export * from './gridCellSelectionInterfaces';
@@ -0,0 +1 @@
1
+ export * from './gridCellSelectionInterfaces';
@@ -0,0 +1,6 @@
1
+ import * as React from 'react';
2
+ import { GridStateInitializer } from '@mui/x-data-grid-pro/internals';
3
+ import { DataGridPremiumProcessedProps } from '../../../models/dataGridPremiumProps';
4
+ import { GridPrivateApiPremium } from '../../../models/gridApiPremium';
5
+ export declare const cellSelectionStateInitializer: GridStateInitializer<Pick<DataGridPremiumProcessedProps, 'unstable_cellSelectionModel' | 'initialState'>>;
6
+ export declare const useGridCellSelection: (apiRef: React.MutableRefObject<GridPrivateApiPremium>, props: Pick<DataGridPremiumProcessedProps, 'unstable_cellSelection' | 'unstable_cellSelectionModel' | 'unstable_onCellSelectionModelChange' | 'pagination' | 'paginationMode'>) => void;
@@ -0,0 +1,295 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import * as React from 'react';
3
+ import { useEventCallback } from '@mui/material/utils';
4
+ import { isNavigationKey, useGridRegisterPipeProcessor, useGridVisibleRows } from '@mui/x-data-grid-pro/internals';
5
+ import { useGridApiEventHandler, useGridApiMethod, GRID_ACTIONS_COLUMN_TYPE, GRID_CHECKBOX_SELECTION_COL_DEF, GRID_DETAIL_PANEL_TOGGLE_FIELD, gridRowsDataRowIdToIdLookupSelector, gridClasses } from '@mui/x-data-grid-pro';
6
+ import { gridCellSelectionStateSelector } from './gridCellSelectionSelector';
7
+ export const cellSelectionStateInitializer = (state, props) => {
8
+ var _props$unstable_cellS, _props$initialState;
9
+ return _extends({}, state, {
10
+ cellSelection: _extends({}, (_props$unstable_cellS = props.unstable_cellSelectionModel) != null ? _props$unstable_cellS : (_props$initialState = props.initialState) == null ? void 0 : _props$initialState.cellSelection)
11
+ });
12
+ };
13
+ export const useGridCellSelection = (apiRef, props) => {
14
+ const visibleRows = useGridVisibleRows(apiRef, props);
15
+ const lastClickedCell = React.useRef();
16
+ const lastMouseDownCell = React.useRef();
17
+ const focusedCellWhenShiftWasPressed = React.useRef(null);
18
+ apiRef.current.registerControlState({
19
+ stateId: 'cellSelection',
20
+ propModel: props.unstable_cellSelectionModel,
21
+ propOnChange: props.unstable_onCellSelectionModelChange,
22
+ stateSelector: gridCellSelectionStateSelector,
23
+ changeEvent: 'cellSelectionChange'
24
+ });
25
+ const runIfCellSelectionIsEnabled = callback => (...args) => {
26
+ if (props.unstable_cellSelection) {
27
+ callback(...args);
28
+ }
29
+ };
30
+ const isCellSelected = React.useCallback((id, field) => {
31
+ if (!props.unstable_cellSelection) {
32
+ return false;
33
+ }
34
+ const cellSelectionModel = gridCellSelectionStateSelector(apiRef.current.state);
35
+ return cellSelectionModel[id] ? !!cellSelectionModel[id][field] : false;
36
+ }, [apiRef, props.unstable_cellSelection]);
37
+ const getCellSelectionModel = React.useCallback(() => {
38
+ return gridCellSelectionStateSelector(apiRef.current.state);
39
+ }, [apiRef]);
40
+ const setCellSelectionModel = React.useCallback(newModel => {
41
+ if (!props.unstable_cellSelection) {
42
+ return;
43
+ }
44
+ apiRef.current.setState(prevState => _extends({}, prevState, {
45
+ cellSelection: newModel
46
+ }));
47
+ apiRef.current.forceUpdate();
48
+ }, [apiRef, props.unstable_cellSelection]);
49
+ const selectCellRange = React.useCallback((start, end, keepOtherSelected = false) => {
50
+ const startRowIndex = apiRef.current.getRowIndexRelativeToVisibleRows(start.id);
51
+ const startColumnIndex = apiRef.current.getColumnIndex(start.field);
52
+ const endRowIndex = apiRef.current.getRowIndexRelativeToVisibleRows(end.id);
53
+ const endColumnIndex = apiRef.current.getColumnIndex(end.field);
54
+ let finalStartRowIndex = startRowIndex;
55
+ let finalStartColumnIndex = startColumnIndex;
56
+ let finalEndRowIndex = endRowIndex;
57
+ let finalEndColumnIndex = endColumnIndex;
58
+ if (finalStartRowIndex > finalEndRowIndex) {
59
+ finalStartRowIndex = endRowIndex;
60
+ finalEndRowIndex = startRowIndex;
61
+ }
62
+ if (finalStartColumnIndex > finalEndColumnIndex) {
63
+ finalStartColumnIndex = endColumnIndex;
64
+ finalEndColumnIndex = startColumnIndex;
65
+ }
66
+ const visibleColumns = apiRef.current.getVisibleColumns();
67
+ const rowsInRange = visibleRows.rows.slice(finalStartRowIndex, finalEndRowIndex + 1);
68
+ const columnsInRange = visibleColumns.slice(finalStartColumnIndex, finalEndColumnIndex + 1);
69
+ const newModel = keepOtherSelected ? apiRef.current.unstable_getCellSelectionModel() : {};
70
+ rowsInRange.forEach(row => {
71
+ if (!newModel[row.id]) {
72
+ newModel[row.id] = {};
73
+ }
74
+ columnsInRange.forEach(column => {
75
+ newModel[row.id][column.field] = true;
76
+ }, {});
77
+ });
78
+ apiRef.current.unstable_setCellSelectionModel(newModel);
79
+ }, [apiRef, visibleRows.rows]);
80
+ const getSelectedCellsAsArray = React.useCallback(() => {
81
+ const model = apiRef.current.unstable_getCellSelectionModel();
82
+ const idToIdLookup = gridRowsDataRowIdToIdLookupSelector(apiRef);
83
+ return Object.entries(model).reduce((acc, [id, fields]) => [...acc, ...Object.entries(fields).reduce((acc2, [field, isSelected]) => {
84
+ return isSelected ? [...acc2, {
85
+ id: idToIdLookup[id],
86
+ field
87
+ }] : acc2;
88
+ }, [])], []);
89
+ }, [apiRef]);
90
+ const cellSelectionApi = {
91
+ unstable_isCellSelected: isCellSelected,
92
+ unstable_getCellSelectionModel: getCellSelectionModel,
93
+ unstable_setCellSelectionModel: setCellSelectionModel,
94
+ unstable_selectCellRange: selectCellRange,
95
+ unstable_getSelectedCellsAsArray: getSelectedCellsAsArray
96
+ };
97
+ useGridApiMethod(apiRef, cellSelectionApi, 'public');
98
+ const handleCellMouseDown = React.useCallback((params, event) => {
99
+ var _apiRef$current$rootE, _apiRef$current$rootE2;
100
+ // Skip if the click comes from the right-button or, only on macOS, Ctrl is pressed
101
+ // Fix for https://github.com/mui/mui-x/pull/6567#issuecomment-1329155578
102
+ const isMacOs = window.navigator.platform.toUpperCase().indexOf('MAC') >= 0;
103
+ if (event.button !== 0 || event.ctrlKey && isMacOs) {
104
+ return;
105
+ }
106
+ lastMouseDownCell.current = {
107
+ id: params.id,
108
+ field: params.field
109
+ };
110
+ (_apiRef$current$rootE = apiRef.current.rootElementRef) == null ? void 0 : (_apiRef$current$rootE2 = _apiRef$current$rootE.current) == null ? void 0 : _apiRef$current$rootE2.classList.add(gridClasses['root--disableUserSelection']);
111
+ }, [apiRef]);
112
+ const handleCellMouseUp = React.useCallback(() => {
113
+ var _apiRef$current$rootE3, _apiRef$current$rootE4;
114
+ lastMouseDownCell.current = null;
115
+ (_apiRef$current$rootE3 = apiRef.current.rootElementRef) == null ? void 0 : (_apiRef$current$rootE4 = _apiRef$current$rootE3.current) == null ? void 0 : _apiRef$current$rootE4.classList.remove(gridClasses['root--disableUserSelection']);
116
+ }, [apiRef]);
117
+ const handleCellMouseOver = React.useCallback((params, event) => {
118
+ if (!lastMouseDownCell.current) {
119
+ return;
120
+ }
121
+ const {
122
+ id,
123
+ field
124
+ } = params;
125
+ apiRef.current.unstable_selectCellRange(lastMouseDownCell.current, {
126
+ id,
127
+ field
128
+ }, event.ctrlKey || event.metaKey);
129
+ }, [apiRef]);
130
+ const handleCellClick = useEventCallback((params, event) => {
131
+ const {
132
+ id,
133
+ field
134
+ } = params;
135
+ if (params.field === GRID_CHECKBOX_SELECTION_COL_DEF.field) {
136
+ return;
137
+ }
138
+ if (params.field === GRID_DETAIL_PANEL_TOGGLE_FIELD) {
139
+ return;
140
+ }
141
+ const column = apiRef.current.getColumn(params.field);
142
+ if (column.type === GRID_ACTIONS_COLUMN_TYPE) {
143
+ return;
144
+ }
145
+ if (params.rowNode.type === 'pinnedRow') {
146
+ return;
147
+ }
148
+ if (event.shiftKey && lastClickedCell.current) {
149
+ apiRef.current.unstable_selectCellRange(lastClickedCell.current, {
150
+ id,
151
+ field
152
+ });
153
+ lastClickedCell.current = {
154
+ id,
155
+ field
156
+ };
157
+ return;
158
+ }
159
+ lastClickedCell.current = {
160
+ id,
161
+ field
162
+ };
163
+ if (event.ctrlKey || event.metaKey) {
164
+ // Add the clicked cell to the selection
165
+ const prevModel = apiRef.current.unstable_getCellSelectionModel();
166
+ apiRef.current.unstable_setCellSelectionModel(_extends({}, prevModel, {
167
+ [id]: _extends({}, prevModel[id], {
168
+ [field]: !apiRef.current.unstable_isCellSelected(id, field)
169
+ })
170
+ }));
171
+ } else {
172
+ // Clear the selection and keep only the clicked cell selected
173
+ apiRef.current.unstable_setCellSelectionModel({
174
+ [id]: {
175
+ [field]: true
176
+ }
177
+ });
178
+ }
179
+ });
180
+ const handleCellKeyDown = useEventCallback((params, event) => {
181
+ const {
182
+ id,
183
+ field
184
+ } = params;
185
+ if (event.key === 'Shift') {
186
+ focusedCellWhenShiftWasPressed.current = {
187
+ id,
188
+ field
189
+ };
190
+ return;
191
+ }
192
+ if (!focusedCellWhenShiftWasPressed.current || !isNavigationKey(event.key) || !event.shiftKey) {
193
+ return;
194
+ }
195
+ let endRowIndex = apiRef.current.getRowIndexRelativeToVisibleRows(id);
196
+ let endColumnIndex = apiRef.current.getColumnIndex(field);
197
+ if (event.key === 'ArrowDown') {
198
+ endRowIndex += 1;
199
+ } else if (event.key === 'ArrowUp') {
200
+ endRowIndex -= 1;
201
+ } else if (event.key === 'ArrowRight') {
202
+ endColumnIndex += 1;
203
+ } else if (event.key === 'ArrowLeft') {
204
+ endColumnIndex -= 1;
205
+ }
206
+ if (endRowIndex < 0 || endRowIndex >= visibleRows.rows.length) {
207
+ return;
208
+ }
209
+ const visibleColumns = apiRef.current.getVisibleColumns();
210
+ if (endColumnIndex < 0 || endColumnIndex >= visibleColumns.length) {
211
+ return;
212
+ }
213
+ apiRef.current.unstable_selectCellRange(focusedCellWhenShiftWasPressed.current, {
214
+ id: visibleRows.rows[endRowIndex].id,
215
+ field: visibleColumns[endColumnIndex].field
216
+ });
217
+ });
218
+ const handleCellKeyUp = useEventCallback((params, event) => {
219
+ if (event.key === 'Shift') {
220
+ focusedCellWhenShiftWasPressed.current = null;
221
+ }
222
+ });
223
+ useGridApiEventHandler(apiRef, 'cellClick', runIfCellSelectionIsEnabled(handleCellClick));
224
+ useGridApiEventHandler(apiRef, 'cellKeyDown', runIfCellSelectionIsEnabled(handleCellKeyDown));
225
+ useGridApiEventHandler(apiRef, 'cellKeyUp', runIfCellSelectionIsEnabled(handleCellKeyUp));
226
+ useGridApiEventHandler(apiRef, 'cellMouseDown', runIfCellSelectionIsEnabled(handleCellMouseDown));
227
+ useGridApiEventHandler(apiRef, 'cellMouseUp', runIfCellSelectionIsEnabled(handleCellMouseUp));
228
+ useGridApiEventHandler(apiRef, 'cellMouseOver', runIfCellSelectionIsEnabled(handleCellMouseOver));
229
+ React.useEffect(() => {
230
+ if (props.unstable_cellSelectionModel) {
231
+ apiRef.current.unstable_setCellSelectionModel(props.unstable_cellSelectionModel);
232
+ }
233
+ }, [apiRef, props.unstable_cellSelectionModel]);
234
+ const checkIfCellIsSelected = React.useCallback((isSelected, {
235
+ id,
236
+ field
237
+ }) => {
238
+ return apiRef.current.unstable_isCellSelected(id, field);
239
+ }, [apiRef]);
240
+ const addClassesToCells = React.useCallback((classes, {
241
+ id,
242
+ field
243
+ }) => {
244
+ const newClasses = [...classes];
245
+ if (!visibleRows.range || !apiRef.current.unstable_isCellSelected(id, field)) {
246
+ return classes;
247
+ }
248
+ const rowIndex = apiRef.current.getRowIndexRelativeToVisibleRows(id);
249
+ const columnIndex = apiRef.current.getColumnIndex(field);
250
+ const visibleColumns = apiRef.current.getVisibleColumns();
251
+ if (rowIndex > 0) {
252
+ const {
253
+ id: previousRowId
254
+ } = visibleRows.rows[rowIndex - 1];
255
+ if (!apiRef.current.unstable_isCellSelected(previousRowId, field)) {
256
+ newClasses.push(gridClasses['cell--rangeTop']);
257
+ }
258
+ } else {
259
+ newClasses.push(gridClasses['cell--rangeTop']);
260
+ }
261
+ if (rowIndex < visibleRows.range.lastRowIndex) {
262
+ const {
263
+ id: nextRowId
264
+ } = visibleRows.rows[rowIndex + 1];
265
+ if (!apiRef.current.unstable_isCellSelected(nextRowId, field)) {
266
+ newClasses.push(gridClasses['cell--rangeBottom']);
267
+ }
268
+ } else {
269
+ newClasses.push(gridClasses['cell--rangeBottom']);
270
+ }
271
+ if (columnIndex > 0) {
272
+ const {
273
+ field: previousColumnField
274
+ } = visibleColumns[columnIndex - 1];
275
+ if (!apiRef.current.unstable_isCellSelected(id, previousColumnField)) {
276
+ newClasses.push(gridClasses['cell--rangeLeft']);
277
+ }
278
+ } else {
279
+ newClasses.push(gridClasses['cell--rangeLeft']);
280
+ }
281
+ if (columnIndex < visibleColumns.length - 1) {
282
+ const {
283
+ field: nextColumnField
284
+ } = visibleColumns[columnIndex + 1];
285
+ if (!apiRef.current.unstable_isCellSelected(id, nextColumnField)) {
286
+ newClasses.push(gridClasses['cell--rangeRight']);
287
+ }
288
+ } else {
289
+ newClasses.push(gridClasses['cell--rangeRight']);
290
+ }
291
+ return newClasses;
292
+ }, [apiRef, visibleRows.range, visibleRows.rows]);
293
+ useGridRegisterPipeProcessor(apiRef, 'isCellSelected', checkIfCellIsSelected);
294
+ useGridRegisterPipeProcessor(apiRef, 'cellClassName', addClassesToCells);
295
+ };
@@ -1,3 +1,4 @@
1
1
  export * from './aggregation';
2
2
  export * from './rowGrouping';
3
3
  export * from './export';
4
+ export * from './cellSelection';