MUI Grid - align to end - css

I'm relatively new to using MUI Grid, and am not trying to do anything too complicated, yet I haven't been able to find a solution on other posts or in the Flexbox/MUI docs. Essentially I just want to align my 'Cancel' and 'Save' buttons all the way to the right on the screen. Docs seem to make this look extremely simple, yet my page layout isn't affected. I've been trying alignItems, alignContent, justifyContent, etc. just to try and make it work - to no avail.
<Grid container xs={12} justify-content='flex-end'>
<Grid item xs={6}/>
<Grid item xs={3}>
<button
onClick={() => doSomething()}
Click here to cancel
</button>
</Grid>
<Grid item xs={3}>
<button
onClick={() => doSomething()}
Click here to Save
</button>
</Grid>
</Grid>

If you are using the Material-UI styled component. You could use the following styles definition below:
.myClass {
display: flex;
flex-direction: row;
justify-content: end;
align-items: flex-end;
}

It seems like the typing mistake with justify-content='flex-end' since it should be justifyContent="flex-end":
<Grid spacing={2} container xs={12} justifyContent="flex-end">
<Grid item xs={3}>
<Item>xs=3</Item>
</Grid>
<Grid item xs={3}>
<Item>xs=3</Item>
</Grid>
</Grid>
Working example
Also since you want to align items along just one horizontal direction you may want to use Stack rather than Grid. Stack is made for aligning items along the vertical or horizontal axis and won't create redundant for these cases DOM-elements(grid-item containers) :
<Stack direction="row" spacing={2} justifyContent="flex-end">
<Item>Item 1</Item>
<Item>Item 2</Item>
<Item>Item 3</Item>
</Stack>
The example of UI generated by this snippet can be seen at the link above.

Related

Adding margin around button components in Material UI

