Skip to content

Commit

Permalink
[DataGrid] Fix autosize missing a few pixels (#10471)
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk authored Oct 2, 2023
1 parent 2d52f67 commit 75b0788
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@
"aggregationColumnHeader--alignRight",
"aggregationColumnHeaderLabel",
"autoHeight",
"autosizing",
"booleanCell",
"cell--editable",
"cell--editing",
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,7 @@
"aggregationColumnHeader--alignRight",
"aggregationColumnHeaderLabel",
"autoHeight",
"autosizing",
"booleanCell",
"cell--editable",
"cell--editing",
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@
"aggregationColumnHeader--alignRight",
"aggregationColumnHeaderLabel",
"autoHeight",
"autosizing",
"booleanCell",
"cell--editable",
"cell--editing",
Expand Down
4 changes: 4 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,10 @@
"nodeName": "the root element",
"conditions": "<code>autoHeight={true}</code>"
},
"autosizing": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root element while it is being autosized"
},
"booleanCell": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the icon of the boolean cell"
Expand Down
4 changes: 4 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@
"nodeName": "the root element",
"conditions": "<code>autoHeight={true}</code>"
},
"autosizing": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root element while it is being autosized"
},
"booleanCell": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the icon of the boolean cell"
Expand Down
4 changes: 4 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@
"nodeName": "the root element",
"conditions": "<code>autoHeight={true}</code>"
},
"autosizing": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root element while it is being autosized"
},
"booleanCell": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the icon of the boolean cell"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ function extractColumnWidths(
) {
const widthByField = {} as Record<string, number>;

const root = apiRef.current.rootElementRef!.current!;
root.classList.add(gridClasses.autosizing);

columns.forEach((column) => {
const cells = findGridCells(apiRef.current, column.field);

Expand All @@ -214,7 +217,7 @@ function extractColumnWidths(
}
const style = window.getComputedStyle(cell, null);
const paddingWidth = parseInt(style.paddingLeft, 10) + parseInt(style.paddingRight, 10);
const contentWidth = (cell.firstElementChild?.scrollWidth ?? -1) + 1;
const contentWidth = cell.firstElementChild?.getBoundingClientRect().width ?? 0;
return paddingWidth + contentWidth;
});

Expand Down Expand Up @@ -248,6 +251,8 @@ function extractColumnWidths(
widthByField[column.field] = clamp(maxContent, min, max);
});

root.classList.remove(gridClasses.autosizing);

return widthByField;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,13 @@ describe('<DataGridPro /> - Columns', () => {
await autosize({ includeHeaders: true }, [155, 177]);
});
it('.includeOutliers works', async () => {
await autosize({ includeOutliers: true }, [50, 145]);
await autosize({ includeOutliers: true }, [50, 144]);
});
it('.outliersFactor works', async () => {
await autosize({ outliersFactor: 40 }, [50, 145]);
await autosize({ outliersFactor: 40 }, [50, 144]);
});
it('.expand works', async () => {
await autosize({ expand: true }, [134, 149]);
await autosize({ expand: true }, [134, 148]);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const GridRootStyles = styled('div', {
[`&.${gridClasses['root--disableUserSelection']} .${gridClasses.cell}`]:
styles['root--disableUserSelection'],
},
{ [`&.${gridClasses.autosizing}`]: styles.autosizing },
{ [`& .${gridClasses.editBooleanCell}`]: styles.editBooleanCell },
{ [`& .${gridClasses['cell--editing']}`]: styles['cell--editing'] },
{ [`& .${gridClasses['cell--textCenter']}`]: styles['cell--textCenter'] },
Expand Down Expand Up @@ -150,6 +151,14 @@ export const GridRootStyles = styled('div', {
borderBottomColor: 'transparent',
},
},
[`&.${gridClasses.autosizing}`]: {
[`& .${gridClasses.columnHeaderTitleContainerContent} > *`]: {
overflow: 'visible !important',
},
[`& .${gridClasses.cell} > *`]: {
overflow: 'visible !important',
},
},
[`& .${gridClasses['virtualScrollerContent--overflowed']} .${gridClasses['row--lastVisible']} .${gridClasses.cell}`]:
{
borderBottomColor: 'transparent',
Expand Down
5 changes: 5 additions & 0 deletions packages/grid/x-data-grid/src/constants/gridClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface GridClasses {
* Styles applied to the root element if `autoHeight={true}`.
*/
autoHeight: string;
/**
* Styles applied to the root element while it is being autosized.
*/
autosizing: string;
/**
* Styles applied to the icon of the boolean cell.
*/
Expand Down Expand Up @@ -546,6 +550,7 @@ export const gridClasses = generateUtilityClasses<GridClassKey>('MuiDataGrid', [
'aggregationColumnHeader--alignRight',
'aggregationColumnHeaderLabel',
'autoHeight',
'autosizing',
'booleanCell',
'cell--editable',
'cell--editing',
Expand Down

0 comments on commit 75b0788

Please sign in to comment.