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

[Grid] grid item doesn't respond to grid container's responsive columns correctly #29715

Merged
merged 11 commits into from
Dec 14, 2021

Conversation

kkorach
Copy link
Contributor

@kkorach kkorach commented Nov 16, 2021

Closes #29379

Problem:

  • Consider <Grid item xs={1} />; the 1 column prop does not carry over to the other breakpoints.

  • In generateGrid the first few lines are

  const size = ownerState[breakpoint];

  if (!size) {
    return;
  }
  • In my case where I only specify xs={1} there won't be any breakpoints in ownerState, so it will exit out of the method.

As a result, the following two show different behaviours; the first one shows the correct behaviour;
codesandbox

    <Grid container spacing={1} columns={{ xs: 4, sm: 8, md: 12 }}>
      {Array.from(Array(16)).map((_, index) => (
        <Grid item xs={1} sm={1} md={1} key={index}>
          <Item>xs=2</Item>
        </Grid>
      ))}
    </Grid>

codesandbox

    <Grid container spacing={1} columns={{ xs: 4, sm: 8, md: 12 }}>
      {Array.from(Array(16)).map((_, index) => (
        <Grid item xs={1} key={index}>
          <Item>xs=2</Item>
        </Grid>
      ))}
    </Grid>

@kkorach
Copy link
Contributor Author

kkorach commented Nov 16, 2021

@hbjORbj Ready for a review

@mui-pr-bot
Copy link

mui-pr-bot commented Nov 16, 2021

Details of bundle changes

Generated by 🚫 dangerJS against 302fbcc

// Keep 7 significant numbers.
const width = `${Math.round((size / columnValue) * 10e7) / 10e5}%`;
let more = {};
function generateGrid({ theme, ownerState }) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the first big change was to convert this method to match generateDirection and the other generate methods. This allowed me to remember the size from the previously defined breakpoint. So if xs={1} is specified and not other breakpoints, each breakpoint will get size 1. By doing it here, it won't affect ownerState where we would want sm=false if sm is not specified.

@hbjORbj hbjORbj added bug 🐛 Something doesn't work component: Grid The React component. labels Nov 17, 2021
@hbjORbj hbjORbj changed the title [Grid] Fix responsive grid when breakpoints are skipped (#29379) [Grid] Component should follow previous breakpoint value for missing breakpoints Nov 18, 2021
@hbjORbj
Copy link
Member

hbjORbj commented Nov 18, 2021

@kkorach Hey, thanks for your contribution. I edited the description to describe clearly what problem we are solving in this PR. I will review your code in more detail before the end of the day :)

@hbjORbj
Copy link
Member

hbjORbj commented Nov 18, 2021

@kkorach I can confirm that the issue is fixed here

Can you (1) revert the changes to demos and (2) add a test verifying that your changes fix the issue?

@kkorach
Copy link
Contributor Author

kkorach commented Nov 19, 2021

@hbjORbj Made the changes to Grid. I also had to fix a bug in resolveBreakpointValues. It would set the previous variable every time through the loop, regardless of whether there was a value in breakpointValues. This meant that if you passed in xs, sm, md, it would work up to lg. The pass through with xl would fail because breakpointsValue[lg] is undefined. I've changed the logic to only set previous if breakpointValues[breakpoint/i] has a value. It does make the code a little more messy.

@kkorach
Copy link
Contributor Author

kkorach commented Dec 1, 2021

What are the next steps, is there anything I need to do? @hbjORbj

Copy link
Member

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

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

@kkorach Thank you for your amazing contribution. I will merge this after one more review from another teammate.

@hbjORbj
Copy link
Member

hbjORbj commented Dec 8, 2021

@siriwatknp Argos tests are failing, but as you can see in the codesandboxes below, the two demos (before and after changes of this PR) are actually identical. Do you know why this is happening?

Before:

@siriwatknp
Copy link
Member

@siriwatknp Argos tests are failing, but as you can see in the codesandboxes below, the two demos (before and after changes of this PR) are actually identical. Do you know why this is happening?

Before:

Can you try updating the branch with the latest master?

@siriwatknp
Copy link
Member

I will merge this after one more review from another teammate.

@hbjORbj I might not have time to review it in detail. From your point of view, do you see any tests that might be needed?

@hbjORbj
Copy link
Member

hbjORbj commented Dec 9, 2021

@kkorach I had a more careful look and I was able to detect unnecessary changes. (1) I reverted your changes in resolveBreakpointValues function. (2) I reverted your changes in Grid that always produce a base of{ xs: true, sm: true, md: true, lg: true, xl: true }. We don't need to include breakpoints that were not provided by user in columns in base. (3) I like how you refactored generateGrid so that we can simply put the function along with other generate...**. (4) I made some additions to code that address the original issue. I can confirm in this codesandbox that the issue has been fixed.

@siriwatknp I added one more test. FYI, there is much less overall change now, so you should be able to review much more easily.

@hbjORbj hbjORbj changed the title [Grid] Component should follow previous breakpoint value for missing breakpoints [Grid] grid item doesn't respond to grid container's responsive columns correctly Dec 9, 2021
@hbjORbj
Copy link
Member

hbjORbj commented Dec 9, 2021

@mnajdova @siriwatknp

Update: Ignore this. I was wrong.

I think we should approve the Argos failure. Apparently, Grid used in PickersToolbar was not working properly before the change of this PR. More specifically, justify-content: space-between was not being applied previously. By looking at code, the expected behaviour is having this property.

<PickersToolbarGrid
  container
  justifyContent="space-between"
  className={classes.dateTitleContainer}
  direction={isLandscape ? landscapeDirection : 'row'}
  alignItems={isLandscape ? 'flex-start' : 'flex-end'}
>

Before the change of this PR:
Screenshot 2021-12-09 at 13 35 58
Screenshot 2021-12-09 at 13 36 08

After the change of this PR:
Screenshot 2021-12-09 at 13 38 11
Screenshot 2021-12-09 at 13 38 25

@mnajdova
Copy link
Member

mnajdova commented Dec 9, 2021

I haven't looked too deep here, but I don't understand how the changes of the PR are related to using the justifyContent property. It kind of scares me that it is introducing a braking change. I may be wrong. Regarding the new look of the DatePicker I don't have strong opinion, I would ask @danilo-leal to take a look at it.

@hbjORbj hbjORbj requested a review from mnajdova December 9, 2021 15:49
@hbjORbj
Copy link
Member

hbjORbj commented Dec 9, 2021

@mnajdova I was wrong about why the argos was failing. Now argos successfully passes. In conclusion, there are only small changes in code. I commented the two places of change. The issue that this PR was originally trying to solve is fixed and can be seen here.

@danilo-leal No need to worry about changed demos!

const width = `${Math.round((size / columnValue) * 10e7) / 10e5}%`;
let more = {};
export function generateGrid({ theme, ownerState }) {
let size;
Copy link
Member

Choose a reason for hiding this comment

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

This PR changed the scope of this variable size. As a result, the value of previous breakpoint can be used now.

typeof columnsBreakpointValues === 'object'
? columnsBreakpointValues[breakpoint]
: columnsBreakpointValues;
if (columnValue === undefined || columnValue === null) {
Copy link
Member

Choose a reason for hiding this comment

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

Another addition of this PR. This prevents width in the next line from being NaN.

@hbjORbj hbjORbj added the package: material-ui Specific to @mui/material label Dec 9, 2021
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

👍 LGTM

@siriwatknp siriwatknp merged commit 5bad7df into mui:master Dec 14, 2021
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: Grid The React component. package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Grid] grid item doesn't respond to grid container's responsive columns correctly
5 participants