Skip to content

Commit

Permalink
[DataGridPro] Fetch new rows only once when multiple models are chang…
Browse files Browse the repository at this point in the history
…ed in one cycle (@arminmeh) (#16382)
  • Loading branch information
arminmeh authored Jan 31, 2025
1 parent 91b4b7f commit f77d2af
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 140 deletions.
2 changes: 2 additions & 0 deletions packages/x-data-grid-generator/src/hooks/useMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type UseMockServerResponse = {
getChildrenCount?: (row: GridRowModel) => number;
fetchRows: (url: string) => Promise<GridGetRowsResponse>;
loadNewData: () => void;
isReady: boolean;
};

type DataSet = 'Commodity' | 'Employee' | 'Movies';
Expand Down Expand Up @@ -360,5 +361,6 @@ export const useMockServer = (
loadNewData: () => {
setIndex((oldIndex) => oldIndex + 1);
},
isReady: Boolean(data?.rows?.length),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GridRowId,
} from '@mui/x-data-grid';
import { gridRowGroupsToFetchSelector, GridStateInitializer } from '@mui/x-data-grid/internals';
import { unstable_debounce as debounce } from '@mui/utils';
import { GridPrivateApiPro } from '../../../models/gridApiPro';
import { DataGridProProcessedProps } from '../../../models/dataGridProProps';
import { gridGetRowsParamsSelector, gridDataSourceErrorsSelector } from './gridDataSourceSelector';
Expand Down Expand Up @@ -252,6 +253,8 @@ export const useGridDataSource = (
});
}, [apiRef]);

const debouncedFetchRows = React.useMemo(() => debounce(fetchRows, 0), [fetchRows]);

const dataSourceApi: GridDataSourceApi = {
unstable_dataSource: {
setChildrenLoading,
Expand All @@ -269,12 +272,20 @@ export const useGridDataSource = (
useGridApiMethod(apiRef, dataSourceApi, 'public');
useGridApiMethod(apiRef, dataSourcePrivateApi, 'private');

useGridApiEventHandler(apiRef, 'sortModelChange', runIfServerMode(props.sortingMode, fetchRows));
useGridApiEventHandler(apiRef, 'filterModelChange', runIfServerMode(props.filterMode, fetchRows));
useGridApiEventHandler(
apiRef,
'sortModelChange',
runIfServerMode(props.sortingMode, debouncedFetchRows),
);
useGridApiEventHandler(
apiRef,
'filterModelChange',
runIfServerMode(props.filterMode, debouncedFetchRows),
);
useGridApiEventHandler(
apiRef,
'paginationModelChange',
runIfServerMode(props.paginationMode, fetchRows),
runIfServerMode(props.paginationMode, debouncedFetchRows),
);

const isFirstRender = React.useRef(true);
Expand Down
Loading

0 comments on commit f77d2af

Please sign in to comment.