@mui/x-data-grid 6.20.1 → 6.20.4
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.
- package/CHANGELOG.md +63 -0
- package/DataGrid/DataGrid.js +5 -0
- package/components/GridPagination.js +2 -7
- package/components/panel/GridPanel.js +4 -1
- package/hooks/features/export/useGridPrintExport.js +8 -4
- package/hooks/features/pagination/gridPaginationInterfaces.d.ts +19 -2
- package/hooks/features/pagination/gridPaginationSelector.d.ts +5 -0
- package/hooks/features/pagination/gridPaginationSelector.js +8 -2
- package/hooks/features/pagination/useGridPagination.d.ts +1 -6
- package/hooks/features/pagination/useGridPagination.js +9 -159
- package/hooks/features/pagination/useGridPaginationModel.d.ts +11 -0
- package/hooks/features/pagination/useGridPaginationModel.js +176 -0
- package/hooks/features/pagination/useGridRowCount.d.ts +8 -0
- package/hooks/features/pagination/useGridRowCount.js +97 -0
- package/index.js +1 -1
- package/legacy/DataGrid/DataGrid.js +5 -0
- package/legacy/components/GridPagination.js +2 -7
- package/legacy/components/panel/GridPanel.js +4 -1
- package/legacy/hooks/features/export/useGridPrintExport.js +10 -4
- package/legacy/hooks/features/pagination/gridPaginationSelector.js +11 -3
- package/legacy/hooks/features/pagination/useGridPagination.js +9 -161
- package/legacy/hooks/features/pagination/useGridPaginationModel.js +182 -0
- package/legacy/hooks/features/pagination/useGridRowCount.js +101 -0
- package/legacy/index.js +1 -1
- package/models/events/gridEventLookup.d.ts +6 -0
- package/models/props/DataGridProps.d.ts +5 -0
- package/modern/DataGrid/DataGrid.js +5 -0
- package/modern/components/GridPagination.js +2 -4
- package/modern/components/panel/GridPanel.js +4 -1
- package/modern/hooks/features/export/useGridPrintExport.js +8 -4
- package/modern/hooks/features/pagination/gridPaginationSelector.js +8 -2
- package/modern/hooks/features/pagination/useGridPagination.js +8 -151
- package/modern/hooks/features/pagination/useGridPaginationModel.js +171 -0
- package/modern/hooks/features/pagination/useGridRowCount.js +94 -0
- package/modern/index.js +1 -1
- package/node/DataGrid/DataGrid.js +5 -0
- package/node/components/GridPagination.js +1 -3
- package/node/components/panel/GridPanel.js +4 -1
- package/node/hooks/features/export/useGridPrintExport.js +8 -4
- package/node/hooks/features/pagination/gridPaginationSelector.js +8 -2
- package/node/hooks/features/pagination/useGridPagination.js +9 -155
- package/node/hooks/features/pagination/useGridPaginationModel.js +182 -0
- package/node/hooks/features/pagination/useGridRowCount.js +103 -0
- package/node/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { gridFilteredTopLevelRowCountSelector } from '../filter';
|
|
4
|
+
import { useGridLogger, useGridSelector, useGridApiMethod } from '../../utils';
|
|
5
|
+
import { useGridRegisterPipeProcessor } from '../../core/pipeProcessing';
|
|
6
|
+
import { gridPaginationRowCountSelector } from './gridPaginationSelector';
|
|
7
|
+
import { noRowCountInServerMode } from './gridPaginationUtils';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @requires useGridFilter (state)
|
|
11
|
+
* @requires useGridDimensions (event) - can be after
|
|
12
|
+
*/
|
|
13
|
+
export const useGridRowCount = (apiRef, props) => {
|
|
14
|
+
var _props$initialState2;
|
|
15
|
+
const logger = useGridLogger(apiRef, 'useGridRowCount');
|
|
16
|
+
const visibleTopLevelRowCount = useGridSelector(apiRef, gridFilteredTopLevelRowCountSelector);
|
|
17
|
+
const rowCount = useGridSelector(apiRef, gridPaginationRowCountSelector);
|
|
18
|
+
apiRef.current.registerControlState({
|
|
19
|
+
stateId: 'paginationRowCount',
|
|
20
|
+
propModel: props.rowCount,
|
|
21
|
+
propOnChange: props.onRowCountChange,
|
|
22
|
+
stateSelector: gridPaginationRowCountSelector,
|
|
23
|
+
changeEvent: 'rowCountChange'
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* API METHODS
|
|
28
|
+
*/
|
|
29
|
+
const setRowCount = React.useCallback(newRowCount => {
|
|
30
|
+
if (rowCount === newRowCount) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
logger.debug("Setting 'rowCount' to", newRowCount);
|
|
34
|
+
apiRef.current.setState(state => _extends({}, state, {
|
|
35
|
+
pagination: _extends({}, state.pagination, {
|
|
36
|
+
rowCount: newRowCount
|
|
37
|
+
})
|
|
38
|
+
}));
|
|
39
|
+
}, [apiRef, logger, rowCount]);
|
|
40
|
+
const paginationRowCountApi = {
|
|
41
|
+
setRowCount
|
|
42
|
+
};
|
|
43
|
+
useGridApiMethod(apiRef, paginationRowCountApi, 'public');
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* PRE-PROCESSING
|
|
47
|
+
*/
|
|
48
|
+
const stateExportPreProcessing = React.useCallback((prevState, context) => {
|
|
49
|
+
var _props$initialState;
|
|
50
|
+
const exportedRowCount = gridPaginationRowCountSelector(apiRef);
|
|
51
|
+
const shouldExportRowCount =
|
|
52
|
+
// Always export if the `exportOnlyDirtyModels` property is not activated
|
|
53
|
+
!context.exportOnlyDirtyModels ||
|
|
54
|
+
// Always export if the `rowCount` is controlled
|
|
55
|
+
props.rowCount != null ||
|
|
56
|
+
// Always export if the `rowCount` has been initialized
|
|
57
|
+
((_props$initialState = props.initialState) == null || (_props$initialState = _props$initialState.pagination) == null ? void 0 : _props$initialState.rowCount) != null;
|
|
58
|
+
if (!shouldExportRowCount) {
|
|
59
|
+
return prevState;
|
|
60
|
+
}
|
|
61
|
+
return _extends({}, prevState, {
|
|
62
|
+
pagination: _extends({}, prevState.pagination, {
|
|
63
|
+
rowCount: exportedRowCount
|
|
64
|
+
})
|
|
65
|
+
});
|
|
66
|
+
}, [apiRef, props.rowCount, (_props$initialState2 = props.initialState) == null || (_props$initialState2 = _props$initialState2.pagination) == null ? void 0 : _props$initialState2.rowCount]);
|
|
67
|
+
const stateRestorePreProcessing = React.useCallback((params, context) => {
|
|
68
|
+
var _context$stateToResto;
|
|
69
|
+
const restoredRowCount = (_context$stateToResto = context.stateToRestore.pagination) != null && _context$stateToResto.rowCount ? context.stateToRestore.pagination.rowCount : gridPaginationRowCountSelector(apiRef);
|
|
70
|
+
apiRef.current.setState(state => _extends({}, state, {
|
|
71
|
+
pagination: _extends({}, state.pagination, {
|
|
72
|
+
rowCount: restoredRowCount
|
|
73
|
+
})
|
|
74
|
+
}));
|
|
75
|
+
return params;
|
|
76
|
+
}, [apiRef]);
|
|
77
|
+
useGridRegisterPipeProcessor(apiRef, 'exportState', stateExportPreProcessing);
|
|
78
|
+
useGridRegisterPipeProcessor(apiRef, 'restoreState', stateRestorePreProcessing);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* EFFECTS
|
|
82
|
+
*/
|
|
83
|
+
React.useEffect(() => {
|
|
84
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
85
|
+
if (props.paginationMode === 'server' && props.rowCount == null) {
|
|
86
|
+
noRowCountInServerMode();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}, [props.rowCount, props.paginationMode]);
|
|
90
|
+
React.useEffect(() => {
|
|
91
|
+
if (props.paginationMode === 'client') {
|
|
92
|
+
apiRef.current.setRowCount(visibleTopLevelRowCount);
|
|
93
|
+
} else if (props.rowCount != null) {
|
|
94
|
+
apiRef.current.setRowCount(props.rowCount);
|
|
95
|
+
}
|
|
96
|
+
}, [apiRef, visibleTopLevelRowCount, props.paginationMode, props.rowCount]);
|
|
97
|
+
};
|
package/index.js
CHANGED
|
@@ -497,6 +497,11 @@ DataGridRaw.propTypes = {
|
|
|
497
497
|
* @param {GridCallbackDetails} details Additional details for this callback.
|
|
498
498
|
*/
|
|
499
499
|
onRowClick: PropTypes.func,
|
|
500
|
+
/**
|
|
501
|
+
* Callback fired when the row count has changed.
|
|
502
|
+
* @param {number} count Updated row count.
|
|
503
|
+
*/
|
|
504
|
+
onRowCountChange: PropTypes.func,
|
|
500
505
|
/**
|
|
501
506
|
* Callback fired when a double click event comes from a row container element.
|
|
502
507
|
* @param {GridRowParams} params With all properties from [[RowParams]].
|
|
@@ -7,8 +7,7 @@ import { styled } from '@mui/material/styles';
|
|
|
7
7
|
import { useGridSelector } from '../hooks/utils/useGridSelector';
|
|
8
8
|
import { useGridApiContext } from '../hooks/utils/useGridApiContext';
|
|
9
9
|
import { useGridRootProps } from '../hooks/utils/useGridRootProps';
|
|
10
|
-
import {
|
|
11
|
-
import { gridPaginationModelSelector } from '../hooks/features/pagination/gridPaginationSelector';
|
|
10
|
+
import { gridPaginationModelSelector, gridPaginationRowCountSelector } from '../hooks/features/pagination/gridPaginationSelector';
|
|
12
11
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
12
|
var GridPaginationRoot = styled(TablePagination)(function (_ref) {
|
|
14
13
|
var theme = _ref.theme;
|
|
@@ -29,11 +28,7 @@ var GridPagination = /*#__PURE__*/React.forwardRef(function GridPagination(props
|
|
|
29
28
|
var apiRef = useGridApiContext();
|
|
30
29
|
var rootProps = useGridRootProps();
|
|
31
30
|
var paginationModel = useGridSelector(apiRef, gridPaginationModelSelector);
|
|
32
|
-
var
|
|
33
|
-
var rowCount = React.useMemo(function () {
|
|
34
|
-
var _ref3, _rootProps$rowCount;
|
|
35
|
-
return (_ref3 = (_rootProps$rowCount = rootProps.rowCount) != null ? _rootProps$rowCount : visibleTopLevelRowCount) != null ? _ref3 : 0;
|
|
36
|
-
}, [rootProps.rowCount, visibleTopLevelRowCount]);
|
|
31
|
+
var rowCount = useGridSelector(apiRef, gridPaginationRowCountSelector);
|
|
37
32
|
var lastPage = React.useMemo(function () {
|
|
38
33
|
return Math.floor(rowCount / (paginationModel.pageSize || 1));
|
|
39
34
|
}, [rowCount, paginationModel.pageSize]);
|
|
@@ -66,7 +66,10 @@ var GridPanel = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
66
66
|
var modifiers = React.useMemo(function () {
|
|
67
67
|
return [{
|
|
68
68
|
name: 'flip',
|
|
69
|
-
enabled:
|
|
69
|
+
enabled: true,
|
|
70
|
+
options: {
|
|
71
|
+
rootBoundary: 'document'
|
|
72
|
+
}
|
|
70
73
|
}, {
|
|
71
74
|
name: 'isPlaced',
|
|
72
75
|
enabled: true,
|
|
@@ -11,7 +11,7 @@ import { gridClasses } from '../../../constants/gridClasses';
|
|
|
11
11
|
import { useGridApiMethod } from '../../utils/useGridApiMethod';
|
|
12
12
|
import { gridRowsMetaSelector } from '../rows/gridRowsMetaSelector';
|
|
13
13
|
import { getColumnsToExport } from './utils';
|
|
14
|
-
import {
|
|
14
|
+
import { getDerivedPaginationModel } from '../pagination/useGridPaginationModel';
|
|
15
15
|
import { useGridRegisterPipeProcessor } from '../../core/pipeProcessing';
|
|
16
16
|
import { GridPrintExportMenuItem } from '../../../components/toolbar/GridToolbarExport';
|
|
17
17
|
import { getTotalHeaderHeight } from '../columns/gridColumnsUtils';
|
|
@@ -251,9 +251,15 @@ export var useGridPrintExport = function useGridPrintExport(apiRef, props) {
|
|
|
251
251
|
page: 0,
|
|
252
252
|
pageSize: visibleRowCount
|
|
253
253
|
};
|
|
254
|
-
apiRef.current.
|
|
255
|
-
|
|
256
|
-
|
|
254
|
+
apiRef.current.setState(function (state) {
|
|
255
|
+
return _extends({}, state, {
|
|
256
|
+
pagination: _extends({}, state.pagination, {
|
|
257
|
+
paginationModel: getDerivedPaginationModel(state.pagination,
|
|
258
|
+
// Using signature `DataGridPro` to allow more than 100 rows in the print export
|
|
259
|
+
'DataGridPro', paginationModel)
|
|
260
|
+
})
|
|
261
|
+
});
|
|
262
|
+
});
|
|
257
263
|
apiRef.current.forceUpdate();
|
|
258
264
|
}
|
|
259
265
|
_context.next = 10;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createSelector, createSelectorMemoized } from '../../../utils/createSelector';
|
|
2
|
-
import {
|
|
2
|
+
import { gridExpandedSortedRowEntriesSelector, gridExpandedSortedRowIdsSelector, gridFilteredSortedTopLevelRowEntriesSelector } from '../filter/gridFilterSelector';
|
|
3
3
|
import { gridRowMaximumTreeDepthSelector, gridRowTreeSelector } from '../rows/gridRowsSelector';
|
|
4
4
|
import { getPageCount } from './gridPaginationUtils';
|
|
5
5
|
|
|
@@ -19,6 +19,14 @@ export var gridPaginationModelSelector = createSelector(gridPaginationSelector,
|
|
|
19
19
|
return pagination.paginationModel;
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Get the row count
|
|
24
|
+
* @category Pagination
|
|
25
|
+
*/
|
|
26
|
+
export var gridPaginationRowCountSelector = createSelector(gridPaginationSelector, function (pagination) {
|
|
27
|
+
return pagination.rowCount;
|
|
28
|
+
});
|
|
29
|
+
|
|
22
30
|
/**
|
|
23
31
|
* Get the index of the page to render if the pagination is enabled
|
|
24
32
|
* @category Pagination
|
|
@@ -39,8 +47,8 @@ export var gridPageSizeSelector = createSelector(gridPaginationModelSelector, fu
|
|
|
39
47
|
* Get the amount of pages needed to display all the rows if the pagination is enabled
|
|
40
48
|
* @category Pagination
|
|
41
49
|
*/
|
|
42
|
-
export var gridPageCountSelector = createSelector(
|
|
43
|
-
return getPageCount(
|
|
50
|
+
export var gridPageCountSelector = createSelector(gridPageSizeSelector, gridPaginationRowCountSelector, function (pageSize, rowCount) {
|
|
51
|
+
return getPageCount(rowCount, pageSize);
|
|
44
52
|
});
|
|
45
53
|
|
|
46
54
|
/**
|
|
@@ -1,177 +1,25 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { useGridLogger, useGridSelector, useGridApiMethod, useGridApiEventHandler } from '../../utils';
|
|
6
|
-
import { useGridRegisterPipeProcessor } from '../../core/pipeProcessing';
|
|
7
|
-
import { gridPaginationModelSelector } from './gridPaginationSelector';
|
|
8
|
-
import { calculatePinnedRowsHeight } from '../rows/gridRowsUtils';
|
|
9
|
-
import { getPageCount, noRowCountInServerMode, defaultPageSize, throwIfPageSizeExceedsTheLimit, getDefaultGridPaginationModel, getValidPage } from './gridPaginationUtils';
|
|
2
|
+
import { throwIfPageSizeExceedsTheLimit, getDefaultGridPaginationModel } from './gridPaginationUtils';
|
|
3
|
+
import { useGridPaginationModel } from './useGridPaginationModel';
|
|
4
|
+
import { useGridRowCount } from './useGridRowCount';
|
|
10
5
|
export var paginationStateInitializer = function paginationStateInitializer(state, props) {
|
|
11
|
-
var _props$paginationMode, _props$initialState;
|
|
6
|
+
var _props$paginationMode, _props$initialState, _ref, _props$rowCount, _props$initialState2;
|
|
12
7
|
var paginationModel = _extends({}, getDefaultGridPaginationModel(props.autoPageSize), (_props$paginationMode = props.paginationModel) != null ? _props$paginationMode : (_props$initialState = props.initialState) == null || (_props$initialState = _props$initialState.pagination) == null ? void 0 : _props$initialState.paginationModel);
|
|
13
8
|
throwIfPageSizeExceedsTheLimit(paginationModel.pageSize, props.signature);
|
|
9
|
+
var rowCount = (_ref = (_props$rowCount = props.rowCount) != null ? _props$rowCount : (_props$initialState2 = props.initialState) == null || (_props$initialState2 = _props$initialState2.pagination) == null ? void 0 : _props$initialState2.rowCount) != null ? _ref : 0;
|
|
14
10
|
return _extends({}, state, {
|
|
15
11
|
pagination: {
|
|
16
|
-
paginationModel: paginationModel
|
|
12
|
+
paginationModel: paginationModel,
|
|
13
|
+
rowCount: rowCount
|
|
17
14
|
}
|
|
18
15
|
});
|
|
19
16
|
};
|
|
20
|
-
export var mergeStateWithPaginationModel = function mergeStateWithPaginationModel(rowCount, signature, paginationModelProp) {
|
|
21
|
-
return function (paginationState) {
|
|
22
|
-
var _paginationModelProp$;
|
|
23
|
-
var paginationModel = paginationState.paginationModel;
|
|
24
|
-
var pageSize = (_paginationModelProp$ = paginationModelProp == null ? void 0 : paginationModelProp.pageSize) != null ? _paginationModelProp$ : paginationModel.pageSize;
|
|
25
|
-
var pageCount = getPageCount(rowCount, pageSize);
|
|
26
|
-
if (paginationModelProp && ((paginationModelProp == null ? void 0 : paginationModelProp.page) !== paginationModel.page || (paginationModelProp == null ? void 0 : paginationModelProp.pageSize) !== paginationModel.pageSize)) {
|
|
27
|
-
paginationModel = paginationModelProp;
|
|
28
|
-
}
|
|
29
|
-
var validPage = getValidPage(paginationModel.page, pageCount);
|
|
30
|
-
if (validPage !== paginationModel.page) {
|
|
31
|
-
paginationModel = _extends({}, paginationModel, {
|
|
32
|
-
page: validPage
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
throwIfPageSizeExceedsTheLimit(paginationModel.pageSize, signature);
|
|
36
|
-
return {
|
|
37
|
-
paginationModel: paginationModel
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
17
|
|
|
42
18
|
/**
|
|
43
19
|
* @requires useGridFilter (state)
|
|
44
20
|
* @requires useGridDimensions (event) - can be after
|
|
45
21
|
*/
|
|
46
22
|
export var useGridPagination = function useGridPagination(apiRef, props) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
var visibleTopLevelRowCount = useGridSelector(apiRef, gridFilteredTopLevelRowCountSelector);
|
|
50
|
-
var densityFactor = useGridSelector(apiRef, gridDensityFactorSelector);
|
|
51
|
-
var rowHeight = Math.floor(props.rowHeight * densityFactor);
|
|
52
|
-
apiRef.current.registerControlState({
|
|
53
|
-
stateId: 'pagination',
|
|
54
|
-
propModel: props.paginationModel,
|
|
55
|
-
propOnChange: props.onPaginationModelChange,
|
|
56
|
-
stateSelector: gridPaginationModelSelector,
|
|
57
|
-
changeEvent: 'paginationModelChange'
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* API METHODS
|
|
62
|
-
*/
|
|
63
|
-
var setPage = React.useCallback(function (page) {
|
|
64
|
-
var currentModel = gridPaginationModelSelector(apiRef);
|
|
65
|
-
if (page === currentModel.page) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
logger.debug("Setting page to ".concat(page));
|
|
69
|
-
apiRef.current.setPaginationModel({
|
|
70
|
-
page: page,
|
|
71
|
-
pageSize: currentModel.pageSize
|
|
72
|
-
});
|
|
73
|
-
}, [apiRef, logger]);
|
|
74
|
-
var setPageSize = React.useCallback(function (pageSize) {
|
|
75
|
-
var currentModel = gridPaginationModelSelector(apiRef);
|
|
76
|
-
if (pageSize === currentModel.pageSize) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
logger.debug("Setting page size to ".concat(pageSize));
|
|
80
|
-
apiRef.current.setPaginationModel({
|
|
81
|
-
pageSize: pageSize,
|
|
82
|
-
page: currentModel.page
|
|
83
|
-
});
|
|
84
|
-
}, [apiRef, logger]);
|
|
85
|
-
var setPaginationModel = React.useCallback(function (paginationModel) {
|
|
86
|
-
var _props$rowCount;
|
|
87
|
-
var currentModel = gridPaginationModelSelector(apiRef);
|
|
88
|
-
if (paginationModel === currentModel) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
logger.debug("Setting 'paginationModel' to", paginationModel);
|
|
92
|
-
apiRef.current.updateControlState('pagination', mergeStateWithPaginationModel((_props$rowCount = props.rowCount) != null ? _props$rowCount : visibleTopLevelRowCount, props.signature, paginationModel), 'setPaginationModel');
|
|
93
|
-
apiRef.current.forceUpdate();
|
|
94
|
-
}, [apiRef, logger, props.rowCount, props.signature, visibleTopLevelRowCount]);
|
|
95
|
-
var pageApi = {
|
|
96
|
-
setPage: setPage,
|
|
97
|
-
setPageSize: setPageSize,
|
|
98
|
-
setPaginationModel: setPaginationModel
|
|
99
|
-
};
|
|
100
|
-
useGridApiMethod(apiRef, pageApi, 'public');
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* PRE-PROCESSING
|
|
104
|
-
*/
|
|
105
|
-
var stateExportPreProcessing = React.useCallback(function (prevState, context) {
|
|
106
|
-
var _props$initialState2;
|
|
107
|
-
var paginationModel = gridPaginationModelSelector(apiRef);
|
|
108
|
-
var shouldExportPaginationModel =
|
|
109
|
-
// Always export if the `exportOnlyDirtyModels` property is not activated
|
|
110
|
-
!context.exportOnlyDirtyModels ||
|
|
111
|
-
// Always export if the `paginationModel` is controlled
|
|
112
|
-
props.paginationModel != null ||
|
|
113
|
-
// Always export if the `paginationModel` has been initialized
|
|
114
|
-
((_props$initialState2 = props.initialState) == null || (_props$initialState2 = _props$initialState2.pagination) == null ? void 0 : _props$initialState2.paginationModel) != null ||
|
|
115
|
-
// Export if `page` or `pageSize` is not equal to the default value
|
|
116
|
-
paginationModel.page !== 0 && paginationModel.pageSize !== defaultPageSize(props.autoPageSize);
|
|
117
|
-
if (!shouldExportPaginationModel) {
|
|
118
|
-
return prevState;
|
|
119
|
-
}
|
|
120
|
-
return _extends({}, prevState, {
|
|
121
|
-
pagination: _extends({}, prevState.pagination, {
|
|
122
|
-
paginationModel: paginationModel
|
|
123
|
-
})
|
|
124
|
-
});
|
|
125
|
-
}, [apiRef, props.paginationModel, (_props$initialState3 = props.initialState) == null || (_props$initialState3 = _props$initialState3.pagination) == null ? void 0 : _props$initialState3.paginationModel, props.autoPageSize]);
|
|
126
|
-
var stateRestorePreProcessing = React.useCallback(function (params, context) {
|
|
127
|
-
var _context$stateToResto, _context$stateToResto2, _props$rowCount2;
|
|
128
|
-
var paginationModel = (_context$stateToResto = context.stateToRestore.pagination) != null && _context$stateToResto.paginationModel ? _extends({}, getDefaultGridPaginationModel(props.autoPageSize), (_context$stateToResto2 = context.stateToRestore.pagination) == null ? void 0 : _context$stateToResto2.paginationModel) : gridPaginationModelSelector(apiRef);
|
|
129
|
-
apiRef.current.updateControlState('pagination', mergeStateWithPaginationModel((_props$rowCount2 = props.rowCount) != null ? _props$rowCount2 : visibleTopLevelRowCount, props.signature, paginationModel), 'stateRestorePreProcessing');
|
|
130
|
-
return params;
|
|
131
|
-
}, [apiRef, props.autoPageSize, props.rowCount, props.signature, visibleTopLevelRowCount]);
|
|
132
|
-
useGridRegisterPipeProcessor(apiRef, 'exportState', stateExportPreProcessing);
|
|
133
|
-
useGridRegisterPipeProcessor(apiRef, 'restoreState', stateRestorePreProcessing);
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* EVENTS
|
|
137
|
-
*/
|
|
138
|
-
var handlePaginationModelChange = function handlePaginationModelChange() {
|
|
139
|
-
var _apiRef$current$virtu;
|
|
140
|
-
var paginationModel = gridPaginationModelSelector(apiRef);
|
|
141
|
-
if ((_apiRef$current$virtu = apiRef.current.virtualScrollerRef) != null && _apiRef$current$virtu.current) {
|
|
142
|
-
apiRef.current.scrollToIndexes({
|
|
143
|
-
rowIndex: paginationModel.page * paginationModel.pageSize
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
apiRef.current.forceUpdate();
|
|
147
|
-
};
|
|
148
|
-
var handleUpdateAutoPageSize = React.useCallback(function () {
|
|
149
|
-
var dimensions = apiRef.current.getRootDimensions();
|
|
150
|
-
if (!props.autoPageSize || !dimensions) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
var pinnedRowsHeight = calculatePinnedRowsHeight(apiRef);
|
|
154
|
-
var maximumPageSizeWithoutScrollBar = Math.floor((dimensions.viewportInnerSize.height - pinnedRowsHeight.top - pinnedRowsHeight.bottom) / rowHeight);
|
|
155
|
-
apiRef.current.setPageSize(maximumPageSizeWithoutScrollBar);
|
|
156
|
-
}, [apiRef, props.autoPageSize, rowHeight]);
|
|
157
|
-
useGridApiEventHandler(apiRef, 'viewportInnerSizeChange', handleUpdateAutoPageSize);
|
|
158
|
-
useGridApiEventHandler(apiRef, 'paginationModelChange', handlePaginationModelChange);
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* EFFECTS
|
|
162
|
-
*/
|
|
163
|
-
React.useEffect(function () {
|
|
164
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
165
|
-
if (props.paginationMode === 'server' && props.rowCount == null) {
|
|
166
|
-
noRowCountInServerMode();
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}, [props.rowCount, props.paginationMode]);
|
|
170
|
-
React.useEffect(function () {
|
|
171
|
-
var _props$rowCount3;
|
|
172
|
-
apiRef.current.updateControlState('pagination', mergeStateWithPaginationModel((_props$rowCount3 = props.rowCount) != null ? _props$rowCount3 : visibleTopLevelRowCount, props.signature, props.paginationModel));
|
|
173
|
-
}, [apiRef, props.paginationModel, props.rowCount, props.paginationMode, visibleTopLevelRowCount, props.signature]);
|
|
174
|
-
React.useEffect(function () {
|
|
175
|
-
handleUpdateAutoPageSize();
|
|
176
|
-
}, [handleUpdateAutoPageSize]);
|
|
23
|
+
useGridPaginationModel(apiRef, props);
|
|
24
|
+
useGridRowCount(apiRef, props);
|
|
177
25
|
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { gridDensityFactorSelector } from '../density';
|
|
4
|
+
import { calculatePinnedRowsHeight } from '../rows/gridRowsUtils';
|
|
5
|
+
import { useGridLogger, useGridSelector, useGridApiMethod, useGridApiEventHandler } from '../../utils';
|
|
6
|
+
import { useGridRegisterPipeProcessor } from '../../core/pipeProcessing';
|
|
7
|
+
import { gridPageCountSelector, gridPaginationModelSelector } from './gridPaginationSelector';
|
|
8
|
+
import { getPageCount, defaultPageSize, throwIfPageSizeExceedsTheLimit, getDefaultGridPaginationModel, getValidPage } from './gridPaginationUtils';
|
|
9
|
+
export var getDerivedPaginationModel = function getDerivedPaginationModel(paginationState, signature, paginationModelProp) {
|
|
10
|
+
var _paginationModelProp$;
|
|
11
|
+
var paginationModel = paginationState.paginationModel;
|
|
12
|
+
var rowCount = paginationState.rowCount;
|
|
13
|
+
var pageSize = (_paginationModelProp$ = paginationModelProp == null ? void 0 : paginationModelProp.pageSize) != null ? _paginationModelProp$ : paginationModel.pageSize;
|
|
14
|
+
var pageCount = getPageCount(rowCount, pageSize);
|
|
15
|
+
if (paginationModelProp && ((paginationModelProp == null ? void 0 : paginationModelProp.page) !== paginationModel.page || (paginationModelProp == null ? void 0 : paginationModelProp.pageSize) !== paginationModel.pageSize)) {
|
|
16
|
+
paginationModel = paginationModelProp;
|
|
17
|
+
}
|
|
18
|
+
var validPage = getValidPage(paginationModel.page, pageCount);
|
|
19
|
+
if (validPage !== paginationModel.page) {
|
|
20
|
+
paginationModel = _extends({}, paginationModel, {
|
|
21
|
+
page: validPage
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
throwIfPageSizeExceedsTheLimit(paginationModel.pageSize, signature);
|
|
25
|
+
return paginationModel;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @requires useGridFilter (state)
|
|
30
|
+
* @requires useGridDimensions (event) - can be after
|
|
31
|
+
*/
|
|
32
|
+
export var useGridPaginationModel = function useGridPaginationModel(apiRef, props) {
|
|
33
|
+
var _props$initialState2;
|
|
34
|
+
var logger = useGridLogger(apiRef, 'useGridPaginationModel');
|
|
35
|
+
var densityFactor = useGridSelector(apiRef, gridDensityFactorSelector);
|
|
36
|
+
var rowHeight = Math.floor(props.rowHeight * densityFactor);
|
|
37
|
+
apiRef.current.registerControlState({
|
|
38
|
+
stateId: 'paginationModel',
|
|
39
|
+
propModel: props.paginationModel,
|
|
40
|
+
propOnChange: props.onPaginationModelChange,
|
|
41
|
+
stateSelector: gridPaginationModelSelector,
|
|
42
|
+
changeEvent: 'paginationModelChange'
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* API METHODS
|
|
47
|
+
*/
|
|
48
|
+
var setPage = React.useCallback(function (page) {
|
|
49
|
+
var currentModel = gridPaginationModelSelector(apiRef);
|
|
50
|
+
if (page === currentModel.page) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
logger.debug("Setting page to ".concat(page));
|
|
54
|
+
apiRef.current.setPaginationModel({
|
|
55
|
+
page: page,
|
|
56
|
+
pageSize: currentModel.pageSize
|
|
57
|
+
});
|
|
58
|
+
}, [apiRef, logger]);
|
|
59
|
+
var setPageSize = React.useCallback(function (pageSize) {
|
|
60
|
+
var currentModel = gridPaginationModelSelector(apiRef);
|
|
61
|
+
if (pageSize === currentModel.pageSize) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
logger.debug("Setting page size to ".concat(pageSize));
|
|
65
|
+
apiRef.current.setPaginationModel({
|
|
66
|
+
pageSize: pageSize,
|
|
67
|
+
page: currentModel.page
|
|
68
|
+
});
|
|
69
|
+
}, [apiRef, logger]);
|
|
70
|
+
var setPaginationModel = React.useCallback(function (paginationModel) {
|
|
71
|
+
var currentModel = gridPaginationModelSelector(apiRef);
|
|
72
|
+
if (paginationModel === currentModel) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
logger.debug("Setting 'paginationModel' to", paginationModel);
|
|
76
|
+
apiRef.current.setState(function (state) {
|
|
77
|
+
return _extends({}, state, {
|
|
78
|
+
pagination: _extends({}, state.pagination, {
|
|
79
|
+
paginationModel: getDerivedPaginationModel(state.pagination, props.signature, paginationModel)
|
|
80
|
+
})
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}, [apiRef, logger, props.signature]);
|
|
84
|
+
var paginationModelApi = {
|
|
85
|
+
setPage: setPage,
|
|
86
|
+
setPageSize: setPageSize,
|
|
87
|
+
setPaginationModel: setPaginationModel
|
|
88
|
+
};
|
|
89
|
+
useGridApiMethod(apiRef, paginationModelApi, 'public');
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* PRE-PROCESSING
|
|
93
|
+
*/
|
|
94
|
+
var stateExportPreProcessing = React.useCallback(function (prevState, context) {
|
|
95
|
+
var _props$initialState;
|
|
96
|
+
var paginationModel = gridPaginationModelSelector(apiRef);
|
|
97
|
+
var shouldExportPaginationModel =
|
|
98
|
+
// Always export if the `exportOnlyDirtyModels` property is not activated
|
|
99
|
+
!context.exportOnlyDirtyModels ||
|
|
100
|
+
// Always export if the `paginationModel` is controlled
|
|
101
|
+
props.paginationModel != null ||
|
|
102
|
+
// Always export if the `paginationModel` has been initialized
|
|
103
|
+
((_props$initialState = props.initialState) == null || (_props$initialState = _props$initialState.pagination) == null ? void 0 : _props$initialState.paginationModel) != null ||
|
|
104
|
+
// Export if `page` or `pageSize` is not equal to the default value
|
|
105
|
+
paginationModel.page !== 0 && paginationModel.pageSize !== defaultPageSize(props.autoPageSize);
|
|
106
|
+
if (!shouldExportPaginationModel) {
|
|
107
|
+
return prevState;
|
|
108
|
+
}
|
|
109
|
+
return _extends({}, prevState, {
|
|
110
|
+
pagination: _extends({}, prevState.pagination, {
|
|
111
|
+
paginationModel: paginationModel
|
|
112
|
+
})
|
|
113
|
+
});
|
|
114
|
+
}, [apiRef, props.paginationModel, (_props$initialState2 = props.initialState) == null || (_props$initialState2 = _props$initialState2.pagination) == null ? void 0 : _props$initialState2.paginationModel, props.autoPageSize]);
|
|
115
|
+
var stateRestorePreProcessing = React.useCallback(function (params, context) {
|
|
116
|
+
var _context$stateToResto, _context$stateToResto2;
|
|
117
|
+
var paginationModel = (_context$stateToResto = context.stateToRestore.pagination) != null && _context$stateToResto.paginationModel ? _extends({}, getDefaultGridPaginationModel(props.autoPageSize), (_context$stateToResto2 = context.stateToRestore.pagination) == null ? void 0 : _context$stateToResto2.paginationModel) : gridPaginationModelSelector(apiRef);
|
|
118
|
+
apiRef.current.setState(function (state) {
|
|
119
|
+
return _extends({}, state, {
|
|
120
|
+
pagination: _extends({}, state.pagination, {
|
|
121
|
+
paginationModel: getDerivedPaginationModel(state.pagination, props.signature, paginationModel)
|
|
122
|
+
})
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
return params;
|
|
126
|
+
}, [apiRef, props.autoPageSize, props.signature]);
|
|
127
|
+
useGridRegisterPipeProcessor(apiRef, 'exportState', stateExportPreProcessing);
|
|
128
|
+
useGridRegisterPipeProcessor(apiRef, 'restoreState', stateRestorePreProcessing);
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* EVENTS
|
|
132
|
+
*/
|
|
133
|
+
var handlePaginationModelChange = function handlePaginationModelChange() {
|
|
134
|
+
var _apiRef$current$virtu;
|
|
135
|
+
var paginationModel = gridPaginationModelSelector(apiRef);
|
|
136
|
+
if ((_apiRef$current$virtu = apiRef.current.virtualScrollerRef) != null && _apiRef$current$virtu.current) {
|
|
137
|
+
apiRef.current.scrollToIndexes({
|
|
138
|
+
rowIndex: paginationModel.page * paginationModel.pageSize
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
var handleUpdateAutoPageSize = React.useCallback(function () {
|
|
143
|
+
if (!props.autoPageSize) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
var dimensions = apiRef.current.getRootDimensions() || {
|
|
147
|
+
viewportInnerSize: {
|
|
148
|
+
height: 0
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
var pinnedRowsHeight = calculatePinnedRowsHeight(apiRef);
|
|
152
|
+
var maximumPageSizeWithoutScrollBar = Math.floor((dimensions.viewportInnerSize.height - pinnedRowsHeight.top - pinnedRowsHeight.bottom) / rowHeight);
|
|
153
|
+
apiRef.current.setPageSize(maximumPageSizeWithoutScrollBar);
|
|
154
|
+
}, [apiRef, props.autoPageSize, rowHeight]);
|
|
155
|
+
var handleRowCountChange = React.useCallback(function (newRowCount) {
|
|
156
|
+
if (newRowCount == null) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
var paginationModel = gridPaginationModelSelector(apiRef);
|
|
160
|
+
var pageCount = gridPageCountSelector(apiRef);
|
|
161
|
+
if (paginationModel.page > pageCount - 1) {
|
|
162
|
+
apiRef.current.setPage(Math.max(0, pageCount - 1));
|
|
163
|
+
}
|
|
164
|
+
}, [apiRef]);
|
|
165
|
+
useGridApiEventHandler(apiRef, 'viewportInnerSizeChange', handleUpdateAutoPageSize);
|
|
166
|
+
useGridApiEventHandler(apiRef, 'paginationModelChange', handlePaginationModelChange);
|
|
167
|
+
useGridApiEventHandler(apiRef, 'rowCountChange', handleRowCountChange);
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* EFFECTS
|
|
171
|
+
*/
|
|
172
|
+
React.useEffect(function () {
|
|
173
|
+
apiRef.current.setState(function (state) {
|
|
174
|
+
return _extends({}, state, {
|
|
175
|
+
pagination: _extends({}, state.pagination, {
|
|
176
|
+
paginationModel: getDerivedPaginationModel(state.pagination, props.signature, props.paginationModel)
|
|
177
|
+
})
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}, [apiRef, props.paginationModel, props.paginationMode, props.signature]);
|
|
181
|
+
React.useEffect(handleUpdateAutoPageSize, [handleUpdateAutoPageSize]);
|
|
182
|
+
};
|