-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] Tree data nested pagination demo #14777
Draft
MBilalShafi
wants to merge
1
commit into
mui:master
Choose a base branch
from
MBilalShafi:nested-pagination-demo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+599
−2
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
docs/data/data-grid/server-side-data/NestedPaginationGroupingCell.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import * as React from 'react'; | ||
import composeClasses from '@mui/utils/composeClasses'; | ||
import Box from '@mui/material/Box'; | ||
import Badge from '@mui/material/Badge'; | ||
import { | ||
getDataGridUtilityClass, | ||
useGridSelector, | ||
useGridRootProps, | ||
} from '@mui/x-data-grid-pro'; | ||
import { useGridPrivateApiContext } from '@mui/x-data-grid-pro/internals'; | ||
import { useGridSelectorV8, createSelectorV8 } from '@mui/x-data-grid/internals'; | ||
import CircularProgress from '@mui/material/CircularProgress'; | ||
|
||
export const gridDataSourceStateSelector = (state) => state.dataSource; | ||
|
||
export const gridDataSourceLoadingIdSelector = createSelectorV8( | ||
gridDataSourceStateSelector, | ||
(dataSource, id) => dataSource.loading[id] ?? false, | ||
); | ||
|
||
export const gridDataSourceErrorSelector = createSelectorV8( | ||
gridDataSourceStateSelector, | ||
(dataSource, id) => dataSource.errors[id], | ||
); | ||
|
||
const useUtilityClasses = (ownerState) => { | ||
const { classes } = ownerState; | ||
|
||
const slots = { | ||
root: ['treeDataGroupingCell'], | ||
toggle: ['treeDataGroupingCellToggle'], | ||
loadingContainer: ['treeDataGroupingCellLoadingContainer'], | ||
}; | ||
|
||
return composeClasses(slots, getDataGridUtilityClass, classes); | ||
}; | ||
|
||
function GridTreeDataGroupingCellIcon(props) { | ||
const apiRef = useGridPrivateApiContext(); | ||
const rootProps = useGridRootProps(); | ||
const classes = useUtilityClasses(rootProps); | ||
const { rowNode, id, field, descendantCount, row, nestedLevelRef } = props; | ||
|
||
const isDataLoading = useGridSelectorV8( | ||
apiRef, | ||
gridDataSourceLoadingIdSelector, | ||
id, | ||
); | ||
const error = useGridSelectorV8(apiRef, gridDataSourceErrorSelector, id); | ||
|
||
const expanded = rowNode.childrenExpanded || row.expanded; | ||
|
||
const handleClick = (event) => { | ||
if (!expanded) { | ||
props.setExpandedRows((prev) => [ | ||
...prev, | ||
{ | ||
...row, | ||
groupingKey: rowNode.groupingKey, | ||
expanded: true, | ||
depth: nestedLevelRef.current, | ||
}, | ||
]); | ||
if (apiRef.current.state.pagination.paginationModel.page > 0) { | ||
apiRef.current.setPage(0); | ||
} | ||
} else if (row.expanded) { | ||
props.setExpandedRows((prev) => { | ||
const index = prev.findIndex((r) => r.id === id); | ||
return prev.slice(0, index); | ||
}); | ||
if (apiRef.current.state.pagination.paginationModel.page > 0) { | ||
apiRef.current.setPage(0); | ||
} | ||
} else { | ||
apiRef.current.setRowChildrenExpansion(id, !expanded); | ||
} | ||
apiRef.current.setCellFocus(id, field); | ||
event.stopPropagation(); // TODO remove event.stopPropagation | ||
}; | ||
|
||
const Icon = expanded | ||
? rootProps.slots.treeDataCollapseIcon | ||
: rootProps.slots.treeDataExpandIcon; | ||
|
||
if (isDataLoading) { | ||
return ( | ||
<div className={classes.loadingContainer}> | ||
<CircularProgress size="1rem" color="inherit" /> | ||
</div> | ||
); | ||
} | ||
return descendantCount > 0 ? ( | ||
<rootProps.slots.baseIconButton | ||
size="small" | ||
onClick={handleClick} | ||
tabIndex={-1} | ||
aria-label={ | ||
rowNode.childrenExpanded | ||
? apiRef.current.getLocaleText('treeDataCollapse') | ||
: apiRef.current.getLocaleText('treeDataExpand') | ||
} | ||
{...rootProps?.slotProps?.baseIconButton} | ||
> | ||
<rootProps.slots.baseTooltip title={error?.message ?? null}> | ||
<Badge variant="dot" color="error" invisible={!error}> | ||
<Icon fontSize="inherit" /> | ||
</Badge> | ||
</rootProps.slots.baseTooltip> | ||
</rootProps.slots.baseIconButton> | ||
) : null; | ||
} | ||
|
||
export function NestedPaginationGroupingCell(props) { | ||
const { | ||
id, | ||
field, | ||
formattedValue, | ||
rowNode, | ||
hideDescendantCount, | ||
offsetMultiplier = 2, | ||
setExpandedRows, | ||
nestedLevelRef, | ||
} = props; | ||
|
||
const rootProps = useGridRootProps(); | ||
const apiRef = useGridPrivateApiContext(); | ||
const rowSelector = (state) => state.rows.dataRowIdToModelLookup[id]; | ||
const row = useGridSelector(apiRef, rowSelector); | ||
const classes = useUtilityClasses(rootProps); | ||
|
||
let descendantCount = 0; | ||
if (row) { | ||
descendantCount = Math.max( | ||
rootProps.unstable_dataSource?.getChildrenCount?.(row) ?? 0, | ||
0, | ||
); | ||
} | ||
|
||
let depth = row.depth ? row.depth : rowNode.depth; | ||
if (!row.expanded && nestedLevelRef.current > 0) { | ||
depth = nestedLevelRef.current; | ||
} | ||
|
||
return ( | ||
<Box className={classes.root} sx={{ ml: depth * offsetMultiplier }}> | ||
<div className={classes.toggle}> | ||
<GridTreeDataGroupingCellIcon | ||
id={id} | ||
field={field} | ||
rowNode={rowNode} | ||
row={row} | ||
setExpandedRows={setExpandedRows} | ||
nestedLevelRef={nestedLevelRef} | ||
descendantCount={descendantCount} | ||
/> | ||
</div> | ||
<span> | ||
{formattedValue === undefined | ||
? (rowNode.groupingKey ?? row.groupingKey) | ||
: formattedValue} | ||
{!hideDescendantCount && descendantCount > 0 ? ` (${descendantCount})` : ''} | ||
</span> | ||
</Box> | ||
); | ||
} |
199 changes: 199 additions & 0 deletions
199
docs/data/data-grid/server-side-data/NestedPaginationGroupingCell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import * as React from 'react'; | ||
import composeClasses from '@mui/utils/composeClasses'; | ||
import Box from '@mui/material/Box'; | ||
import Badge from '@mui/material/Badge'; | ||
import { | ||
getDataGridUtilityClass, | ||
GridRenderCellParams, | ||
GridDataSourceGroupNode, | ||
useGridSelector, | ||
useGridRootProps, | ||
DataGridProProcessedProps, | ||
GridPrivateApiPro, | ||
GridStatePro, | ||
} from '@mui/x-data-grid-pro'; | ||
import { useGridPrivateApiContext } from '@mui/x-data-grid-pro/internals'; | ||
import { useGridSelectorV8, createSelectorV8 } from '@mui/x-data-grid/internals'; | ||
import CircularProgress from '@mui/material/CircularProgress'; | ||
|
||
export const gridDataSourceStateSelector = (state: GridStatePro) => state.dataSource; | ||
|
||
export const gridDataSourceLoadingIdSelector = createSelectorV8( | ||
gridDataSourceStateSelector, | ||
(dataSource, id: GridRowId) => dataSource.loading[id] ?? false, | ||
); | ||
|
||
export const gridDataSourceErrorSelector = createSelectorV8( | ||
gridDataSourceStateSelector, | ||
(dataSource, id: GridRowId) => dataSource.errors[id], | ||
); | ||
|
||
type OwnerState = DataGridProProcessedProps; | ||
|
||
const useUtilityClasses = (ownerState: OwnerState) => { | ||
const { classes } = ownerState; | ||
|
||
const slots = { | ||
root: ['treeDataGroupingCell'], | ||
toggle: ['treeDataGroupingCellToggle'], | ||
loadingContainer: ['treeDataGroupingCellLoadingContainer'], | ||
}; | ||
|
||
return composeClasses(slots, getDataGridUtilityClass, classes); | ||
}; | ||
|
||
interface GridTreeDataGroupingCellProps | ||
extends GridRenderCellParams<any, any, any, GridDataSourceGroupNode> { | ||
hideDescendantCount?: boolean; | ||
/** | ||
* The cell offset multiplier used for calculating cell offset (`rowNode.depth * offsetMultiplier` px). | ||
* @default 2 | ||
*/ | ||
offsetMultiplier?: number; | ||
} | ||
|
||
interface GridTreeDataGroupingCellIconProps | ||
extends Pick<GridTreeDataGroupingCellProps, 'id' | 'field' | 'rowNode' | 'row'> { | ||
descendantCount: number; | ||
} | ||
|
||
function GridTreeDataGroupingCellIcon( | ||
props: GridTreeDataGroupingCellIconProps & { | ||
setExpandedRows: (rows: GridValidRowModel[]) => void; | ||
nestedLevelRef: React.RefObject<number>; | ||
}, | ||
) { | ||
const apiRef = | ||
useGridPrivateApiContext() as React.MutableRefObject<GridPrivateApiPro>; | ||
const rootProps = useGridRootProps(); | ||
const classes = useUtilityClasses(rootProps); | ||
const { rowNode, id, field, descendantCount, row, nestedLevelRef } = props; | ||
|
||
const isDataLoading = useGridSelectorV8( | ||
apiRef, | ||
gridDataSourceLoadingIdSelector, | ||
id, | ||
); | ||
const error = useGridSelectorV8(apiRef, gridDataSourceErrorSelector, id); | ||
|
||
const expanded = rowNode.childrenExpanded || row.expanded; | ||
|
||
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
if (!expanded) { | ||
props.setExpandedRows((prev) => [ | ||
...prev, | ||
{ | ||
...row, | ||
groupingKey: rowNode.groupingKey, | ||
expanded: true, | ||
depth: nestedLevelRef.current, | ||
}, | ||
]); | ||
if (apiRef.current.state.pagination.paginationModel.page > 0) { | ||
apiRef.current.setPage(0); | ||
} | ||
} else if (row.expanded) { | ||
props.setExpandedRows((prev) => { | ||
const index = prev.findIndex((r) => r.id === id); | ||
return prev.slice(0, index); | ||
}); | ||
if (apiRef.current.state.pagination.paginationModel.page > 0) { | ||
apiRef.current.setPage(0); | ||
} | ||
} else { | ||
apiRef.current.setRowChildrenExpansion(id, !expanded); | ||
} | ||
apiRef.current.setCellFocus(id, field); | ||
event.stopPropagation(); // TODO remove event.stopPropagation | ||
}; | ||
|
||
const Icon = expanded | ||
? rootProps.slots.treeDataCollapseIcon | ||
: rootProps.slots.treeDataExpandIcon; | ||
|
||
if (isDataLoading) { | ||
return ( | ||
<div className={classes.loadingContainer}> | ||
<CircularProgress size="1rem" color="inherit" /> | ||
</div> | ||
); | ||
} | ||
return descendantCount > 0 ? ( | ||
<rootProps.slots.baseIconButton | ||
size="small" | ||
onClick={handleClick} | ||
tabIndex={-1} | ||
aria-label={ | ||
rowNode.childrenExpanded | ||
? apiRef.current.getLocaleText('treeDataCollapse') | ||
: apiRef.current.getLocaleText('treeDataExpand') | ||
} | ||
{...rootProps?.slotProps?.baseIconButton} | ||
> | ||
<rootProps.slots.baseTooltip title={error?.message ?? null}> | ||
<Badge variant="dot" color="error" invisible={!error}> | ||
<Icon fontSize="inherit" /> | ||
</Badge> | ||
</rootProps.slots.baseTooltip> | ||
</rootProps.slots.baseIconButton> | ||
) : null; | ||
} | ||
|
||
export function NestedPaginationGroupingCell( | ||
props: GridTreeDataGroupingCellProps & { | ||
setExpandedRows: (rows: GridValidRowModel[]) => void; | ||
nestedLevelRef: React.RefObject<number>; | ||
}, | ||
) { | ||
const { | ||
id, | ||
field, | ||
formattedValue, | ||
rowNode, | ||
hideDescendantCount, | ||
offsetMultiplier = 2, | ||
setExpandedRows, | ||
nestedLevelRef, | ||
} = props; | ||
|
||
const rootProps = useGridRootProps(); | ||
const apiRef = useGridPrivateApiContext(); | ||
const rowSelector = (state: GridStatePro) => state.rows.dataRowIdToModelLookup[id]; | ||
const row = useGridSelector(apiRef, rowSelector); | ||
const classes = useUtilityClasses(rootProps); | ||
|
||
let descendantCount = 0; | ||
if (row) { | ||
descendantCount = Math.max( | ||
rootProps.unstable_dataSource?.getChildrenCount?.(row) ?? 0, | ||
0, | ||
); | ||
} | ||
|
||
let depth = row.depth ? row.depth : rowNode.depth; | ||
if (!row.expanded && nestedLevelRef.current > 0) { | ||
depth = nestedLevelRef.current; | ||
} | ||
|
||
return ( | ||
<Box className={classes.root} sx={{ ml: depth * offsetMultiplier }}> | ||
<div className={classes.toggle}> | ||
<GridTreeDataGroupingCellIcon | ||
id={id} | ||
field={field} | ||
rowNode={rowNode} | ||
row={row} | ||
setExpandedRows={setExpandedRows} | ||
nestedLevelRef={nestedLevelRef} | ||
descendantCount={descendantCount} | ||
/> | ||
</div> | ||
<span> | ||
{formattedValue === undefined | ||
? (rowNode.groupingKey ?? row.groupingKey) | ||
: formattedValue} | ||
{!hideDescendantCount && descendantCount > 0 ? ` (${descendantCount})` : ''} | ||
</span> | ||
</Box> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self note, export these selectors