Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGridPro] Server-side tree data support #12317

Merged
merged 107 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
3d71bf4
First rough implementation of data source with tree data
MBilalShafi Mar 4, 2024
b83276c
Minor improvement
MBilalShafi Mar 4, 2024
0354ba2
Merge branch 'next' into feat-datasource
MBilalShafi Mar 10, 2024
20b56f2
Add paginationType
MBilalShafi Mar 10, 2024
82f0446
Merge branch 'next' into feat-datasource
MBilalShafi Mar 10, 2024
0a32a43
Merge branch 'next' into feat-datasource
MBilalShafi Mar 12, 2024
9695647
Merge master into feat-datasource
MBilalShafi Mar 22, 2024
5c3e69c
useDemoDataSource, useGridServerSideCache + minor improvements
MBilalShafi Apr 2, 2024
4799165
Merge 'master' into feat-datasource
MBilalShafi Apr 5, 2024
cc10806
Merge 'master' into feat-datasource
MBilalShafi Apr 17, 2024
e6eb3e5
Merge branch 'master' into feat-datasource
MBilalShafi Apr 21, 2024
e1af3b9
Run docs:api script
MBilalShafi Apr 21, 2024
cb0d5bb
Move unstable_dataSource interface to MIT package
MBilalShafi Apr 22, 2024
0c3d45e
Remove extra export
MBilalShafi Apr 22, 2024
e11117a
Initial support for defaultGroupingExpansionDepth and isGroupExpanded…
MBilalShafi Apr 29, 2024
d682a7a
Merge branch 'master' into feat-datasource
MBilalShafi Apr 29, 2024
6a7b9ac
Cache BE data
MBilalShafi Apr 29, 2024
000ca99
Move the getRowsParams logic outside the hook
MBilalShafi Apr 29, 2024
66f0d99
Minor improvement
MBilalShafi Apr 29, 2024
24bd862
Minor docs update
MBilalShafi Apr 29, 2024
ddccb00
Updates
MBilalShafi Apr 29, 2024
1618447
Improvements
MBilalShafi May 5, 2024
931831c
Improve defaultGroupingExpansionDepth fetch logic
MBilalShafi May 6, 2024
6266921
Add error handling demo
MBilalShafi May 10, 2024
d2188c0
Merge branch 'master' into feat-datasource
MBilalShafi May 10, 2024
67e0384
Add an internal cache + a few demos
MBilalShafi May 12, 2024
7f35108
Fix client-side tree-data regression
MBilalShafi May 13, 2024
0bad3b4
Add tree data error handling, add state for data source, other minor …
MBilalShafi May 13, 2024
ac7b58c
Add more demos + rename dataSourceCache
MBilalShafi May 13, 2024
909a71d
Docs improvements
MBilalShafi May 13, 2024
0a27143
Merge branch 'master' into feat-datasource
MBilalShafi May 17, 2024
2ae9563
Try msw as a network interceptor
MBilalShafi May 19, 2024
5867429
Update API url
MBilalShafi May 19, 2024
c698b09
Add the mock server to other demos on overview
MBilalShafi May 19, 2024
1213416
Add the mock server to the tree data example
MBilalShafi May 19, 2024
acc47b4
Remove msw from many demos due to limitation + some refactors
MBilalShafi May 20, 2024
1986f74
Rename loading slate file
MBilalShafi May 20, 2024
a998152
Disable client-side features with data source
MBilalShafi May 20, 2024
9d25100
Fix broken reference
MBilalShafi May 20, 2024
af771c6
Minor improvement
MBilalShafi May 20, 2024
2f30d5c
Merge branch 'master' into feat-datasource
MBilalShafi May 27, 2024
19a4952
Minor updates
MBilalShafi May 27, 2024
2593dc8
lint
MBilalShafi May 27, 2024
444f960
lint
MBilalShafi May 27, 2024
ab621a6
Add verbose mode to 'useMockServer'
MBilalShafi May 27, 2024
9c2602b
Rename isServerSide -> hasServerChildren
MBilalShafi May 28, 2024
bb73c0e
Improve NestedDataManager
MBilalShafi May 28, 2024
c072c10
Clean proptypes
MBilalShafi May 28, 2024
3b2bb68
Add enqueue to handleCellKeyDown
MBilalShafi May 28, 2024
ae378cb
Merge branch 'master' into feat-datasource
MBilalShafi Jun 3, 2024
d7ca580
Fix collapsing of nodes using keyboard
MBilalShafi Jun 3, 2024
9ac088f
Rename enqueueChildrenFetch -> queueChildrenFetch
MBilalShafi Jun 3, 2024
9054f4e
Rename serverSide in related props to dataSource
MBilalShafi Jun 3, 2024
1f496d9
Rename variables in useDataGridXProps hooks
MBilalShafi Jun 3, 2024
21b2da4
Refactor variables, files, and folders naming
MBilalShafi Jun 3, 2024
12cedf8
Rename enums
MBilalShafi Jun 3, 2024
0d9026c
Move relevant props to dataSource interface
MBilalShafi Jun 3, 2024
102a8fe
Use useLazyRef for initilization
MBilalShafi Jun 3, 2024
ba269ea
Use derived selector for row + remove extra useCallbacks
MBilalShafi Jun 3, 2024
8b56585
Use rootProps in GridTreeDataGroupingCell
MBilalShafi Jun 3, 2024
fb3c15a
Move processQueue to run on Status change
MBilalShafi Jun 5, 2024
5e93c82
Remove hasChildren in favor of getChildrenCount
MBilalShafi Jun 5, 2024
7d0d26d
Move loading container styling to root styles
MBilalShafi Jun 5, 2024
0d42b81
Remove msw and add console printing
MBilalShafi Jun 5, 2024
f06bff6
Add TTL option to the default cache
MBilalShafi Jun 5, 2024
0b185a3
Fix prop validation
MBilalShafi Jun 5, 2024
4c28dcd
Fix loading indicator not showing with less concurrent requests
MBilalShafi Jun 5, 2024
2646b36
Fix types' issues
MBilalShafi Jun 5, 2024
53be563
Merge branch 'master' into feat-datasource
MBilalShafi Jun 5, 2024
30909ad
Fix a few issues
MBilalShafi Jun 5, 2024
3576334
Improve docs a bit
MBilalShafi Jun 5, 2024
7b119fc
Resolve a few comments
MBilalShafi Jun 6, 2024
6a58259
Update filter condition
MBilalShafi Jun 6, 2024
39d241d
Fix valueParser in serverUtils
MBilalShafi Jun 7, 2024
e3f77b8
Resolve a few comments
MBilalShafi Jun 7, 2024
c5e0cb3
docs:typescript:formatted
MBilalShafi Jun 7, 2024
4997e3f
Refactor cache API
MBilalShafi Jun 10, 2024
ffa8b79
GridDataSourceDefaultCache -> GridDataSourceCacheDefault
MBilalShafi Jun 10, 2024
8b1ced8
Make behavior of isLoading consistent
MBilalShafi Jun 10, 2024
f4ad39c
Combine API methods into one
MBilalShafi Jun 10, 2024
2bb357d
Group pubic methods under unstable_dataSource namespace
MBilalShafi Jun 10, 2024
e54602a
Cleanup
MBilalShafi Jun 10, 2024
0848913
Fix nasty cache sync bug
MBilalShafi Jun 10, 2024
df9eceb
Some docs improvements
MBilalShafi Jun 10, 2024
4fd8e49
Resolve a few comments
MBilalShafi Jun 10, 2024
e048ace
Resolve a few more comments
MBilalShafi Jun 10, 2024
655cc09
Move cache to data source hook
MBilalShafi Jun 11, 2024
f304ca5
Add comment
MBilalShafi Jun 11, 2024
957c1f3
Make the CI happy
MBilalShafi Jun 11, 2024
35b221c
State update optimization
MBilalShafi Jun 11, 2024
f8f680d
Memory optimization
MBilalShafi Jun 11, 2024
c5e69bd
Docs improvement
MBilalShafi Jun 11, 2024
f175ebf
Stop preserving null in cache state
MBilalShafi Jun 11, 2024
c240baf
Remove redundant condition
MBilalShafi Jun 13, 2024
cdb40bc
Improve comment
MBilalShafi Jun 13, 2024
a022d54
Loading state optimization
MBilalShafi Jun 13, 2024
6676357
Get rid of the swr demo
MBilalShafi Jun 13, 2024
17acfc1
Move updateServerRows to private
MBilalShafi Jun 13, 2024
3e970e7
Merge branch 'master' into feat-datasource
MBilalShafi Jun 14, 2024
b3d43ed
Rename everything to use DataSource
MBilalShafi Jun 19, 2024
39e90de
Merge branch 'master' into feat-datasource
MBilalShafi Jun 19, 2024
c13d7d4
Update a leftover
MBilalShafi Jun 19, 2024
7cdec25
lint
MBilalShafi Jun 19, 2024
1304eb4
Lint
MBilalShafi Jun 25, 2024
935c9a3
Merge branch 'master' into feat-datasource
MBilalShafi Jun 25, 2024
71c37a3
Some final docs improvements
MBilalShafi Jun 28, 2024
b6fb914
Merge branch 'master' into feat-datasource
MBilalShafi Jun 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Try msw as a network interceptor
  • Loading branch information
