I am very new to material UI, typescript, and react, so if I say something incorrect or use the wrong terminology please correct me. I have 4 boxes that I want to place on a page, but three boxes have to go in a row and stacking the 4th on the bottom of the box 1.
I currently have a grid as a parent that holds these boxes together as shown
<Grid container={true} spacing={1} justifyContent="center" alignItems='flex-start'>
<Box sx={{m: 1}} style={{width: '500px', height: '600px'}}>
<SomeData data={defaultDeploymentFrequencyData}/>
</Box>
<Box sx={{m: 1}} style={{width: '500px', height: '600px'}}>
<SomeData data={defaultDeploymentFrequencyData}/>
</Box>
<Box sx={{m: 1}} style={{width: '500px', height: '600px'}}>
<SomeData data={defaultDeploymentFrequencyData}/>
</Box>
<Box sx={{m: 1}} style={{width: '500px', height: '600px'}}>
<SomeData data={defaultDeploymentFrequencyData}/>
</Box>
</Grid>
Instead of using boxes all the time and editing them there, is there a way that i can call a method so that it aligns them correctly
export const BoxLayout = ():JSX.Element => {
return (
<Box>
sx={{
display: 'grid',
gap: 1,
gridTemplateColumns: 'repeat(2, 1fr)',
}}
</Box>
)
}
I am thinking this could be called when laying out the boxes, maybe like
<Grid container={true} spacing={1} justifyContent="center" alignItems='flex-start'>
<BoxLayout>
<SomeData data={defaultDeploymentFrequencyData}/>
<SomeData data={defaultDeploymentFrequencyData}/>
<SomeData data={defaultDeploymentFrequencyData}/>
<SomeData data={defaultDeploymentFrequencyData}/>
<BoxLayout>
</Grid>
CodePudding user response:
You also need to wrap each Box
inside <Grid item>
component.
<Grid container spacing={1}>
<Grid item xs={4}>
<Box style={{ width: 50, height: 50}}>Box</Box>
</Grid>
<Grid item xs={4}>
<Box style={{ width: 50, height: 50}}>Box</Box>
</Grid>
<Grid item xs={4}>
<Box style={{ width: 50, height: 50}}>Box</Box>
</Grid>
<Grid item xs={4}>
<Box style={{ width: 50, height: 50}}>Box</Box>
</Grid>
</Grid>
Column widths are integer values between 1 and 12; they apply at any breakpoint and indicate how many columns are occupied by the component.
So by saying xs={4}
you mean this box occupies 4/12 of the space or one third. Then last box goes to the next line under the first box.