Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette committed Aug 31, 2023
2 parents 8850361 + a8b474e commit 1c1b0ee
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
15 changes: 4 additions & 11 deletions packages/grid/x-data-grid-pro/src/components/GridScrollArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
unstable_useEventCallback as useEventCallback,
} from '@mui/utils';
import { styled } from '@mui/system';
import { getTotalHeaderHeight } from '@mui/x-data-grid/internals';
import { getTotalHeaderHeight, useTimeout } from '@mui/x-data-grid/internals';
import {
GridEventListener,
GridScrollParams,
Expand Down Expand Up @@ -66,7 +66,7 @@ function GridScrollAreaRaw(props: ScrollAreaProps) {
const { scrollDirection } = props;
const rootRef = React.useRef<HTMLDivElement>(null);
const apiRef = useGridApiContext();
const timeout = React.useRef<any>();
const timeout = useTimeout();
const [dragging, setDragging] = React.useState<boolean>(false);
const [canScrollMore, setCanScrollMore] = React.useState<boolean>(true);
const densityFactor = useGridSelector(apiRef, gridDensityFactorSelector);
Expand Down Expand Up @@ -124,24 +124,17 @@ function GridScrollAreaRaw(props: ScrollAreaProps) {

offset = (offset - CLIFF) * SLOP + CLIFF;

clearTimeout(timeout.current);
// Avoid freeze and inertia.
timeout.current = setTimeout(() => {
timeout.start(0, () => {
apiRef.current.scroll({
left: scrollPosition.current.left + offset,
top: scrollPosition.current.top,
});
});
},
[scrollDirection, apiRef],
[scrollDirection, apiRef, timeout],
);

React.useEffect(() => {
return () => {
clearTimeout(timeout.current);
};
}, []);

const handleColumnHeaderDragStart = useEventCallback(() => {
setDragging(true);
});
Expand Down
24 changes: 5 additions & 19 deletions packages/grid/x-data-grid/src/hooks/features/rows/useGridRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
gridRowsDataRowIdToIdLookupSelector,
gridRowMaximumTreeDepthSelector,
} from './gridRowsSelector';
import { useTimeout } from '../../utils/useTimeout';
import { GridSignature, useGridApiEventHandler } from '../../utils/useGridApiEventHandler';
import { GridStateInitializer } from '../../utils/useGridInitializeState';
import { useGridVisibleRows } from '../../utils/useGridVisibleRows';
Expand Down Expand Up @@ -89,7 +90,7 @@ export const useGridRows = (
const currentPage = useGridVisibleRows(apiRef, props);

const lastUpdateMs = React.useRef(Date.now());
const timeout = React.useRef<NodeJS.Timeout | null>(null);
const timeout = useTimeout();

const getRow = React.useCallback<GridRowApi['getRow']>(
(id) => {
Expand Down Expand Up @@ -135,7 +136,6 @@ export const useGridRows = (
const throttledRowsChange = React.useCallback(
({ cache, throttle }: { cache: GridRowsInternalCache; throttle: boolean }) => {
const run = () => {
timeout.current = null;
lastUpdateMs.current = Date.now();
apiRef.current.setState((state) => ({
...state,
Expand All @@ -151,10 +151,7 @@ export const useGridRows = (
apiRef.current.forceUpdate();
};

if (timeout.current) {
clearTimeout(timeout.current);
timeout.current = null;
}
timeout.clear();

apiRef.current.caches.rows = cache;

Expand All @@ -165,13 +162,13 @@ export const useGridRows = (

const throttleRemainingTimeMs = props.throttleRowsMs - (Date.now() - lastUpdateMs.current);
if (throttleRemainingTimeMs > 0) {
timeout.current = setTimeout(run, throttleRemainingTimeMs);
timeout.start(throttleRemainingTimeMs, run);
return;
}

run();
},
[props.throttleRowsMs, props.rowCount, props.loading, apiRef],
[props.throttleRowsMs, props.rowCount, props.loading, apiRef, timeout],
);

/**
Expand Down Expand Up @@ -583,17 +580,6 @@ export const useGridRows = (
props.signature === GridSignature.DataGrid ? 'private' : 'public',
);

/**
* EFFECTS
*/
React.useEffect(() => {
return () => {
if (timeout.current !== null) {
clearTimeout(timeout.current);
}
};
}, []);

// The effect do not track any value defined synchronously during the 1st render by hooks called after `useGridRows`
// As a consequence, the state generated by the 1st run of this useEffect will always be equal to the initialization one
const isFirstRender = React.useRef(true);
Expand Down
1 change: 1 addition & 0 deletions packages/grid/x-data-grid/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export {
getRenderableIndexes,
} from '../hooks/features/virtualization/useGridVirtualScroller';

export { useTimeout } from '../hooks/utils/useTimeout';
export { useGridVisibleRows, getVisibleRows } from '../hooks/utils/useGridVisibleRows';
export { useGridInitializeState } from '../hooks/utils/useGridInitializeState';
export type { GridStateInitializer } from '../hooks/utils/useGridInitializeState';
Expand Down

0 comments on commit 1c1b0ee

Please sign in to comment.