forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add
renderHeader
recipe to the Master Details docs (mui#13370)
Signed-off-by: Michel Engelen <[email protected]> Co-authored-by: Andrew Cherniavskii <[email protected]> Co-authored-by: Sycamore <[email protected]>
- Loading branch information
1 parent
766c3fc
commit 6e1dcd9
Showing
6 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
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
6 changes: 6 additions & 0 deletions
6
docs/data/data-grid/row-recipes/DetailPanelCollapseAll.tsx.preview
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,6 @@ | ||
<DataGridPro | ||
rows={rows} | ||
columns={columns} | ||
getDetailPanelContent={getDetailPanelContent} | ||
getDetailPanelHeight={getDetailPanelHeight} | ||
/> |
134 changes: 134 additions & 0 deletions
134
docs/data/data-grid/row-recipes/DetailPanelExpandCollapseAll.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,134 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import IconButton from '@mui/material/IconButton'; | ||
import UnfoldLessIcon from '@mui/icons-material/UnfoldLess'; | ||
import UnfoldMoreIcon from '@mui/icons-material/UnfoldMore'; | ||
import { | ||
DataGridPro, | ||
useGridApiContext, | ||
useGridSelector, | ||
gridRowsLookupSelector, | ||
gridDetailPanelExpandedRowIdsSelector, | ||
gridDetailPanelExpandedRowsContentCacheSelector, | ||
GRID_DETAIL_PANEL_TOGGLE_COL_DEF, | ||
} from '@mui/x-data-grid-pro'; | ||
import { | ||
randomCreatedDate, | ||
randomCurrency, | ||
randomEmail, | ||
randomPrice, | ||
} from '@mui/x-data-grid-generator'; | ||
|
||
export default function DetailPanelExpandCollapseAll() { | ||
const getDetailPanelContent = React.useCallback( | ||
({ row }) => <Box sx={{ p: 2 }}>{`Order #${row.id}`}</Box>, | ||
[], | ||
); | ||
|
||
const getDetailPanelHeight = React.useCallback(() => 50, []); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGridPro | ||
rows={rows} | ||
columns={columns} | ||
getDetailPanelContent={getDetailPanelContent} | ||
getDetailPanelHeight={getDetailPanelHeight} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function CustomDetailPanelHeader() { | ||
const apiRef = useGridApiContext(); | ||
|
||
const expandedRowIds = useGridSelector( | ||
apiRef, | ||
gridDetailPanelExpandedRowIdsSelector, | ||
); | ||
const rowsWithDetailPanels = useGridSelector( | ||
apiRef, | ||
gridDetailPanelExpandedRowsContentCacheSelector, | ||
); | ||
|
||
const noDetailPanelsOpen = expandedRowIds.length === 0; | ||
|
||
const expandOrCollapseAll = () => { | ||
const dataRowIdToModelLookup = gridRowsLookupSelector(apiRef); | ||
const allRowIdsWithDetailPanels = Object.keys(rowsWithDetailPanels).map((key) => | ||
apiRef.current.getRowId(dataRowIdToModelLookup[key]), | ||
); | ||
|
||
apiRef.current.setExpandedDetailPanels( | ||
noDetailPanelsOpen ? allRowIdsWithDetailPanels : [], | ||
); | ||
}; | ||
|
||
const Icon = noDetailPanelsOpen ? UnfoldMoreIcon : UnfoldLessIcon; | ||
|
||
return ( | ||
<IconButton | ||
size="small" | ||
tabIndex={-1} | ||
onClick={expandOrCollapseAll} | ||
aria-label={noDetailPanelsOpen ? 'Expand All' : 'Collapse All'} | ||
> | ||
<Icon fontSize="inherit" /> | ||
</IconButton> | ||
); | ||
} | ||
|
||
const columns = [ | ||
{ | ||
...GRID_DETAIL_PANEL_TOGGLE_COL_DEF, | ||
renderHeader: () => <CustomDetailPanelHeader />, | ||
}, | ||
{ field: 'id', headerName: 'Order ID' }, | ||
{ field: 'customer', headerName: 'Customer', width: 200 }, | ||
{ field: 'date', type: 'date', headerName: 'Placed at' }, | ||
{ field: 'currency', headerName: 'Currency' }, | ||
{ field: 'total', type: 'number', headerName: 'Total' }, | ||
]; | ||
|
||
const rows = [ | ||
{ | ||
id: 1, | ||
customer: 'Matheus', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
{ | ||
id: 2, | ||
customer: 'Olivier', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
{ | ||
id: 3, | ||
customer: 'Flavien', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
{ | ||
id: 4, | ||
customer: 'Danail', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
{ | ||
id: 5, | ||
customer: 'Alexandre', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
]; |
138 changes: 138 additions & 0 deletions
138
docs/data/data-grid/row-recipes/DetailPanelExpandCollapseAll.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,138 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import IconButton from '@mui/material/IconButton'; | ||
import UnfoldLessIcon from '@mui/icons-material/UnfoldLess'; | ||
import UnfoldMoreIcon from '@mui/icons-material/UnfoldMore'; | ||
import { | ||
DataGridPro, | ||
GridColDef, | ||
GridRowsProp, | ||
GridRowParams, | ||
useGridApiContext, | ||
useGridSelector, | ||
gridRowsLookupSelector, | ||
gridDetailPanelExpandedRowIdsSelector, | ||
gridDetailPanelExpandedRowsContentCacheSelector, | ||
GRID_DETAIL_PANEL_TOGGLE_COL_DEF, | ||
GridRowId, | ||
} from '@mui/x-data-grid-pro'; | ||
import { | ||
randomCreatedDate, | ||
randomCurrency, | ||
randomEmail, | ||
randomPrice, | ||
} from '@mui/x-data-grid-generator'; | ||
|
||
export default function DetailPanelExpandCollapseAll() { | ||
const getDetailPanelContent = React.useCallback( | ||
({ row }: GridRowParams) => <Box sx={{ p: 2 }}>{`Order #${row.id}`}</Box>, | ||
[], | ||
); | ||
|
||
const getDetailPanelHeight = React.useCallback(() => 50, []); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGridPro | ||
rows={rows} | ||
columns={columns} | ||
getDetailPanelContent={getDetailPanelContent} | ||
getDetailPanelHeight={getDetailPanelHeight} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function CustomDetailPanelHeader() { | ||
const apiRef = useGridApiContext(); | ||
|
||
const expandedRowIds = useGridSelector( | ||
apiRef, | ||
gridDetailPanelExpandedRowIdsSelector, | ||
); | ||
const rowsWithDetailPanels = useGridSelector( | ||
apiRef, | ||
gridDetailPanelExpandedRowsContentCacheSelector, | ||
); | ||
|
||
const noDetailPanelsOpen = expandedRowIds.length === 0; | ||
|
||
const expandOrCollapseAll = () => { | ||
const dataRowIdToModelLookup = gridRowsLookupSelector(apiRef); | ||
const allRowIdsWithDetailPanels: GridRowId[] = Object.keys( | ||
rowsWithDetailPanels, | ||
).map((key) => apiRef.current.getRowId(dataRowIdToModelLookup[key])); | ||
|
||
apiRef.current.setExpandedDetailPanels( | ||
noDetailPanelsOpen ? allRowIdsWithDetailPanels : [], | ||
); | ||
}; | ||
|
||
const Icon = noDetailPanelsOpen ? UnfoldMoreIcon : UnfoldLessIcon; | ||
|
||
return ( | ||
<IconButton | ||
size="small" | ||
tabIndex={-1} | ||
onClick={expandOrCollapseAll} | ||
aria-label={noDetailPanelsOpen ? 'Expand All' : 'Collapse All'} | ||
> | ||
<Icon fontSize="inherit" /> | ||
</IconButton> | ||
); | ||
} | ||
|
||
const columns: GridColDef[] = [ | ||
{ | ||
...GRID_DETAIL_PANEL_TOGGLE_COL_DEF, | ||
renderHeader: () => <CustomDetailPanelHeader />, | ||
}, | ||
{ field: 'id', headerName: 'Order ID' }, | ||
{ field: 'customer', headerName: 'Customer', width: 200 }, | ||
{ field: 'date', type: 'date', headerName: 'Placed at' }, | ||
{ field: 'currency', headerName: 'Currency' }, | ||
{ field: 'total', type: 'number', headerName: 'Total' }, | ||
]; | ||
|
||
const rows: GridRowsProp = [ | ||
{ | ||
id: 1, | ||
customer: 'Matheus', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
{ | ||
id: 2, | ||
customer: 'Olivier', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
{ | ||
id: 3, | ||
customer: 'Flavien', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
{ | ||
id: 4, | ||
customer: 'Danail', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
{ | ||
id: 5, | ||
customer: 'Alexandre', | ||
email: randomEmail(), | ||
date: randomCreatedDate(), | ||
currency: randomCurrency(), | ||
total: randomPrice(1, 1000), | ||
}, | ||
]; |
6 changes: 6 additions & 0 deletions
6
docs/data/data-grid/row-recipes/DetailPanelExpandCollapseAll.tsx.preview
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,6 @@ | ||
<DataGridPro | ||
rows={rows} | ||
columns={columns} | ||
getDetailPanelContent={getDetailPanelContent} | ||
getDetailPanelHeight={getDetailPanelHeight} | ||
/> |
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