Skip to content

Commit

Permalink
[DataGrid] Fix cell slot style override (#11215)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Cherniavskyi <andrew@mui.com>
  • Loading branch information
oliviertassinari and cherniavskii authored Dec 6, 2023
1 parent bbdf16a commit 164a80a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
9 changes: 7 additions & 2 deletions packages/grid/x-data-grid/src/components/cell/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>((props, ref) =>
isSelected,
rowId,
tabIndex,
style: styleProp,
value,
width,
className,
Expand Down Expand Up @@ -348,9 +349,10 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>((props, ref) =>
maxWidth: width,
minHeight: height,
maxHeight: height === 'auto' ? 'none' : height, // max-height doesn't support "auto"
...styleProp,
};
return cellStyle;
}, [width, height, isNotVisible]);
}, [width, height, isNotVisible, styleProp]);

React.useEffect(() => {
if (!hasFocus || cellMode === GridCellModes.Edit) {
Expand Down Expand Up @@ -544,6 +546,7 @@ const GridCellV7 = React.forwardRef<HTMLDivElement, GridCellV7Props>((props, ref
onKeyUp,
onDragEnter,
onDragOver,
style: styleProp,
...other
} = props;

Expand Down Expand Up @@ -678,16 +681,18 @@ const GridCellV7 = React.forwardRef<HTMLDivElement, GridCellV7Props>((props, ref
opacity: 0,
width: 0,
border: 0,
...styleProp,
};
}
const cellStyle = {
minWidth: width,
maxWidth: width,
minHeight: height,
maxHeight: height === 'auto' ? 'none' : height, // max-height doesn't support "auto"
...styleProp,
};
return cellStyle;
}, [width, height, isNotVisible]);
}, [width, height, isNotVisible, styleProp]);

React.useEffect(() => {
if (!hasFocus || cellMode === GridCellModes.Edit) {
Expand Down
37 changes: 24 additions & 13 deletions packages/grid/x-data-grid/src/tests/components.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@ import * as React from 'react';
import { createRenderer, ErrorBoundary, fireEvent, screen } from '@mui-internal/test-utils';
import { expect } from 'chai';
import { spy } from 'sinon';
import { DataGrid, GridOverlay } from '@mui/x-data-grid';
import { DataGrid, GridOverlay, DataGridProps } from '@mui/x-data-grid';
import { getCell, getRow } from 'test/utils/helperFn';

describe('<DataGrid /> - Components', () => {
const { render } = createRenderer();

const baselineProps = {
rows: [
{
id: 0,
brand: 'Nike',
},
{
id: 1,
brand: 'Adidas',
},
{
id: 2,
brand: 'Puma',
},
{ id: 0, brand: 'Nike' },
{ id: 1, brand: 'Adidas' },
{ id: 2, brand: 'Puma' },
],
columns: [{ field: 'brand' }],
};
Expand Down Expand Up @@ -64,6 +55,26 @@ describe('<DataGrid /> - Components', () => {
expect(getCell(0, 0)).to.have.attr('data-name', 'foobar');
});

it('should not override cell dimensions when passing `slotProps.cell.style` to the cell', () => {
function Test(props: Partial<DataGridProps>) {
return (
<div style={{ width: 300, height: 500 }}>
<DataGrid {...baselineProps} {...props} />
</div>
);
}

const { setProps } = render(<Test slotProps={{ cell: {} }} />);

const initialCellWidth = getCell(0, 0).getBoundingClientRect().width;

setProps({ slotProps: { cell: { style: { backgroundColor: 'red' } } } });

const cell = getCell(0, 0);
expect(cell).toHaveInlineStyle({ backgroundColor: 'red' });
expect(cell.getBoundingClientRect().width).to.equal(initialCellWidth);
});

it('should pass the props from componentsProps.row to the row', () => {
render(
<div style={{ width: 300, height: 500 }}>
Expand Down

0 comments on commit 164a80a

Please sign in to comment.