-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
Deploy preview: https://deploy-preview-11215--material-ui-x.netlify.app/ |
There was a problem hiding this 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.
Signed-off-by: Olivier Tassinari <[email protected]>
9639523
to
e873b5c
Compare
This comment was marked as resolved.
This comment was marked as resolved.
bf02bda
to
9ca37c6
Compare
Thanks @oliviertassinari! |
@@ -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', () => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙂
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
const initialCellWidth = getCell(0, 0).getBoundingClientRect().width; | ||
|
||
const button = screen.getByRole('button', { name: /Apply cell styles/i }); | ||
fireEvent.click(button); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
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,
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