MBilalShafi committed May 19, 2024
commit 2ae9563681758590f0f9155536ba928e1bb626f9
72 changes: 63 additions & 9 deletions docs/data/data-grid/server-side-data/ServerSideDataGrid.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,82 @@
import * as React from 'react';
import { DataGridPro } from '@mui/x-data-grid-pro';
import CircularProgress from '@mui/material/CircularProgress';
import { lighten, darken, alpha, styled } from '@mui/material/styles';
import { useDemoDataSource } from '@mui/x-data-grid-generator';

function ServerSideDataGrid() {
const { getRows, columns, initialState } = useDemoDataSource(
{},
{ useCursorPagination: false },
function getBorderColor(theme) {
if (theme.palette.mode === 'light') {
return lighten(alpha(theme.palette.divider, 1), 0.88);
}
return darken(alpha(theme.palette.divider, 1), 0.68);
}

function getBorderRadius(theme) {
const radius = theme.shape.borderRadius;
return typeof radius === 'number' ? `${radius}px` : radius;
}

const serverOptions = { useCursorPagination: false };
const dataSetOptions = {};

const dataSource = {
getRows: async (params) => {
const urlParams = new URLSearchParams({
paginationModel: encodeURIComponent(JSON.stringify(params.paginationModel)),
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)),
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)),
groupKeys: encodeURIComponent(JSON.stringify(params.groupKeys)),
});
const serverResponse = await fetch(
`https://mui.com/x/api/x-grid?${urlParams.toString()}`,
);
const getRowsResponse = await serverResponse.json();
return {
rows: getRowsResponse.rows,
rowCount: getRowsResponse.rowCount,
};
},
};

const Div = styled('div')(({ theme }) => ({
height: 400,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
border: `1px solid ${getBorderColor(theme)}`,
borderRadius: getBorderRadius(theme),
}));

function LoadingSlate() {
return (
<Div>
<CircularProgress />
</Div>
);
}

function ServerSideDataGrid() {
const {
loading: serverConfiguring,
columns,
initialState,
} = useDemoDataSource(dataSetOptions, serverOptions);

const initialStateWithPagination = React.useMemo(
() => ({
...initialState,
pagination: {
paginationModel: { pageSize: 10, page: 0 },
rowCount: 0,
},
}),
[initialState],
);

const dataSource = React.useMemo(() => {
return {
getRows,
};
}, [getRows]);
if (serverConfiguring) {
return <LoadingSlate />;
}

return (
<div style={{ height: 400, width: '100%' }}>
Expand Down
78 changes: 68 additions & 10 deletions docs/data/data-grid/server-side-data/ServerSideDataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,86 @@
import * as React from 'react';
import { DataGridPro } from '@mui/x-data-grid-pro';
import {
DataGridPro,
GridDataSource,
GridGetRowsResponse,
} from '@mui/x-data-grid-pro';
import CircularProgress from '@mui/material/CircularProgress';
import { lighten, darken, alpha, styled, Theme } from '@mui/material/styles';
import { useDemoDataSource } from '@mui/x-data-grid-generator';

function ServerSideDataGrid() {
const { getRows, columns, initialState } = useDemoDataSource(
{},
{ useCursorPagination: false },
function getBorderColor(theme: Theme) {
if (theme.palette.mode === 'light') {
return lighten(alpha(theme.palette.divider, 1), 0.88);
}
return darken(alpha(theme.palette.divider, 1), 0.68);
}

function getBorderRadius(theme: Theme) {
const radius = theme.shape.borderRadius;
return typeof radius === 'number' ? `${radius}px` : radius;
}

const serverOptions = { useCursorPagination: false };
const dataSetOptions = {};

const dataSource: GridDataSource = {
getRows: async (params) => {
const urlParams = new URLSearchParams({
paginationModel: encodeURIComponent(JSON.stringify(params.paginationModel)),
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)),
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)),
groupKeys: encodeURIComponent(JSON.stringify(params.groupKeys)),
});
const serverResponse = await fetch(
`https://mui.com/x/api/x-grid?${urlParams.toString()}`,
);
const getRowsResponse = (await serverResponse.json()) as GridGetRowsResponse;
return {
rows: getRowsResponse.rows,
rowCount: getRowsResponse.rowCount,
};
},
};

