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

[docs] Add a recipe limiting to one expanded detail panel at a time #9488

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions docs/data/data-grid/master-detail/master-detail.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ Notice that the toggle column is pinned to make sure that it will always be visi

{{"demo": "FullWidthDetailPanel.js", "bg": "inline", "defaultCodeOpen": false}}

## Recipes

More examples of how to customize the detail panel:

- [One expanded detail panel at a time](/x/react-data-grid/row-recipes/#one-expanded-detail-panel-at-a-time)

## apiRef

The grid exposes a set of methods that enables all of these features using the imperative `apiRef`. To know more about how to use it, check the [API Object](/x/react-data-grid/api-object/) section.
Expand Down
90 changes: 90 additions & 0 deletions docs/data/data-grid/row-recipes/DetailPanelOneExpandedRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import { DataGridPro } from '@mui/x-data-grid-pro';
import {
randomCreatedDate,
randomCurrency,
randomEmail,
randomPrice,
} from '@mui/x-data-grid-generator';

const getDetailPanelContent = ({ row }) => (
<Box sx={{ p: 2 }}>{`Order #${row.id}`}</Box>
);
const getDetailPanelHeight = () => 50;

export default function DetailPanelOneExpandedRow() {
const [detailPanelExpandedRowIds, setDetailPanelExpandedRowIds] = React.useState(
[],
);

const handleDetailPanelExpandedRowIdsChange = React.useCallback((newIds) => {
setDetailPanelExpandedRowIds(
newIds.length > 1 ? [newIds[newIds.length - 1]] : newIds,
);
}, []);

return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGridPro
rows={rows}
columns={columns}
getDetailPanelContent={getDetailPanelContent}
getDetailPanelHeight={getDetailPanelHeight}
detailPanelExpandedRowIds={detailPanelExpandedRowIds}
onDetailPanelExpandedRowIdsChange={handleDetailPanelExpandedRowIdsChange}
/>
</Box>
);
}

const columns = [
{ 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),
},
];
99 changes: 99 additions & 0 deletions docs/data/data-grid/row-recipes/DetailPanelOneExpandedRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import {
DataGridPro,
DataGridProProps,
GridRowsProp,
GridRowId,
GridColDef,
} from '@mui/x-data-grid-pro';
import {
randomCreatedDate,
randomCurrency,
randomEmail,
randomPrice,
} from '@mui/x-data-grid-generator';

const getDetailPanelContent: DataGridProProps['getDetailPanelContent'] = ({
row,
}) => <Box sx={{ p: 2 }}>{`Order #${row.id}`}</Box>;
const getDetailPanelHeight: DataGridProProps['getDetailPanelHeight'] = () => 50;

export default function DetailPanelOneExpandedRow() {
const [detailPanelExpandedRowIds, setDetailPanelExpandedRowIds] = React.useState<
GridRowId[]
>([]);

const handleDetailPanelExpandedRowIdsChange = React.useCallback(
(newIds: GridRowId[]) => {
setDetailPanelExpandedRowIds(
newIds.length > 1 ? [newIds[newIds.length - 1]] : newIds,
);
},
[],
);

return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGridPro
rows={rows}
columns={columns}
getDetailPanelContent={getDetailPanelContent}
getDetailPanelHeight={getDetailPanelHeight}
detailPanelExpandedRowIds={detailPanelExpandedRowIds}
onDetailPanelExpandedRowIdsChange={handleDetailPanelExpandedRowIdsChange}
/>
</Box>
);
}

const columns: GridColDef[] = [
{ 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),
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<DataGridPro
rows={rows}
columns={columns}
getDetailPanelContent={getDetailPanelContent}
getDetailPanelHeight={getDetailPanelHeight}
detailPanelExpandedRowIds={detailPanelExpandedRowIds}
onDetailPanelExpandedRowIdsChange={handleDetailPanelExpandedRowIdsChange}
/>
15 changes: 15 additions & 0 deletions docs/data/data-grid/row-recipes/row-recipes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Data Grid - Row customization recipes
---

# Data Grid - Row customization recipes

<p class="description">Advanced row customization recipes.</p>

## One expanded detail panel at a time
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option was to add this example to the Master detail docs, but I decided to avoid increasing the size of the feature doc page and add it here instead.


By default, the [Master detail <span class="plan-pro" />](/x/react-data-grid/master-detail/) feature supports multiple expanded detail panels simultaneously.

However, you can [control the expanded detail panels](/x/react-data-grid/master-detail/#controlling-expanded-detail-panels) to have only one detail panel expanded at a time.

{{"demo": "DetailPanelOneExpandedRow.js", "bg": "inline", "defaultCodeOpen": false}}
1 change: 1 addition & 0 deletions docs/data/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const pages: MuiPage[] = [
{ pathname: '/x/react-data-grid/master-detail', plan: 'pro' },
{ pathname: '/x/react-data-grid/row-ordering', plan: 'pro' },
{ pathname: '/x/react-data-grid/row-pinning', plan: 'pro' },
{ pathname: '/x/react-data-grid/row-recipes', title: 'Recipes' },
],
},
{ pathname: '/x/react-data-grid/editing' },
Expand Down
7 changes: 7 additions & 0 deletions docs/pages/x/react-data-grid/row-recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import * as pageProps from 'docsx/data/data-grid/row-recipes/row-recipes.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs {...pageProps} />;
}