I have a centered Toolbar in Material UI that has 3 components. Each component is a button. I want to add a margin around each button. I tried adding the {mt} option to the component button as below, but nothing changed. I've been experimenting with makeStyles, but haven't figured it out.
<Box display="flex">
<Box m="auto">
<Toolbar>
<SeasonComponent>
<WeekComponent>
<GameComponent>
</Toolbar>
</Box>
</Box>
Season component:
return (
<div>
<Button
variant="outlined"
color="primary"
onClick={handleClickOpen}
mt={2}
>
Button text
</Button>
</div>
Here is a picture of the buttons:
You can wrap buttons in a horizontal <Stack>:
<Toolbar>
<Stack spacing={2} direction="row">
<SeasonComponent>
<WeekComponent>
<GameComponent>
</Stack>
</Toolbar>
Here's a simple example: https://codesandbox.io/s/basicbuttons-material-demo-forked-0gpgz?file=/demo.js:234-269
Rather than upgrade my repo to version 5 right now, I just added an invisible button between the buttons. Not a perfect solution, but it solved the problem in the short term.
// SpacerButton.js
import React from 'react';
import Button from '#material-ui/core/Button';
const style = {
minWidth: 1
}
export default function SpacerButton(props) {
return (
<Button variant="text" style={style}>
</Button>
);
}

Materialui - where to load CSS classes from? part2

I m starting to learn Material UI - Grid. This is a followup to the recent question I had asked
Materialui - where to load CSS classes from?
asking where to define the classes referenced by Material ui, namely classes.root
????
I modified the code as per a recommendation and now Compilation errors are as follows:
Failed to compile
./src/Materialuig.jsx
Line 25:31: 'props' is not defined no-undef
Line 30:22: 'Paper' is not defined react/jsx-no-undef
..
Here is the complete code:
import React from "react";
import MenuIcon from "#material-ui/icons/Menu";
import {
Button,
Icon,
makeStyles,
Grid,
IconButton,
AppBar,
Toolbar,
Typography,
} from "#material-ui/core";
function Materialuig(){
const useStyles = makeStyles({
root: {
backgroundColor: "red",
color: (props) => props.color,
},
});
const classes = useStyles(props);
return (
<div className={classes.root}>
<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 item xs={3}>
<Paper className={classes.paper}>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper className={classes.paper}>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper className={classes.paper}>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper className={classes.paper}>xs=3</Paper>
</Grid>
</Grid>
</div>
);
}
export default Materialuig;
You are using the Paper component, but you aren't importing it, thus it is undefined.
You can import it in the same manner as Grid by adding it to your main list of Material-UI imports:
import {
Button,
Icon,
makeStyles,
Grid,
Paper,
IconButton,
AppBar,
Toolbar,
Typography,
} from "#material-ui/core";
or you can import it as follows:
import Paper from '#material-ui/core/Paper';
props is undefined because you aren't declaring it anywhere.
You need
function Materialuig(props){
instead of
function Materialuig(){

With react and material-ui, can I have some paper expand to full width outside a Container?

If I have:
<Container maxWidth='lg'>
<Grid container spacing={3}>
<Grid item xs={12} md={6} >
<Image src="/img/undraw_programming_2svr.png" color='transparent' aspectRatio={1041 / 554} />
</Grid>
<SomeElement />
</Container>
How can I have SomeElement expand to full width?
First, A few things that were already mentioned, Your grid tagging is wrong and I understand that it's a mistake and probably not essential to your question.
The answer to your question is no, the point of the container is to stop SomeElement from expanding. A simple fix, however, would be to stop the container, render SomeElement, and then start a new container.
<Container>
...
</Container>
<SomeElement />
<Container>
...

Material UI button moves down when textField error message shown

I have added the material ui error message display to my text field, if I click on the button without any text typed on the text field, an error message will be shown, this message will push down the button, I want to stop that, I am unable to find the way to do that
This is my form code.
<Grid container justify='center' alignContent='center' >
<Grid item xs={12} >
<TextField
id="outlined-full-width"
label="Input"
style={{ width:'100%',marginTop:30 }}
placeholder="Add A Todo Item "
margin="normal"
InputLabelProps={{
shrink: true,
}}
error={this.state.errorState }
helperText={
this.state.errorState && "Item name can't be blank"
}
size="large"
variant="outlined"
value={newItem}
onChange={handleInput}
/>
</Grid>
</Grid>
<CardActions>
<Grid container justify='center' alignContent='center' >
<Grid item xs={12} md={6} >
<Button
type="submit"
variant="contained"
color='inherit'
fontsize='inherit'
style={styles.add}
startIcon={<AddIcon/>}
>
Add Item
</Button>
</Grid>
</Grid>
</CardActions>
</form>
In your main theme, add these rules so that the helperText / error text stays within the margin of your field. This will prevent the text from pushing down the content underneath.
const theme = createTheme({
// ...
MuiFormHelperText: {
root: {
// Use existing space / prevents shifting content below field
marginTop: 0,
height: 0,
},
}
})

new width of react component not respected by component next to it

I am using this component: react select.
This is my situation:
screenshot
When I add more and more tags in such a way that they don't fit inside the select component anymore - you can see position of other components (which are located next to react select) like the black "X" button are not updated, e.g. the "X" component should move to the right since width of react-select has increased. However, it doesn't. Can someone tell me what is wrong?
This is how those components (each row) is created:
<div style={{ display: "flex", verticalAlign: "middle" }}>
<Checkbox style={{ marginTop: 9, width: 40 }} checked={data.checked}
onCheck={(e, value) => { this.updateTreeNodeCheckedById(data.id, value);}} />
<Select value={data.value} simpleValue placeholder={data.label} multi
style={{marginTop:"5px", width: "300px", maxWidth:"300px"}}
onChange={(value)=>{ this.updateTreeNodeValueById(data.id, value)}}
options={[
{value:0, label:"სატესტო1"},
{value:1, label:"სატესტო2"},
{value:2, label:"სატესტო3"},
{value:3, label:"სატესტო4"}
]} />
<IconButton onClick={() => { this.deleteTreeNode(data.id)}}>
<ClearBtn />
</IconButton>
</div>

Resources