const Div = styled('div')(({ theme }) => ({
height: 400,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
border: `1px solid ${getBorderColor(theme)}`,
borderRadius: getBorderRadius(theme),
}));

function LoadingSlate() {
return (
<Div>
<CircularProgress />
</Div>
);
}

function ServerSideDataGrid() {
const {
loading: serverConfiguring,
columns,
initialState,
} = useDemoDataSource(dataSetOptions, serverOptions);

const initialStateWithPagination = React.useMemo(
() => ({
...initialState,
pagination: {
paginationModel: { pageSize: 10, page: 0 },
rowCount: 0,
},
}),
[initialState],
);

const dataSource = React.useMemo(() => {
return {
getRows,
};
}, [getRows]);
if (serverConfiguring) {
return <LoadingSlate />;
}

return (
<div style={{ height: 400, width: '100%' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { useDemoDataSource } from '@mui/x-data-grid-generator';

const pageSizeOptions = [5, 10, 50];

const serverOptions = { useCursorPagination: false };
const dataSetOptions = {};

export default function ServerSideDataGridNoCache() {
const { getRows, columns, initialState } = useDemoDataSource(
{},
{ useCursorPagination: false },
dataSetOptions,
serverOptions,
);

const dataSource = React.useMemo(() => {
Expand Down
romgrk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { useDemoDataSource } from '@mui/x-data-grid-generator';

const pageSizeOptions = [5, 10, 50];

const serverOptions = { useCursorPagination: false };
const dataSetOptions = {};

export default function ServerSideDataGridNoCache() {
const { getRows, columns, initialState } = useDemoDataSource(
{},
{ useCursorPagination: false },
dataSetOptions,
serverOptions,
);

const dataSource = React.useMemo(() => {
Expand Down
MBilalShafi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { DataGridPro } from '@mui/x-data-grid-pro';
import { useDemoDataSource } from '@mui/x-data-grid-generator';
import { useSWRConfig } from 'swr';

const serverOptions = { useCursorPagination: false };
const dataSetOptions = {};

function ServerSideDataGridWithSWR() {
const { getRows, columns, initialState } = useDemoDataSource(
{},
{ useCursorPagination: false },
dataSetOptions,
serverOptions,
);
const { cache: swrCache } = useSWRConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
import { useDemoDataSource } from '@mui/x-data-grid-generator';
import { useSWRConfig } from 'swr';

const serverOptions = { useCursorPagination: false };
const dataSetOptions = {};

function ServerSideDataGridWithSWR() {
const { getRows, columns, initialState } = useDemoDataSource(
{},
{ useCursorPagination: false },
dataSetOptions,
serverOptions,
);
const { cache: swrCache } = useSWRConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useDemoDataSource } from '@mui/x-data-grid-generator';

const pageSizeOptions = [5, 10, 50];
const serverOptions = { useCursorPagination: false };
const datasetOptions = {};

function getBorderColor(theme) {
if (theme.palette.mode === 'light') {
Expand Down Expand Up @@ -44,7 +45,7 @@ export default function ServerSideErrorHandling() {
const [shouldRequestsFail, setShouldRequestsFail] = React.useState(false);

const { getRows, ...props } = useDemoDataSource(
{},
datasetOptions,
serverOptions,
shouldRequestsFail,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useDemoDataSource } from '@mui/x-data-grid-generator';

const pageSizeOptions = [5, 10, 50];
const serverOptions = { useCursorPagination: false };
const datasetOptions = {};

function getBorderColor(theme: Theme) {
if (theme.palette.mode === 'light') {
Expand Down Expand Up @@ -49,7 +50,7 @@ export default function ServerSideErrorHandling() {
const [shouldRequestsFail, setShouldRequestsFail] = React.useState(false);

const { getRows, ...props } = useDemoDataSource(
{},
datasetOptions,
serverOptions,
shouldRequestsFail,
);
Expand Down
24 changes: 17 additions & 7 deletions docs/data/data-grid/server-side-data/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,32 @@ const customDataSource: GridDataSource = {

The code has been significantly reduced, the need for managing the controlled states is removed, and data fetching logic is centralized.

The following demo uses a public testing API `dummyjson.com` to fetch products data with server side pagination.

{{"demo": "ServerSideDataGridDummyJson.js", "bg": "inline"}}

:::info

For the following examples, a utility `useDemoDataSource` is used to simulate the server-side data fetching based on the package `@mui/x-data-grid-generator`. It returns a function `getRows` apart from other props that could be used to create a custom data source.
For the following examples, a utility `useDemoDataSource` is used to simulate the server-side data fetching based on the package `@mui/x-data-grid-generator`. It creates a dummy server based on the mock service worker. You can replace this with your actual server-side data fetching logic.

```tsx
const { getRows, columns, initialState } = useDemoDataSource(
const { loading, columns, initialState } = useDemoDataSource(
dataSetOptions,
serverOptions,
);

const customDataSource: GridDataSource = {
getRows,
getRows: async (params: GridGetRowsParams): GetRowsResponse => {
const requestParams = new URLSearchParams({
page: params.page.toString(),
pageSize: params.pageSize.toString(),
sortModel: JSON.stringify(params.sortModel),
filterModel: JSON.stringify(params.filterModel),
});
const response = await fetch(`https://api-url?${requestParams.toString()}`);
const data = await response.json();

return {
rows: data.rows,
rowCount: data.totalCount,
};
},
};

<DataGridPro columns={columns} pagination unstable_dataSource={customDataSource} />;
Expand Down
5 changes: 5 additions & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,10 @@
"@types/webpack-bundle-analyzer": "^4.7.0",
"gm": "^1.25.0",
"serve": "^14.2.3"
},
"msw": {
MBilalShafi marked this conversation as resolved.
Show resolved Hide resolved
"workerDirectory": [
"public"
]
}
}
Loading