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] Fix cell slot style override #11215

Merged
merged 9 commits into from
Dec 6, 2023

Conversation

oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Nov 27, 2023

I noticed this in #207 (comment), the style override is broken.

You can also reproduce from this demo https://mui.com/x/react-data-grid/components/#cell,

diff --git a/docs/data/data-grid/components/CellWithPopover.tsx b/docs/data/data-grid/components/CellWithPopover.tsx
index aa193d809..9d021ddb0 100644
--- a/docs/data/data-grid/components/CellWithPopover.tsx
+++ b/docs/data/data-grid/components/CellWithPopover.tsx
@@ -33,6 +33,9 @@ export default function CellWithPopover() {
         {...data}
         slotProps={{
           cell: {
+            style: {
+              background: "red",
+            },
             onMouseEnter: handlePopoverOpen,
             onMouseLeave: handlePopoverClose,
           },

https://codesandbox.io/p/sandbox/icy-glade-xd8hxx?file=%2Fsrc%2FDemo.tsx%3A35%2C18

Before: https://codesandbox.io/p/sandbox/busy-mccarthy-83skjy?file=%2Fsrc%2FDemo.tsx%3A3%2C58
After: https://codesandbox.io/p/sandbox/mystifying-tharp-38s8cj?file=%2Fsrc%2FDemo.tsx%3A3%2C58

@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! labels Nov 27, 2023
@oliviertassinari oliviertassinari marked this pull request as ready for review November 27, 2023 17:49
@mui-bot
Copy link

mui-bot commented Nov 27, 2023

Deploy preview: https://deploy-preview-11215--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against f4f56e7

Copy link
Contributor

@romgrk romgrk left a comment

Choose a reason for hiding this comment

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

I've picked this change on the sticky PR which contains conflicts with this, this can be closed.

romgrk added a commit to romgrk/mui-x that referenced this pull request Nov 27, 2023
@oliviertassinari oliviertassinari added the on hold There is a blocker, we need to wait label Nov 27, 2023
@oliviertassinari oliviertassinari mentioned this pull request Nov 27, 2023
9 tasks
Signed-off-by: Olivier Tassinari <olivier.tassinari@gmail.com>
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 28, 2023

This comment was marked as resolved.

@cherniavskii cherniavskii changed the base branch from next to master November 28, 2023 20:00
@cherniavskii cherniavskii added v6.x customization: css Design CSS customizability and removed on hold There is a blocker, we need to wait PR: out-of-date The pull request has merge conflicts and can't be merged labels Nov 28, 2023
@cherniavskii
Copy link
Member

Thanks @oliviertassinari!
I've updated the PR to target the master branch to fix the issue for v6, added a unit test, and applied the fix to GridCellV7 👍🏻

@@ -64,6 +55,29 @@ 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', () => {
Copy link
Member

Choose a reason for hiding this comment

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

@romgrk Could you cherry-pick this test to the sticky headers PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah but I don't understand what it's testing? Aren't the styles empty both times? Also we're changing a bit the approach in sticky, we use CSS variables to set dimensions.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah but I don't understand what it's testing? Aren't the styles empty both times?

It doesn't matter what we pass in slotProps.cell.style (this is why it's an empty object).
What matters is that the cell shouldn't lose its dimensions which are set as inline styles (see the screenshot in #11215 (comment))

we're changing a bit the approach in sticky, we use CSS variables to set dimensions.

The CSS variables are passed as inline styles, so this test still makes sense there. If slotProps.cell.style is not properly merged with the internal inline styles, they will be overwritten 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like that test :| I think we should move the destructured props from this:

<div
  style={style}
  {...other}
>
  {children}
</div>

to this and call it a day:

<div
  {...other}
  style={style}
>
  {children}
</div>

Probably because I just spent weeks going through all sorts of tests, but I feel like our test suite might need some cleanup. And I don't like the idea that we keep adding tests for bugs that are very unlikely to reoccur, such as this one, because we keep paying more and more time to run it. And if we test for the style prop, why wouldn't we also test all the other props that are equally likely to be overriden?

Lmk what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

On a different note, about the test, IIUC this might be incorrect. We want to test that we preserve the cell dimensions but we never have a moment when slotProps.cell.style is undefined, therefore initialCellWidth will always return the same value, regardless if the dimensions are correct.

Copy link
Member

Choose a reason for hiding this comment

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

We want to test that we preserve the cell dimensions but we never have a moment when slotProps.cell.style is undefined,

Hmm, the initial render doesn't have any cell styles passed, so the cell width is the proper one.
After the button click, the cell styles are added. Am I missing something?

Maybe setProps instead of button click would be more readable.

Copy link
Member

Choose a reason for hiding this comment

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

I double checked and the test fails if the fix is not applied:

AssertionError: expected 48.609375 to equal 100

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair point, let's keep it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've picked up this test & fix on the sticky PR.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

@cherniavskii cherniavskii changed the title [data grid] Fix cell slot style override [DataGrid] Fix cell slot style override Nov 28, 2023
const initialCellWidth = getCell(0, 0).getBoundingClientRect().width;

const button = screen.getByRole('button', { name: /Apply cell styles/i });
fireEvent.click(button);
Copy link
Member Author

Choose a reason for hiding this comment

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

We could maybe simplify the test. One render that assert the inline style applied to the cell could be enough. But yeah, the way it's written here is more resilient to future code changes.

Should we maybe check that in addition to not break styles, we can add new ones?

Copy link
Member

Choose a reason for hiding this comment

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

Done!

romgrk added a commit to romgrk/mui-x that referenced this pull request Dec 4, 2023
@cherniavskii cherniavskii enabled auto-merge (squash) December 6, 2023 16:22
@cherniavskii cherniavskii merged commit 164a80a into mui:master Dec 6, 2023
15 checks passed
@oliviertassinari oliviertassinari deleted the fix-style-override branch December 11, 2023 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! customization: css Design CSS customizability v6.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants