Skip to content

Commit

Permalink
[DataGrid] Add resizeThrottleMs prop (#12556)
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk committed Mar 26, 2024
1 parent 7f86c76 commit 2ac14fd
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@
"returned": "Promise<R> | R"
}
},
"resizeThrottleMs": { "type": { "name": "number" }, "default": "60" },
"rowBufferPx": { "type": { "name": "number" }, "default": "150" },
"rowCount": { "type": { "name": "number" } },
"rowGroupingColumnMode": {
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@
"returned": "Promise<R> | R"
}
},
"resizeThrottleMs": { "type": { "name": "number" }, "default": "60" },
"rowBufferPx": { "type": { "name": "number" }, "default": "150" },
"rowCount": { "type": { "name": "number" } },
"rowHeight": { "type": { "name": "number" }, "default": "52" },
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@
"returned": "Promise<R> | R"
}
},
"resizeThrottleMs": { "type": { "name": "number" }, "default": "60" },
"rowBufferPx": { "type": { "name": "number" }, "default": "150" },
"rowCount": { "type": { "name": "number" } },
"rowHeight": { "type": { "name": "number" }, "default": "52" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@
"Promise<R> | R": "The final values to update the row."
}
},
"resizeThrottleMs": { "description": "The milliseconds throttle delay for resizing the grid." },
"rowBufferPx": { "description": "Row region in pixels to render before/after the viewport" },
"rowCount": {
"description": "Set the total number of rows, if it is different from the length of the value <code>rows</code> prop. If some rows have children (for instance in the tree data), this number represents the amount of top level rows."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@
"Promise<R> | R": "The final values to update the row."
}
},
"resizeThrottleMs": { "description": "The milliseconds throttle delay for resizing the grid." },
"rowBufferPx": { "description": "Row region in pixels to render before/after the viewport" },
"rowCount": {
"description": "Set the total number of rows, if it is different from the length of the value <code>rows</code> prop. If some rows have children (for instance in the tree data), this number represents the amount of top level rows."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@
"Promise<R> | R": "The final values to update the row."
}
},
"resizeThrottleMs": { "description": "The milliseconds throttle delay for resizing the grid." },
"rowBufferPx": { "description": "Row region in pixels to render before/after the viewport" },
"rowCount": {
"description": "Set the total number of rows, if it is different from the length of the value <code>rows</code> prop. If some rows have children (for instance in the tree data), this number represents the amount of top level rows."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,11 @@ DataGridPremiumRaw.propTypes = {
* @returns {Promise<R> | R} The final values to update the row.
*/
processRowUpdate: PropTypes.func,
/**
* The milliseconds throttle delay for resizing the grid.
* @default 60
*/
resizeThrottleMs: PropTypes.number,
/**
* Row region in pixels to render before/after the viewport
* @default 150
Expand Down
5 changes: 5 additions & 0 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,11 @@ DataGridProRaw.propTypes = {
* @returns {Promise<R> | R} The final values to update the row.
*/
processRowUpdate: PropTypes.func,
/**
* The milliseconds throttle delay for resizing the grid.
* @default 60
*/
resizeThrottleMs: PropTypes.number,
/**
* Row region in pixels to render before/after the viewport
* @default 150
Expand Down
5 changes: 5 additions & 0 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,11 @@ DataGridRaw.propTypes = {
* @returns {Promise<R> | R} The final values to update the row.
*/
processRowUpdate: PropTypes.func,
/**
* The milliseconds throttle delay for resizing the grid.
* @default 60
*/
resizeThrottleMs: PropTypes.number,
/**
* Row region in pixels to render before/after the viewport
* @default 150
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid/src/DataGrid/useDataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const DATA_GRID_PROPS_DEFAULT_VALUES: DataGridPropsWithDefaultValues = {
pagination: false,
paginationMode: 'client',
rowHeight: 52,
resizeThrottleMs: 60,
pageSizeOptions: [25, 50, 100],
rowSpacingType: 'margin',
showCellVerticalBorder: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import {
unstable_debounce as debounce,
unstable_ownerDocument as ownerDocument,
unstable_useEnhancedEffect as useEnhancedEffect,
unstable_useEventCallback as useEventCallback,
Expand All @@ -14,6 +13,7 @@ import {
useGridApiOptionHandler,
} from '../../utils/useGridApiEventHandler';
import { useGridApiMethod } from '../../utils/useGridApiMethod';
import { throttle } from '../../../utils/throttle';
import { useGridLogger } from '../../utils/useGridLogger';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { GridDimensions, GridDimensionsApi, GridDimensionsPrivateApi } from './gridDimensionsApi';
Expand All @@ -40,6 +40,7 @@ type RootProps = Pick<
| 'autoHeight'
| 'getRowHeight'
| 'rowHeight'
| 'resizeThrottleMs'
| 'columnHeaderHeight'
>;

Expand Down Expand Up @@ -95,7 +96,10 @@ export function useGridDimensions(
const rightPinnedWidth = pinnedColumns.right.reduce((w, col) => w + col.computedWidth, 0);

const [savedSize, setSavedSize] = React.useState<ElementSize>();
const debouncedSetSavedSize = React.useMemo(() => debounce(setSavedSize, 60), []);
const debouncedSetSavedSize = React.useMemo(
() => throttle(setSavedSize, props.resizeThrottleMs),
[props.resizeThrottleMs],
);
const previousSize = React.useRef<ElementSize>();

const getRootDimensions = () => apiRef.current.state.dimensions;
Expand Down
5 changes: 5 additions & 0 deletions packages/x-data-grid/src/models/props/DataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ export interface DataGridPropsWithDefaultValues<R extends GridValidRowModel = an
* @default true
*/
rowSelection: boolean;
/**
* The milliseconds throttle delay for resizing the grid.
* @default 60
*/
resizeThrottleMs: number;
/**
* If `true`, column filters are disabled.
* @default false
Expand Down
27 changes: 27 additions & 0 deletions packages/x-data-grid/src/utils/throttle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface Cancelable {
clear(): void;
}

export function throttle<T extends (...args: any[]) => any>(func: T, wait = 166) {
let timeout: ReturnType<typeof setTimeout> | undefined;
let lastArgs: Parameters<T>;

const later = () => {
timeout = undefined;
func(...lastArgs);
};

function throttled(...args: Parameters<T>) {
lastArgs = args;
if (timeout === undefined) {
timeout = setTimeout(later, wait);
}
}

throttled.clear = () => {
clearTimeout(timeout);
timeout = undefined;
};

return throttled as T & Cancelable;
}

0 comments on commit 2ac14fd

Please sign in to comment.