@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,94 @@
|
|
|
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
|
+
const logger = useGridLogger(apiRef, 'useGridRowCount');
|
|
15
|
+
const visibleTopLevelRowCount = useGridSelector(apiRef, gridFilteredTopLevelRowCountSelector);
|
|
16
|
+
const rowCount = useGridSelector(apiRef, gridPaginationRowCountSelector);
|
|
17
|
+
apiRef.current.registerControlState({
|
|
18
|
+
stateId: 'paginationRowCount',
|
|
19
|
+
propModel: props.rowCount,
|
|
20
|
+
propOnChange: props.onRowCountChange,
|
|
21
|
+
stateSelector: gridPaginationRowCountSelector,
|
|
22
|
+
changeEvent: 'rowCountChange'
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* API METHODS
|
|
27
|
+
*/
|
|
28
|
+
const setRowCount = React.useCallback(newRowCount => {
|
|
29
|
+
if (rowCount === newRowCount) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
logger.debug("Setting 'rowCount' to", newRowCount);
|
|
33
|
+
apiRef.current.setState(state => _extends({}, state, {
|
|
34
|
+
pagination: _extends({}, state.pagination, {
|
|
35
|
+
rowCount: newRowCount
|
|
36
|
+
})
|
|
37
|
+
}));
|
|
38
|
+
}, [apiRef, logger, rowCount]);
|
|
39
|
+
const paginationRowCountApi = {
|
|
40
|
+
setRowCount
|
|
41
|
+
};
|
|
42
|
+
useGridApiMethod(apiRef, paginationRowCountApi, 'public');
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* PRE-PROCESSING
|
|
46
|
+
*/
|
|
47
|
+
const stateExportPreProcessing = React.useCallback((prevState, context) => {
|
|
48
|
+
const exportedRowCount = gridPaginationRowCountSelector(apiRef);
|
|
49
|
+
const shouldExportRowCount =
|
|
50
|
+
// Always export if the `exportOnlyDirtyModels` property is not activated
|
|
51
|
+
!context.exportOnlyDirtyModels ||
|
|
52
|
+
// Always export if the `rowCount` is controlled
|
|
53
|
+
props.rowCount != null ||
|
|
54
|
+
// Always export if the `rowCount` has been initialized
|
|
55
|
+
props.initialState?.pagination?.rowCount != null;
|
|
56
|
+
if (!shouldExportRowCount) {
|
|
57
|
+
return prevState;
|
|
58
|
+
}
|
|
59
|
+
return _extends({}, prevState, {
|
|
60
|
+
pagination: _extends({}, prevState.pagination, {
|
|
61
|
+
rowCount: exportedRowCount
|
|
62
|
+
})
|
|
63
|
+
});
|
|
64
|
+
}, [apiRef, props.rowCount, props.initialState?.pagination?.rowCount]);
|
|
65
|
+
const stateRestorePreProcessing = React.useCallback((params, context) => {
|
|
66
|
+
const restoredRowCount = context.stateToRestore.pagination?.rowCount ? context.stateToRestore.pagination.rowCount : gridPaginationRowCountSelector(apiRef);
|
|
67
|
+
apiRef.current.setState(state => _extends({}, state, {
|
|
68
|
+
pagination: _extends({}, state.pagination, {
|
|
69
|
+
rowCount: restoredRowCount
|
|
70
|
+
})
|
|
71
|
+
}));
|
|
72
|
+
return params;
|
|
73
|
+
}, [apiRef]);
|
|
74
|
+
useGridRegisterPipeProcessor(apiRef, 'exportState', stateExportPreProcessing);
|
|
75
|
+
useGridRegisterPipeProcessor(apiRef, 'restoreState', stateRestorePreProcessing);
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* EFFECTS
|
|
79
|
+
*/
|
|
80
|
+
React.useEffect(() => {
|
|
81
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
82
|
+
if (props.paginationMode === 'server' && props.rowCount == null) {
|
|
83
|
+
noRowCountInServerMode();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}, [props.rowCount, props.paginationMode]);
|
|
87
|
+
React.useEffect(() => {
|
|
88
|
+
if (props.paginationMode === 'client') {
|
|
89
|
+
apiRef.current.setRowCount(visibleTopLevelRowCount);
|
|
90
|
+
} else if (props.rowCount != null) {
|
|
91
|
+
apiRef.current.setRowCount(props.rowCount);
|
|
92
|
+
}
|
|
93
|
+
}, [apiRef, visibleTopLevelRowCount, props.paginationMode, props.rowCount]);
|
|
94
|
+
};
|
package/modern/index.js
CHANGED
|
@@ -503,6 +503,11 @@ DataGridRaw.propTypes = {
|
|
|
503
503
|
* @param {GridCallbackDetails} details Additional details for this callback.
|
|
504
504
|
*/
|
|
505
505
|
onRowClick: _propTypes.default.func,
|
|
506
|
+
/**
|
|
507
|
+
* Callback fired when the row count has changed.
|
|
508
|
+
* @param {number} count Updated row count.
|
|
509
|
+
*/
|
|
510
|
+
onRowCountChange: _propTypes.default.func,
|
|
506
511
|
/**
|
|
507
512
|
* Callback fired when a double click event comes from a row container element.
|
|
508
513
|
* @param {GridRowParams} params With all properties from [[RowParams]].
|
|
@@ -13,7 +13,6 @@ var _styles = require("@mui/material/styles");
|
|
|
13
13
|
var _useGridSelector = require("../hooks/utils/useGridSelector");
|
|
14
14
|
var _useGridApiContext = require("../hooks/utils/useGridApiContext");
|
|
15
15
|
var _useGridRootProps = require("../hooks/utils/useGridRootProps");
|
|
16
|
-
var _filter = require("../hooks/features/filter");
|
|
17
16
|
var _gridPaginationSelector = require("../hooks/features/pagination/gridPaginationSelector");
|
|
18
17
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
19
18
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
@@ -41,8 +40,7 @@ const GridPagination = exports.GridPagination = /*#__PURE__*/React.forwardRef(fu
|
|
|
41
40
|
const apiRef = (0, _useGridApiContext.useGridApiContext)();
|
|
42
41
|
const rootProps = (0, _useGridRootProps.useGridRootProps)();
|
|
43
42
|
const paginationModel = (0, _useGridSelector.useGridSelector)(apiRef, _gridPaginationSelector.gridPaginationModelSelector);
|
|
44
|
-
const
|
|
45
|
-
const rowCount = React.useMemo(() => rootProps.rowCount ?? visibleTopLevelRowCount ?? 0, [rootProps.rowCount, visibleTopLevelRowCount]);
|
|
43
|
+
const rowCount = (0, _useGridSelector.useGridSelector)(apiRef, _gridPaginationSelector.gridPaginationRowCountSelector);
|
|
46
44
|
const lastPage = React.useMemo(() => Math.floor(rowCount / (paginationModel.pageSize || 1)), [rowCount, paginationModel.pageSize]);
|
|
47
45
|
const handlePageSizeChange = React.useCallback(event => {
|
|
48
46
|
const pageSize = Number(event.target.value);
|
|
@@ -65,7 +65,10 @@ const GridPanel = exports.GridPanel = /*#__PURE__*/React.forwardRef((props, ref)
|
|
|
65
65
|
}, [apiRef]);
|
|
66
66
|
const modifiers = React.useMemo(() => [{
|
|
67
67
|
name: 'flip',
|
|
68
|
-
enabled:
|
|
68
|
+
enabled: true,
|
|
69
|
+
options: {
|
|
70
|
+
rootBoundary: 'document'
|
|
71
|
+
}
|
|
69
72
|
}, {
|
|
70
73
|
name: 'isPlaced',
|
|
71
74
|
enabled: true,
|
|
@@ -15,7 +15,7 @@ var _gridClasses = require("../../../constants/gridClasses");
|
|
|
15
15
|
var _useGridApiMethod = require("../../utils/useGridApiMethod");
|
|
16
16
|
var _gridRowsMetaSelector = require("../rows/gridRowsMetaSelector");
|
|
17
17
|
var _utils2 = require("./utils");
|
|
18
|
-
var
|
|
18
|
+
var _useGridPaginationModel = require("../pagination/useGridPaginationModel");
|
|
19
19
|
var _pipeProcessing = require("../../core/pipeProcessing");
|
|
20
20
|
var _GridToolbarExport = require("../../../components/toolbar/GridToolbarExport");
|
|
21
21
|
var _gridColumnsUtils = require("../columns/gridColumnsUtils");
|
|
@@ -231,9 +231,13 @@ const useGridPrintExport = (apiRef, props) => {
|
|
|
231
231
|
page: 0,
|
|
232
232
|
pageSize: visibleRowCount
|
|
233
233
|
};
|
|
234
|
-
apiRef.current.
|
|
235
|
-
|
|
236
|
-
|
|
234
|
+
apiRef.current.setState(state => (0, _extends2.default)({}, state, {
|
|
235
|
+
pagination: (0, _extends2.default)({}, state.pagination, {
|
|
236
|
+
paginationModel: (0, _useGridPaginationModel.getDerivedPaginationModel)(state.pagination,
|
|
237
|
+
// Using signature `DataGridPro` to allow more than 100 rows in the print export
|
|
238
|
+
'DataGridPro', paginationModel)
|
|
239
|
+
})
|
|
240
|
+
}));
|
|
237
241
|
apiRef.current.forceUpdate();
|
|
238
242
|
}
|
|
239
243
|
await updateGridColumnsForPrint(options?.fields, options?.allColumns, options?.includeCheckboxes);
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.gridPaginationSelector = exports.gridPaginationRowRangeSelector = exports.gridPaginationModelSelector = exports.gridPaginatedVisibleSortedGridRowIdsSelector = exports.gridPaginatedVisibleSortedGridRowEntriesSelector = exports.gridPageSizeSelector = exports.gridPageSelector = exports.gridPageCountSelector = void 0;
|
|
6
|
+
exports.gridPaginationSelector = exports.gridPaginationRowRangeSelector = exports.gridPaginationRowCountSelector = exports.gridPaginationModelSelector = exports.gridPaginatedVisibleSortedGridRowIdsSelector = exports.gridPaginatedVisibleSortedGridRowEntriesSelector = exports.gridPageSizeSelector = exports.gridPageSelector = exports.gridPageCountSelector = void 0;
|
|
7
7
|
var _createSelector = require("../../../utils/createSelector");
|
|
8
8
|
var _gridFilterSelector = require("../filter/gridFilterSelector");
|
|
9
9
|
var _gridRowsSelector = require("../rows/gridRowsSelector");
|
|
@@ -21,6 +21,12 @@ const gridPaginationSelector = state => state.pagination;
|
|
|
21
21
|
exports.gridPaginationSelector = gridPaginationSelector;
|
|
22
22
|
const gridPaginationModelSelector = exports.gridPaginationModelSelector = (0, _createSelector.createSelector)(gridPaginationSelector, pagination => pagination.paginationModel);
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Get the row count
|
|
26
|
+
* @category Pagination
|
|
27
|
+
*/
|
|
28
|
+
const gridPaginationRowCountSelector = exports.gridPaginationRowCountSelector = (0, _createSelector.createSelector)(gridPaginationSelector, pagination => pagination.rowCount);
|
|
29
|
+
|
|
24
30
|
/**
|
|
25
31
|
* Get the index of the page to render if the pagination is enabled
|
|
26
32
|
* @category Pagination
|
|
@@ -37,7 +43,7 @@ const gridPageSizeSelector = exports.gridPageSizeSelector = (0, _createSelector.
|
|
|
37
43
|
* Get the amount of pages needed to display all the rows if the pagination is enabled
|
|
38
44
|
* @category Pagination
|
|
39
45
|
*/
|
|
40
|
-
const gridPageCountSelector = exports.gridPageCountSelector = (0, _createSelector.createSelector)(
|
|
46
|
+
const gridPageCountSelector = exports.gridPageCountSelector = (0, _createSelector.createSelector)(gridPageSizeSelector, gridPaginationRowCountSelector, (pageSize, rowCount) => (0, _gridPaginationUtils.getPageCount)(rowCount, pageSize));
|
|
41
47
|
|
|
42
48
|
/**
|
|
43
49
|
* Get the index of the first and the last row to include in the current page if the pagination is enabled.
|
|
@@ -4,176 +4,30 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
|
-
exports.useGridPagination = exports.paginationStateInitializer =
|
|
7
|
+
exports.useGridPagination = exports.paginationStateInitializer = void 0;
|
|
8
8
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
9
|
-
var React = _interopRequireWildcard(require("react"));
|
|
10
|
-
var _filter = require("../filter");
|
|
11
|
-
var _density = require("../density");
|
|
12
|
-
var _utils = require("../../utils");
|
|
13
|
-
var _pipeProcessing = require("../../core/pipeProcessing");
|
|
14
|
-
var _gridPaginationSelector = require("./gridPaginationSelector");
|
|
15
|
-
var _gridRowsUtils = require("../rows/gridRowsUtils");
|
|
16
9
|
var _gridPaginationUtils = require("./gridPaginationUtils");
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
var _useGridPaginationModel = require("./useGridPaginationModel");
|
|
11
|
+
var _useGridRowCount = require("./useGridRowCount");
|
|
19
12
|
const paginationStateInitializer = (state, props) => {
|
|
20
13
|
const paginationModel = (0, _extends2.default)({}, (0, _gridPaginationUtils.getDefaultGridPaginationModel)(props.autoPageSize), props.paginationModel ?? props.initialState?.pagination?.paginationModel);
|
|
21
14
|
(0, _gridPaginationUtils.throwIfPageSizeExceedsTheLimit)(paginationModel.pageSize, props.signature);
|
|
15
|
+
const rowCount = props.rowCount ?? props.initialState?.pagination?.rowCount ?? 0;
|
|
22
16
|
return (0, _extends2.default)({}, state, {
|
|
23
17
|
pagination: {
|
|
24
|
-
paginationModel
|
|
18
|
+
paginationModel,
|
|
19
|
+
rowCount
|
|
25
20
|
}
|
|
26
21
|
});
|
|
27
22
|
};
|
|
28
|
-
exports.paginationStateInitializer = paginationStateInitializer;
|
|
29
|
-
const mergeStateWithPaginationModel = (rowCount, signature, paginationModelProp) => paginationState => {
|
|
30
|
-
let paginationModel = paginationState.paginationModel;
|
|
31
|
-
const pageSize = paginationModelProp?.pageSize ?? paginationModel.pageSize;
|
|
32
|
-
const pageCount = (0, _gridPaginationUtils.getPageCount)(rowCount, pageSize);
|
|
33
|
-
if (paginationModelProp && (paginationModelProp?.page !== paginationModel.page || paginationModelProp?.pageSize !== paginationModel.pageSize)) {
|
|
34
|
-
paginationModel = paginationModelProp;
|
|
35
|
-
}
|
|
36
|
-
const validPage = (0, _gridPaginationUtils.getValidPage)(paginationModel.page, pageCount);
|
|
37
|
-
if (validPage !== paginationModel.page) {
|
|
38
|
-
paginationModel = (0, _extends2.default)({}, paginationModel, {
|
|
39
|
-
page: validPage
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
(0, _gridPaginationUtils.throwIfPageSizeExceedsTheLimit)(paginationModel.pageSize, signature);
|
|
43
|
-
return {
|
|
44
|
-
paginationModel
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
23
|
|
|
48
24
|
/**
|
|
49
25
|
* @requires useGridFilter (state)
|
|
50
26
|
* @requires useGridDimensions (event) - can be after
|
|
51
27
|
*/
|
|
52
|
-
exports.
|
|
28
|
+
exports.paginationStateInitializer = paginationStateInitializer;
|
|
53
29
|
const useGridPagination = (apiRef, props) => {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const densityFactor = (0, _utils.useGridSelector)(apiRef, _density.gridDensityFactorSelector);
|
|
57
|
-
const rowHeight = Math.floor(props.rowHeight * densityFactor);
|
|
58
|
-
apiRef.current.registerControlState({
|
|
59
|
-
stateId: 'pagination',
|
|
60
|
-
propModel: props.paginationModel,
|
|
61
|
-
propOnChange: props.onPaginationModelChange,
|
|
62
|
-
stateSelector: _gridPaginationSelector.gridPaginationModelSelector,
|
|
63
|
-
changeEvent: 'paginationModelChange'
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* API METHODS
|
|
68
|
-
*/
|
|
69
|
-
const setPage = React.useCallback(page => {
|
|
70
|
-
const currentModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
71
|
-
if (page === currentModel.page) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
logger.debug(`Setting page to ${page}`);
|
|
75
|
-
apiRef.current.setPaginationModel({
|
|
76
|
-
page,
|
|
77
|
-
pageSize: currentModel.pageSize
|
|
78
|
-
});
|
|
79
|
-
}, [apiRef, logger]);
|
|
80
|
-
const setPageSize = React.useCallback(pageSize => {
|
|
81
|
-
const currentModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
82
|
-
if (pageSize === currentModel.pageSize) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
logger.debug(`Setting page size to ${pageSize}`);
|
|
86
|
-
apiRef.current.setPaginationModel({
|
|
87
|
-
pageSize,
|
|
88
|
-
page: currentModel.page
|
|
89
|
-
});
|
|
90
|
-
}, [apiRef, logger]);
|
|
91
|
-
const setPaginationModel = React.useCallback(paginationModel => {
|
|
92
|
-
const currentModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
93
|
-
if (paginationModel === currentModel) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
logger.debug("Setting 'paginationModel' to", paginationModel);
|
|
97
|
-
apiRef.current.updateControlState('pagination', mergeStateWithPaginationModel(props.rowCount ?? visibleTopLevelRowCount, props.signature, paginationModel), 'setPaginationModel');
|
|
98
|
-
apiRef.current.forceUpdate();
|
|
99
|
-
}, [apiRef, logger, props.rowCount, props.signature, visibleTopLevelRowCount]);
|
|
100
|
-
const pageApi = {
|
|
101
|
-
setPage,
|
|
102
|
-
setPageSize,
|
|
103
|
-
setPaginationModel
|
|
104
|
-
};
|
|
105
|
-
(0, _utils.useGridApiMethod)(apiRef, pageApi, 'public');
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* PRE-PROCESSING
|
|
109
|
-
*/
|
|
110
|
-
const stateExportPreProcessing = React.useCallback((prevState, context) => {
|
|
111
|
-
const paginationModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
112
|
-
const shouldExportPaginationModel =
|
|
113
|
-
// Always export if the `exportOnlyDirtyModels` property is not activated
|
|
114
|
-
!context.exportOnlyDirtyModels ||
|
|
115
|
-
// Always export if the `paginationModel` is controlled
|
|
116
|
-
props.paginationModel != null ||
|
|
117
|
-
// Always export if the `paginationModel` has been initialized
|
|
118
|
-
props.initialState?.pagination?.paginationModel != null ||
|
|
119
|
-
// Export if `page` or `pageSize` is not equal to the default value
|
|
120
|
-
paginationModel.page !== 0 && paginationModel.pageSize !== (0, _gridPaginationUtils.defaultPageSize)(props.autoPageSize);
|
|
121
|
-
if (!shouldExportPaginationModel) {
|
|
122
|
-
return prevState;
|
|
123
|
-
}
|
|
124
|
-
return (0, _extends2.default)({}, prevState, {
|
|
125
|
-
pagination: (0, _extends2.default)({}, prevState.pagination, {
|
|
126
|
-
paginationModel
|
|
127
|
-
})
|
|
128
|
-
});
|
|
129
|
-
}, [apiRef, props.paginationModel, props.initialState?.pagination?.paginationModel, props.autoPageSize]);
|
|
130
|
-
const stateRestorePreProcessing = React.useCallback((params, context) => {
|
|
131
|
-
const paginationModel = context.stateToRestore.pagination?.paginationModel ? (0, _extends2.default)({}, (0, _gridPaginationUtils.getDefaultGridPaginationModel)(props.autoPageSize), context.stateToRestore.pagination?.paginationModel) : (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
132
|
-
apiRef.current.updateControlState('pagination', mergeStateWithPaginationModel(props.rowCount ?? visibleTopLevelRowCount, props.signature, paginationModel), 'stateRestorePreProcessing');
|
|
133
|
-
return params;
|
|
134
|
-
}, [apiRef, props.autoPageSize, props.rowCount, props.signature, visibleTopLevelRowCount]);
|
|
135
|
-
(0, _pipeProcessing.useGridRegisterPipeProcessor)(apiRef, 'exportState', stateExportPreProcessing);
|
|
136
|
-
(0, _pipeProcessing.useGridRegisterPipeProcessor)(apiRef, 'restoreState', stateRestorePreProcessing);
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* EVENTS
|
|
140
|
-
*/
|
|
141
|
-
const handlePaginationModelChange = () => {
|
|
142
|
-
const paginationModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
143
|
-
if (apiRef.current.virtualScrollerRef?.current) {
|
|
144
|
-
apiRef.current.scrollToIndexes({
|
|
145
|
-
rowIndex: paginationModel.page * paginationModel.pageSize
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
apiRef.current.forceUpdate();
|
|
149
|
-
};
|
|
150
|
-
const handleUpdateAutoPageSize = React.useCallback(() => {
|
|
151
|
-
const dimensions = apiRef.current.getRootDimensions();
|
|
152
|
-
if (!props.autoPageSize || !dimensions) {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
const pinnedRowsHeight = (0, _gridRowsUtils.calculatePinnedRowsHeight)(apiRef);
|
|
156
|
-
const maximumPageSizeWithoutScrollBar = Math.floor((dimensions.viewportInnerSize.height - pinnedRowsHeight.top - pinnedRowsHeight.bottom) / rowHeight);
|
|
157
|
-
apiRef.current.setPageSize(maximumPageSizeWithoutScrollBar);
|
|
158
|
-
}, [apiRef, props.autoPageSize, rowHeight]);
|
|
159
|
-
(0, _utils.useGridApiEventHandler)(apiRef, 'viewportInnerSizeChange', handleUpdateAutoPageSize);
|
|
160
|
-
(0, _utils.useGridApiEventHandler)(apiRef, 'paginationModelChange', handlePaginationModelChange);
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* EFFECTS
|
|
164
|
-
*/
|
|
165
|
-
React.useEffect(() => {
|
|
166
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
167
|
-
if (props.paginationMode === 'server' && props.rowCount == null) {
|
|
168
|
-
(0, _gridPaginationUtils.noRowCountInServerMode)();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}, [props.rowCount, props.paginationMode]);
|
|
172
|
-
React.useEffect(() => {
|
|
173
|
-
apiRef.current.updateControlState('pagination', mergeStateWithPaginationModel(props.rowCount ?? visibleTopLevelRowCount, props.signature, props.paginationModel));
|
|
174
|
-
}, [apiRef, props.paginationModel, props.rowCount, props.paginationMode, visibleTopLevelRowCount, props.signature]);
|
|
175
|
-
React.useEffect(() => {
|
|
176
|
-
handleUpdateAutoPageSize();
|
|
177
|
-
}, [handleUpdateAutoPageSize]);
|
|
30
|
+
(0, _useGridPaginationModel.useGridPaginationModel)(apiRef, props);
|
|
31
|
+
(0, _useGridRowCount.useGridRowCount)(apiRef, props);
|
|
178
32
|
};
|
|
179
33
|
exports.useGridPagination = useGridPagination;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.useGridPaginationModel = exports.getDerivedPaginationModel = void 0;
|
|
8
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
9
|
+
var React = _interopRequireWildcard(require("react"));
|
|
10
|
+
var _density = require("../density");
|
|
11
|
+
var _gridRowsUtils = require("../rows/gridRowsUtils");
|
|
12
|
+
var _utils = require("../../utils");
|
|
13
|
+
var _pipeProcessing = require("../../core/pipeProcessing");
|
|
14
|
+
var _gridPaginationSelector = require("./gridPaginationSelector");
|
|
15
|
+
var _gridPaginationUtils = require("./gridPaginationUtils");
|
|
16
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
17
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
18
|
+
const getDerivedPaginationModel = (paginationState, signature, paginationModelProp) => {
|
|
19
|
+
let paginationModel = paginationState.paginationModel;
|
|
20
|
+
const rowCount = paginationState.rowCount;
|
|
21
|
+
const pageSize = paginationModelProp?.pageSize ?? paginationModel.pageSize;
|
|
22
|
+
const pageCount = (0, _gridPaginationUtils.getPageCount)(rowCount, pageSize);
|
|
23
|
+
if (paginationModelProp && (paginationModelProp?.page !== paginationModel.page || paginationModelProp?.pageSize !== paginationModel.pageSize)) {
|
|
24
|
+
paginationModel = paginationModelProp;
|
|
25
|
+
}
|
|
26
|
+
const validPage = (0, _gridPaginationUtils.getValidPage)(paginationModel.page, pageCount);
|
|
27
|
+
if (validPage !== paginationModel.page) {
|
|
28
|
+
paginationModel = (0, _extends2.default)({}, paginationModel, {
|
|
29
|
+
page: validPage
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
(0, _gridPaginationUtils.throwIfPageSizeExceedsTheLimit)(paginationModel.pageSize, signature);
|
|
33
|
+
return paginationModel;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @requires useGridFilter (state)
|
|
38
|
+
* @requires useGridDimensions (event) - can be after
|
|
39
|
+
*/
|
|
40
|
+
exports.getDerivedPaginationModel = getDerivedPaginationModel;
|
|
41
|
+
const useGridPaginationModel = (apiRef, props) => {
|
|
42
|
+
const logger = (0, _utils.useGridLogger)(apiRef, 'useGridPaginationModel');
|
|
43
|
+
const densityFactor = (0, _utils.useGridSelector)(apiRef, _density.gridDensityFactorSelector);
|
|
44
|
+
const rowHeight = Math.floor(props.rowHeight * densityFactor);
|
|
45
|
+
apiRef.current.registerControlState({
|
|
46
|
+
stateId: 'paginationModel',
|
|
47
|
+
propModel: props.paginationModel,
|
|
48
|
+
propOnChange: props.onPaginationModelChange,
|
|
49
|
+
stateSelector: _gridPaginationSelector.gridPaginationModelSelector,
|
|
50
|
+
changeEvent: 'paginationModelChange'
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* API METHODS
|
|
55
|
+
*/
|
|
56
|
+
const setPage = React.useCallback(page => {
|
|
57
|
+
const currentModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
58
|
+
if (page === currentModel.page) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
logger.debug(`Setting page to ${page}`);
|
|
62
|
+
apiRef.current.setPaginationModel({
|
|
63
|
+
page,
|
|
64
|
+
pageSize: currentModel.pageSize
|
|
65
|
+
});
|
|
66
|
+
}, [apiRef, logger]);
|
|
67
|
+
const setPageSize = React.useCallback(pageSize => {
|
|
68
|
+
const currentModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
69
|
+
if (pageSize === currentModel.pageSize) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
logger.debug(`Setting page size to ${pageSize}`);
|
|
73
|
+
apiRef.current.setPaginationModel({
|
|
74
|
+
pageSize,
|
|
75
|
+
page: currentModel.page
|
|
76
|
+
});
|
|
77
|
+
}, [apiRef, logger]);
|
|
78
|
+
const setPaginationModel = React.useCallback(paginationModel => {
|
|
79
|
+
const currentModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
80
|
+
if (paginationModel === currentModel) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
logger.debug("Setting 'paginationModel' to", paginationModel);
|
|
84
|
+
apiRef.current.setState(state => (0, _extends2.default)({}, state, {
|
|
85
|
+
pagination: (0, _extends2.default)({}, state.pagination, {
|
|
86
|
+
paginationModel: getDerivedPaginationModel(state.pagination, props.signature, paginationModel)
|
|
87
|
+
})
|
|
88
|
+
}));
|
|
89
|
+
}, [apiRef, logger, props.signature]);
|
|
90
|
+
const paginationModelApi = {
|
|
91
|
+
setPage,
|
|
92
|
+
setPageSize,
|
|
93
|
+
setPaginationModel
|
|
94
|
+
};
|
|
95
|
+
(0, _utils.useGridApiMethod)(apiRef, paginationModelApi, 'public');
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* PRE-PROCESSING
|
|
99
|
+
*/
|
|
100
|
+
const stateExportPreProcessing = React.useCallback((prevState, context) => {
|
|
101
|
+
const paginationModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
102
|
+
const shouldExportPaginationModel =
|
|
103
|
+
// Always export if the `exportOnlyDirtyModels` property is not activated
|
|
104
|
+
!context.exportOnlyDirtyModels ||
|
|
105
|
+
// Always export if the `paginationModel` is controlled
|
|
106
|
+
props.paginationModel != null ||
|
|
107
|
+
// Always export if the `paginationModel` has been initialized
|
|
108
|
+
props.initialState?.pagination?.paginationModel != null ||
|
|
109
|
+
// Export if `page` or `pageSize` is not equal to the default value
|
|
110
|
+
paginationModel.page !== 0 && paginationModel.pageSize !== (0, _gridPaginationUtils.defaultPageSize)(props.autoPageSize);
|
|
111
|
+
if (!shouldExportPaginationModel) {
|
|
112
|
+
return prevState;
|
|
113
|
+
}
|
|
114
|
+
return (0, _extends2.default)({}, prevState, {
|
|
115
|
+
pagination: (0, _extends2.default)({}, prevState.pagination, {
|
|
116
|
+
paginationModel
|
|
117
|
+
})
|
|
118
|
+
});
|
|
119
|
+
}, [apiRef, props.paginationModel, props.initialState?.pagination?.paginationModel, props.autoPageSize]);
|
|
120
|
+
const stateRestorePreProcessing = React.useCallback((params, context) => {
|
|
121
|
+
const paginationModel = context.stateToRestore.pagination?.paginationModel ? (0, _extends2.default)({}, (0, _gridPaginationUtils.getDefaultGridPaginationModel)(props.autoPageSize), context.stateToRestore.pagination?.paginationModel) : (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
122
|
+
apiRef.current.setState(state => (0, _extends2.default)({}, state, {
|
|
123
|
+
pagination: (0, _extends2.default)({}, state.pagination, {
|
|
124
|
+
paginationModel: getDerivedPaginationModel(state.pagination, props.signature, paginationModel)
|
|
125
|
+
})
|
|
126
|
+
}));
|
|
127
|
+
return params;
|
|
128
|
+
}, [apiRef, props.autoPageSize, props.signature]);
|
|
129
|
+
(0, _pipeProcessing.useGridRegisterPipeProcessor)(apiRef, 'exportState', stateExportPreProcessing);
|
|
130
|
+
(0, _pipeProcessing.useGridRegisterPipeProcessor)(apiRef, 'restoreState', stateRestorePreProcessing);
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* EVENTS
|
|
134
|
+
*/
|
|
135
|
+
const handlePaginationModelChange = () => {
|
|
136
|
+
const paginationModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
137
|
+
if (apiRef.current.virtualScrollerRef?.current) {
|
|
138
|
+
apiRef.current.scrollToIndexes({
|
|
139
|
+
rowIndex: paginationModel.page * paginationModel.pageSize
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const handleUpdateAutoPageSize = React.useCallback(() => {
|
|
144
|
+
if (!props.autoPageSize) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const dimensions = apiRef.current.getRootDimensions() || {
|
|
148
|
+
viewportInnerSize: {
|
|
149
|
+
height: 0
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const pinnedRowsHeight = (0, _gridRowsUtils.calculatePinnedRowsHeight)(apiRef);
|
|
153
|
+
const maximumPageSizeWithoutScrollBar = Math.floor((dimensions.viewportInnerSize.height - pinnedRowsHeight.top - pinnedRowsHeight.bottom) / rowHeight);
|
|
154
|
+
apiRef.current.setPageSize(maximumPageSizeWithoutScrollBar);
|
|
155
|
+
}, [apiRef, props.autoPageSize, rowHeight]);
|
|
156
|
+
const handleRowCountChange = React.useCallback(newRowCount => {
|
|
157
|
+
if (newRowCount == null) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const paginationModel = (0, _gridPaginationSelector.gridPaginationModelSelector)(apiRef);
|
|
161
|
+
const pageCount = (0, _gridPaginationSelector.gridPageCountSelector)(apiRef);
|
|
162
|
+
if (paginationModel.page > pageCount - 1) {
|
|
163
|
+
apiRef.current.setPage(Math.max(0, pageCount - 1));
|
|
164
|
+
}
|
|
165
|
+
}, [apiRef]);
|
|
166
|
+
(0, _utils.useGridApiEventHandler)(apiRef, 'viewportInnerSizeChange', handleUpdateAutoPageSize);
|
|
167
|
+
(0, _utils.useGridApiEventHandler)(apiRef, 'paginationModelChange', handlePaginationModelChange);
|
|
168
|
+
(0, _utils.useGridApiEventHandler)(apiRef, 'rowCountChange', handleRowCountChange);
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* EFFECTS
|
|
172
|
+
*/
|
|
173
|
+
React.useEffect(() => {
|
|
174
|
+
apiRef.current.setState(state => (0, _extends2.default)({}, state, {
|
|
175
|
+
pagination: (0, _extends2.default)({}, state.pagination, {
|
|
176
|
+
paginationModel: getDerivedPaginationModel(state.pagination, props.signature, props.paginationModel)
|
|
177
|
+
})
|
|
178
|
+
}));
|
|
179
|
+
}, [apiRef, props.paginationModel, props.paginationMode, props.signature]);
|
|
180
|
+
React.useEffect(handleUpdateAutoPageSize, [handleUpdateAutoPageSize]);
|
|
181
|
+
};
|
|
182
|
+
exports.useGridPaginationModel = useGridPaginationModel;
|