how to use grid material ui? - reactjs

I have a component whose grid looks like this:
component Region:
<Grid container spacing={10} >
<Grid item xs={4} ></Grid>
<Grid item xs={4} ></Grid>
<Grid item xs={4} ></Grid>
<Grid item xs={4} ></Grid>
<Grid item xs={4} ></Grid>
</Grid>
And I use this component in another component, which also has a series of grids..component address:
<Grid container spacing={10}>
<Grid item>
<form >
<Region></Region>
<Grid container spacing={10}>
<Grid item xs={4} ></Grid>
</Grid>
<form >
</Grid>
</Grid>
What I want is for this gride to be a continuation of the other grids, but for me it goes to the next line.
What should I do to get the grid inside the address component in the following component region?

You need to have all grid items in the same grid container. So:
remove the top container and item, because you only got one item and it doesn't make sense.
remove the container from Region component and use a Fragment
remove the container from Address
Region:
<>
<Grid item xs={4}>1</Grid>
<Grid item xs={4}>2</Grid>
<Grid item xs={4}>3</Grid>
<Grid item xs={4}>4</Grid>
<Grid item xs={4}>5</Grid>
</>
Form:
<form>
<Grid container spacing={10}>
<Region />
<Grid item xs={4} >6</Grid>
</Grid>
</form>

Related

MaterialUI - Indepdendent Grids next to each other?

I have a Grid container and two "sub-containers" each taking half the space of the parent grid. I want to stack items of different heights on each of the sub-containers, but I'm having a problem with one side's height affecting the other.
The code is extremely simple:
<Box sx={{ flexGrow: 1 }}>
<Grid container spacing={3}>
<Grid item container xs={6}>
<Grid item xs={12}>
<Item>Short items</Item>
</Grid>
<Grid item xs={12}>
<Item>Short items</Item>
</Grid>
<Grid item xs={12}>
<Item>Short items</Item>
</Grid>
</Grid>
<Grid item container xs={6}>
<Grid item xs={6}>
<Item style={{ height: "450px" }}>Tall item</Item>
</Grid>
</Grid>
</Grid>
</Box>
And also a Codesandbox forked from the documentation's. The result I want is the items on the left stacking right on top of each other.
You can add alignContent="baseline" to your left Grid container like:
<Grid item container xs={6} alignContent="baseline">
You can take a look at this sandbox for a live working example of this usage.

Is it possible to put html elements to the left side of a material UI grid?

