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

[DataGrid] Allow for truncated and multiline content in grid cells #3955

Merged
merged 4 commits into from
Feb 24, 2022
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
1 change: 1 addition & 0 deletions docs/pages/api-docs/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@
"cell--textRight",
"cell--withRenderer",
"cell",
"cellContent",
"cellCheckbox",
"checkboxInput",
"columnHeader--alignCenter",
Expand Down
1 change: 1 addition & 0 deletions docs/pages/api-docs/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
"cell--textRight",
"cell--withRenderer",
"cell",
"cellContent",
"cellCheckbox",
"checkboxInput",
"columnHeader--alignCenter",
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 @@ -294,6 +294,7 @@
"cell--textRight",
"cell--withRenderer",
"cell",
"cellContent",
"cellCheckbox",
"checkboxInput",
"columnHeader--alignCenter",
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 @@ -236,6 +236,7 @@
"cell--textRight",
"cell--withRenderer",
"cell",
"cellContent",
"cellCheckbox",
"checkboxInput",
"columnHeader--alignCenter",
Expand Down
4 changes: 4 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-pro-pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@
"conditions": "the cell has a custom renderer"
},
"cell": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the cell element" },
"cellContent": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the element that wraps the cell content"
},
"cellCheckbox": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the cell checkbox element"
Expand Down
4 changes: 4 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-pro-zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@
"conditions": "the cell has a custom renderer"
},
"cell": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the cell element" },
"cellContent": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the element that wraps the cell content"
},
"cellCheckbox": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the cell checkbox element"
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 @@ -166,6 +166,10 @@
"conditions": "the cell has a custom renderer"
},
"cell": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the cell element" },
"cellContent": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the element that wraps the cell content"
},
"cellCheckbox": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the cell checkbox element"
Expand Down
4 changes: 4 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
"conditions": "the cell has a custom renderer"
},
"cell": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the cell element" },
"cellContent": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the element that wraps the cell content"
},
"cellCheckbox": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the cell checkbox element"
Expand Down
4 changes: 4 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
"conditions": "the cell has a custom renderer"
},
"cell": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the cell element" },
"cellContent": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the element that wraps the cell content"
},
"cellCheckbox": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the cell checkbox element"
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 @@ -135,6 +135,10 @@
"conditions": "the cell has a custom renderer"
},
"cell": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the cell element" },
"cellContent": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the element that wraps the cell content"
},
"cellCheckbox": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the cell checkbox element"
Expand Down
7 changes: 6 additions & 1 deletion packages/grid/_modules_/grid/components/cell/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const useUtilityClasses = (ownerState: OwnerState) => {
isEditable && 'cell--editable',
showRightBorder && 'withBorder',
],
content: ['cellContent'],
};

return composeClasses(slots, getDataGridUtilityClass, classes);
Expand Down Expand Up @@ -227,7 +228,11 @@ function GridCell(props: GridCellProps) {
{...other}
onFocus={handleFocus}
>
{children != null ? children : valueToRender?.toString()}
{children != null ? (
children
) : (
<div className={classes.content}>{valueToRender?.toString()}</div>
)}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const GridRootStyles = styled('div', {
// TODO: v6 - remove
{ [`& .${gridClasses['cell--withRenderer']}`]: styles['cell--withRenderer'] },
{ [`& .${gridClasses.cell}`]: styles.cell },
{ [`& .${gridClasses.cellContent}`]: styles.cellContent },
{ [`& .${gridClasses.cellCheckbox}`]: styles.cellCheckbox },
{ [`& .${gridClasses.checkboxInput}`]: styles.checkboxInput },
{ [`& .${gridClasses['columnHeader--alignCenter']}`]: styles['columnHeader--alignCenter'] },
Expand Down Expand Up @@ -226,10 +227,13 @@ export const GridRootStyles = styled('div', {
[`& .${gridClasses.cell}`]: {
display: 'flex',
alignItems: 'center',
overflow: 'hidden',
borderBottom: `1px solid ${borderColor}`,
DanailH marked this conversation as resolved.
Show resolved Hide resolved
},
[`& .${gridClasses.cellContent}`]: {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
borderBottom: `1px solid ${borderColor}`,
},
[`& .${gridClasses.cell}.${gridClasses['cell--editing']}`]: {
padding: 1,
Expand Down
5 changes: 5 additions & 0 deletions packages/grid/_modules_/grid/gridClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export interface GridClasses {
* Styles applied to the cell element.
*/
cell: string;
/**
* Styles applied to the element that wraps the cell content.
*/
cellContent: string;
/**
* Styles applied to the cell checkbox element.
*/
Expand Down Expand Up @@ -373,6 +377,7 @@ export const gridClasses = generateUtilityClasses('MuiDataGrid', [
'cell--textRight',
'cell--withRenderer',
'cell',
'cellContent',
'cellCheckbox',
'checkboxInput',
'columnHeader--alignCenter',
Expand Down
40 changes: 40 additions & 0 deletions packages/storybook/src/stories/grid-style.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,43 @@ export const ColumnCellRendererWithPadding = () => {
</div>
);
};
export const TruncatedCellTextSnap = () => {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGridPro
columns={[{ field: 'text' }]}
rows={[
{
id: 1,
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam a accumsan lacus. Etiam faucibus fermentum ipsum. Integer bibendum diam quis est sagittis laoreet. Pellentesque finibus lacus vitae erat aliquet dignissim.',
},
]}
/>
</div>
);
};
export const MultilineCustomCellContentSnap = () => {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGridPro
columns={[
{
field: 'name',
renderCell: (params) => (
<div style={{ display: 'flex', flexDirection: 'column' }}>
<span>{params.value}</span>
<span>{params.value}</span>
</div>
),
},
]}
rows={[
{
id: 1,
name: 'MUI',
},
]}
/>
</div>
);
};