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] Fix detail panel demo not working well with pinned columns #14883

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 17 additions & 16 deletions docs/data/data-grid/master-detail/FullWidthDetailPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ import {
randomCommodity,
} from '@mui/x-data-grid-generator';

const getDetailPanelWidth = (gridDimensions) => {
return (
gridDimensions.viewportInnerSize.width +
gridDimensions.leftPinnedWidth +
gridDimensions.rightPinnedWidth
);
};

function DetailPanelContent({ row: rowProp }) {
const apiRef = useGridApiContext();
const [width, setWidth] = React.useState(() => {
const dimensions = apiRef.current.getRootDimensions();
return dimensions.viewportInnerSize.width;
});
const [width, setWidth] = React.useState(() =>
getDetailPanelWidth(apiRef.current.getRootDimensions()),
);

const handleViewportInnerSizeChange = React.useCallback(() => {
const dimensions = apiRef.current.getRootDimensions();
setWidth(dimensions.viewportInnerSize.width);
setWidth(getDetailPanelWidth(apiRef.current.getRootDimensions()));
}, [apiRef]);

React.useEffect(() => {
Expand Down Expand Up @@ -196,21 +202,16 @@ export default function FullWidthDetailPanel() {

const getDetailPanelHeight = React.useCallback(() => 400, []);

const [pinnedColumns, setPinnedColumns] = React.useState({
left: [GRID_DETAIL_PANEL_TOGGLE_FIELD],
});

const handlePinnedColumnsChange = React.useCallback((updatedPinnedColumns) => {
setPinnedColumns(updatedPinnedColumns);
}, []);

return (
<Box sx={{ width: '100%', height: 400 }}>
<DataGridPro
columns={columns}
rows={rows}
pinnedColumns={pinnedColumns}
onPinnedColumnsChange={handlePinnedColumnsChange}
initialState={{
pinnedColumns: {
left: [GRID_DETAIL_PANEL_TOGGLE_FIELD],
},
}}
getDetailPanelHeight={getDetailPanelHeight}
getDetailPanelContent={getDetailPanelContent}
sx={{
Expand Down
38 changes: 18 additions & 20 deletions docs/data/data-grid/master-detail/FullWidthDetailPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
useGridApiContext,
GridRowParams,
GRID_DETAIL_PANEL_TOGGLE_FIELD,
GridPinnedColumnFields,
GridDimensions,
} from '@mui/x-data-grid-pro';
import {
randomCreatedDate,
Expand All @@ -23,16 +23,22 @@ import {
randomCommodity,
} from '@mui/x-data-grid-generator';

const getDetailPanelWidth = (gridDimensions: GridDimensions) => {
return (
gridDimensions.viewportInnerSize.width +
gridDimensions.leftPinnedWidth +
gridDimensions.rightPinnedWidth
);
};

function DetailPanelContent({ row: rowProp }: { row: Customer }) {
const apiRef = useGridApiContext();
const [width, setWidth] = React.useState(() => {
const dimensions = apiRef.current.getRootDimensions();
return dimensions.viewportInnerSize.width;
});
const [width, setWidth] = React.useState(() =>
getDetailPanelWidth(apiRef.current.getRootDimensions()),
);

const handleViewportInnerSizeChange = React.useCallback(() => {
const dimensions = apiRef.current.getRootDimensions();
setWidth(dimensions.viewportInnerSize.width);
setWidth(getDetailPanelWidth(apiRef.current.getRootDimensions()));
}, [apiRef]);

React.useEffect(() => {
Expand Down Expand Up @@ -201,24 +207,16 @@ export default function FullWidthDetailPanel() {

const getDetailPanelHeight = React.useCallback(() => 400, []);

const [pinnedColumns, setPinnedColumns] = React.useState<GridPinnedColumnFields>({
left: [GRID_DETAIL_PANEL_TOGGLE_FIELD],
});

const handlePinnedColumnsChange = React.useCallback(
(updatedPinnedColumns: GridPinnedColumnFields) => {
setPinnedColumns(updatedPinnedColumns);
},
[],
);

return (
<Box sx={{ width: '100%', height: 400 }}>
<DataGridPro
columns={columns}
rows={rows}
pinnedColumns={pinnedColumns}
onPinnedColumnsChange={handlePinnedColumnsChange}
initialState={{
pinnedColumns: {
left: [GRID_DETAIL_PANEL_TOGGLE_FIELD],
},
}}
getDetailPanelHeight={getDetailPanelHeight}
getDetailPanelContent={getDetailPanelContent}
sx={{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<DataGridPro
columns={columns}
rows={rows}
pinnedColumns={pinnedColumns}
onPinnedColumnsChange={handlePinnedColumnsChange}
initialState={{
pinnedColumns: {
left: [GRID_DETAIL_PANEL_TOGGLE_FIELD],
},
}}
getDetailPanelHeight={getDetailPanelHeight}
getDetailPanelContent={getDetailPanelContent}
sx={{
Expand Down
Loading