Using material UI for the styling.
Is it possible to place an HTML element or Typography element to the left side of a Grid and have it still be on the same line?
Here is a small snippet of the code.
return (
<Wrapper>
<form>
<Grid container spacing={1}>
<Grid item xsn={8}>
<SideLabel>{labels.dates}</SideLabel>
</Grid>
<Grid item xsn={22}>
<DatePicker name="serviceDateOne" label={labels.serviceDateOne} />
</Grid>
<Grid item xsn={23}>
<DatePicker name="serviceDateTwo" label={labels.serviceDateTwo} />
</Grid>
<Grid item xsn={47} />
</Grid>
</form>
</Wrapper>
);
Lets say for example, I want to add a typography element and have it shown on the same line as grid items serviceDateOne and serviceDateTwo, what would that look like? I've tried
return (
<Wrapper>
<form>
<Typography>TEST</Typography>
<Grid container spacing={1}>
<Grid item xsn={8}>
<SideLabel>{labels.dates}</SideLabel>
</Grid>
<Grid item xsn={22}>
<DatePicker name="serviceDateOne" label={labels.serviceDateOne} />
</Grid>
<Grid item xsn={23}>
<DatePicker name="serviceDateTwo" label={labels.serviceDateTwo} />
</Grid>
<Grid item xsn={47} />
</Grid>
</form>
</Wrapper>
);
Add the Typography element is displayed above the line where serviceDateOne and serviceDateTwo are. BTW, the component is just a styled div. The element is included as part of the Grid, what i'm trying to achieve is to place that element outside of the grid and to the left of the form, acting like side labels for that row, and only have the grid items as part of the grid
Grid is using for the seperation, so just make the seperation at the top level.
return (
<>
<Grid container spacing={1}>
<Grid item xs={3} style={{ backgroundColor: "lightgray" }}>
<Typography>TEST</Typography>
</Grid>
<Grid item xs={9}>
XXX
<Grid container spacing={1}>
<Grid item xs={12}>
YYY
</Grid>
<Grid item xs={6}>
ZZ
</Grid>
<Grid item xs={6}>
ZZ
</Grid>
</Grid>
</Grid>
</Grid>
</>
);
Update
Show two child in one line: ref QA
return (
<div style={{ display: "flex" }}>
<Typography style={{ backgroundColor: "lightgray", width: 100 }}>
TEST
</Typography>
<Grid container spacing={1} style={{ backgroundColor: "gray" }}>
<Grid item xs={12}>

Better way to position elements in a grid

I try to have a header on my application, where the left item (A) is aligned to the left side of the header, the second item (B) is in the center of the header and the third item is aligned to the right.
I tried a lot and found out that only this worked for me
const AppHeader = () => {
return(
<Grid container
alignItems="baseline"
>
<Grid item xs={4}>
<Grid container justify="flex-start">
A
</Grid>
</Grid>
<Grid item xs={4}>
<Grid container justify="center">
B
</Grid>
</Grid>
<Grid item xs={4}>
<Grid container justify="flex-end">
C
</Grid>
</Grid>
</Grid>
)
}
I think this is a ugly solution, because putting a container inside an item seems so unnecessary to me. Especially if we consider that this container has no grid items. I would really like to tell the item to align its content to a position. It would be better if this solution could be more like his:
const AppHeader = () => {
return(
<Grid container
alignItems="baseline"
>
<Grid item xs={4} justify="flex-start">
A
</Grid>
<Grid item xs={4} justify="center">
B
</Grid>
<Grid item xs={4} justify="flex-end">
C
</Grid>
</Grid>
)
}
Is there a way to do something similar like that?
Assuming you are using material-ui Grid component. You can do something like this
<Grid container direction="row" justify="space-between" alignItems="center">
<div>A</div>
<div>B</div>
<div>C</div>
</Grid>
CodeSandbox Link: https://codesandbox.io/s/small-haze-1wvtr

how to turn label horizontal center in material UI in grid?

I am trying to make my label horizontally center in material UI using GRID API.
could you please tell me how I achieve this
https://codesandbox.io/s/007k3v472w
<div className="search-container">
<Grid container direction="row" spacing={24}>
<Grid item xs={6} className="abc">
<label>h</label>
</Grid>
<Grid item xs={6} className="pqr">
<label>hnnn</label>
</Grid>
</Grid>
</div>
https://material-ui.com/api/grid/
I already use alignItems but it not work
Expected out h and hnnn should be horizally center of their width.
currently they are left aligned
All you have to do add container attribute to each of your grid item and apply alignItem to make it center.
Your code should look something like this,
<Grid container direction="row" spacing={24}>
<Grid item xs={6}
container
direction="column"
justify="center"
alignItems="center"
className="abc">
<label>h</label>
</Grid>
<Grid item xs={6}
container
direction="column"
justify="center"
alignItems="center"
className="pqr">
<label>hnnn</label>
</Grid>
</Grid>
Let me know if it help!
Where you have defined your class as abc remove item and replace it with container and add justify="center"
To do it with CSS use text-align: center

Incompatibility of Material UI Grid in IE 11

I'm trying to achieve the following grid layout using the Grid component in Material UI.
I was able to achieve this layout in Chrome and Firefox using the following sample code, however in IE 11 all the grid items overlap and the textfields get expanded.
<Grid container direction='column' spacing={0}> // Grid 1 (Level0)
<Grid item xs={12}> // Grid 1 (Level1)
<Grid container direction='row' spacing={0}>
<Grid item xs={6}>
Logo
</Grid>
<Grid item xs={6}>
FormControl
</Grid>
</Grid>
</Grid>
<Grid item xs={12}> // Grid 2 (Level1)
<Grid container direction='column' spacing={0}>
<Grid item xs={12}> // Grid 1 (Level2)
<Grid container direction='row' spacing={0}>
<Grid item xs={12}>
<TextField/>
</Grid>
<Grid item xs={12}>
<TextField/>
</Grid>
</Grid>
</Grid>
<Grid item xs={12}> // Grid 2 (Level2)
<Grid container direction='row' spacing={0}>
<Grid item xs={6}>
<TextField />
</Grid>
<Grid item xs={6}>
<TextField />
</Grid>
</Grid>
</Grid>
</Grid>
</Grid>
<Grid item xs={12}> // Grid 3 (Level1)
<Grid container direction='row' spacing={0}>
<Grid item xs={6}>
Label
</Grid>
<Grid item xs={6}>
Label
</Grid>
</Grid>
</Grid>
</Grid>
Please let me know where am I going wrong!
According to the Material UI documentation, Material UI supports IE 11.
However, the Grid formatting issues as you describe them are a documented problem with a fix. The issues are a result of a Grid item not having the flex size set. In Chrome you can just use <Grid item>, but for IE you need to explicitly set the flex size <Grid item xs=12>.
How does this relate to you?
In your case it looks like your child <Grid container> components should also be <Grid item> components. That is, they should be of the form <Grid container item>, but they should also have the flex size set <Grid container item xs=12>.
Below is a sample snippet:
<Grid container>
<Grid item xs={12}>
<Grid container item xs={12}>
<Grid item xs={6}>
Logo
</Grid>
<Grid item xs={6}>
FormControl
</Grid>
</Grid>
</Grid>
</Grid>

Resources