How can I have grid item elements displayed on one line? - reactjs

I'm just starting to learn Material-ui,but when I used Grid, I got an error. This is my code.
My requirement is to put the header buttons on the same line, and if I zoom out it will appear as a scroll bar
<Grid container spacing={1}>
<Grid item xs={12} md={7}>
<UserList></UserList>
</Grid>
</Grid>
but i just get this:enter image description here
and when i change my code :
<Grid container xs={12} md={7} spacing={1}>
<UserList></UserList>
</Grid>
i can get what I wanted:
enter image description here
I know the prop md of Grid must be used on item.
But how do I write to get the results I want right?
I hope someone can help me! thanks!

In the first case, you are allotting 7 columns for the grid item (i.e. your <UserList /> component). Now, the header part which includes the search icon etc. is wrapping up in those 7 columns, I guess that's the way the header part is designed (may be with flex display), but the lower part i.e. the table has a fixed width and that's why it is overflowing and resulting in a horizontal scroll. It is behaving as expected.
In the second case, the way you have written the code does not make any sense about using grid layout, it is giving you what you finally need because it is like not using any grid layout at all, <UserList /> is taking the whole width.
Please think about where you really want to use grid. Probably you don't actually want to put the whole component <UserList /> as a grid item. Still, if you want to use only one item in the grid, you probably want to give md={12} to it.

Related

Google-maps-react rendered map overlapping other content

I am quite new to coding and building my first React app. I have made good progress in styling and solving my other problems on my own, but I just cannot figure out this one: my rendered map obscures something else on the page. For styling I am using v5 of Material-UI.
To be clear, the map renders and I have no issue seeing my map in my app (sometimes), as well as having it geographically positioned to specific coordinates. The code looks like this for its specific grid:
<Grid container direction="row" rowSpacing={150}>
<Grid item xs={12} position="relative">
<MapsContainer />
</Grid>
</Grid>
Depending on the grid position property of the MapsContainer, the map either disappears completely (absolute and fixed), or just a sliver shows above the page footer unless rowSpacing is as obscene as the example code, and I lose footer functionality (no set property). When position is relative, I lose both header and footer functionality.
Regardless of the footer component position property, the map does not push it further down.
I hope this is not too convoluted to understand
*Edit: The problem came from the google-maps-react module. After pouring over it for 3 hours with a more skilled developer, not even he could figure it out, so we just broke it less by trying to reign in the div the rendered map would throw around itself. Is it fixed? Yes. Is it fixed in the proper way? Probably not. If you have the proper way to fix it, by all means share!

Does Material UI Grid Item require defined breakpoints on positional properties ? (textAlign / text-align in my case)

extension to this question: How to horizontally center an item in Material UI grid item?
I've noticed this behavior but I was unable to find corresponding explanation in my look into the Grid documentation for material UI
in essence, I have a button within a grid item that is in a grid container that I am trying to center. It looks like this
<Grid container>
<Grid item xs={9}>
</Grid>
<Grid item xs={3}>
<Grid item xs={12} style={{textAlign: "center"}}>
<Button>Create a Post</Button>
</Grid>
</Grid>
</Grid>
The code above only runs because I provided the breakpoint of xs={12}, without which it would refuse to center the element.
My question being, is this a requirement that I provide a breakpoint when I attempt to center elements?
First of all, give your Element inside a grid a fullWidth property, otherwise it is not going to display as you wish.
By default your element is not centered inside of grid, so yes, you need to provide some styles. No need to make a grid inside of another grid. If you fork your code in sandbox, I can help more.
<Grid item xs={3} style={{//center here, using flexbox or something else}}>
<Button fullWidth>Create a Post</Button>
</Grid>

How to make search bar with filter that doesn't shift based on filter length

I want to make a search bar like the one Amazon has, but right now, whenever I select a filter, it will shift the entire bar to the right.
I am using MaterialUI to do this.
Ex: Selecting "All" as filter then selecting "clothes" as filter will shift everything to the right since the "all" is smaller than "clothes". I want to display the whole string and not hide it.
Right now my code looks a bit like this:
<Grid item>
<filter/>
</Grid>
<Grid item md={9}>
<searchbar/>
</Grid>
<Grid item>
<submit/>
</Grid>

Mui Spacing V4 won't work. Spacing seems overwritten by grid size, despite new spacing enums

In short, looking to add spacing to a 2 column grid (xs={6}), but the after new spacing update, it doesnt seem to work. Has any one else had success with Mui spacing after the rework?
I've tried the example, copy/pasted, from the Mui site (https://material-ui.com/components/grid/#spacing). Of course, I was using spacing before, which was working fine, however, after the change, no spacing occurs.
<Grid container spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper}>xs=12</Paper>
</Grid>
<Grid item xs={6}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
<Grid item xs={6}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
</Grid>
Expected - while grid items are size 6, they aren't touching
Actual - grid items are touching
This feels like it should be super easy, but having used MUI SOOOO much, I'm turning to SO for help as I'm STUMPED!
The valid values for Grid's spacing property changed from v3 to v4. In v3 you need to use multiples of 8, so the equivalent of v4's spacing={3} would be spacing={24}. If you check the console, you should see errors (if using v3) about "3" being an invalid spacing value.
Here is an example (based on the code in your question) demonstrating this:
v3: https://codesandbox.io/s/grid-spacing-v3-w3ubl
v4: https://codesandbox.io/s/grid-spacing-v4-pwi73

How to align content differently for xs and md in material ui grid

I am using material ui Grid component to create a responsive grid layout. And I want to align the content inside grid depending on the screen size - alignt left for xs and align right for md. How do I do that?
I used xs and md breakpoints to divide the grid depending on screen size. Used material-ui-grid doc. The documentation also mentions a property called align-items-xs-flex-start. So I gave align-items-xs-flex-start and align-items-md-flex-end as props to Grid component to see if the content is aligned to left in xs and right in md. But it didn't work.
Some example code.
<Grid>
<Grid xs={12} md={7}>
Hi there!
</Grid>
<Grid xs={12} md={5}>
John Doe!
</Grid>
</Grid>
On a medium sized screen, I want John Doe to be aligned right. On a small sized screen, I want John Doe to come to next line and align to left. When I tried using align-items-xs-flex-start and align-items-md-flex-end, it's still aligned to left in both cases.
I know this is a old issue, but just in case somebody reach here looking for a solution, as I do, I found this article of Monica M. where it explained very well how use sx, in general, and how add different styles for different breakpoint.
https://blog.devgenius.io/how-to-use-the-sx-prop-in-mui-v5-4ccfd588836
justifyContent:{ xs: start, md: 'center'}
Just make sure you set properties for each breakpoint. In other xs property will be set as default for the others one.
Not sure if you solved this in the end? I'll answer anyway in case anyone else has themselves a similar problem:
You can then use Material UI breakpoints to set different styles for different sizes. For example, for your text alignment example, you would need something like this:
[theme.breakpoints.down('sm')]: {
textAlign: left,
},
[theme.breakpoints.up('md')]: {
textAlign: center,
}
Also, in case anyone gets confused by your example, they should know that the Grid elements should have the container and item properties to display correctly:
<Grid container>
<Grid item className="grid-el" xs={12} md={7}>
Hi there!
</Grid>
<Grid item className="grid-el" xs={12} md={5}>
John Doe!
</Grid>
</Grid>
More information on breakpoints: https://material-ui.com/customization/breakpoints/

Resources