materriel -ui ToggleButton looks like button - css

I have a ToggleButton
I want the selection to look like a button
But when I put a button inside I get such an error in the console
const StyledToggleButtonGroup = withStyles((theme) => ({
grouped: {
margin: theme.spacing(0.5),
border: 'none',
},
}))(ToggleButtonGroup);
<StyledToggleButtonGroup size="medium" value={problem} exclusive onChange={handleChange}>
<ToggleButton color="red" value="technical" className={helpClasses.boxButton}>
<Button size="medium" fullWidth color="primary" variant="outlined">
{t('help.technical')}
</Button>
</ToggleButton>
</StyledToggleButtonGroup>
<DialogActions>
error in console
Warning: validateDOMNesting(...): <button> cannot appear as a descendant of <button>.
I want it to look like this

From the documentation, https://material-ui.com/components/toggle-button/
It says, you can put icons inside ToggleButton, or most likely anything other than Button.
Try follow the doc,
<ToggleButtonGroup
value={alignment}
exclusive
onChange={handleAlignment}
aria-label="text alignment"
>
<ToggleButton value="left" aria-label="left aligned">
<FormatAlignLeftIcon />
</ToggleButton>
<ToggleButton value="center" aria-label="centered">
<FormatAlignCenterIcon />
</ToggleButton>
<ToggleButton value="right" aria-label="right aligned">
<FormatAlignRightIcon />
</ToggleButton>
<ToggleButton value="justify" aria-label="justified" disabled>
<FormatAlignJustifyIcon />
</ToggleButton>
</ToggleButtonGroup>

I did it so
const classes = makeStyles((theme) => ({
boxToggleButton: {
borderRadius: '5px',
borderStyle: 'solid',
border: 1,
width: '6vw',
height: '6vw',
},
textToggleButton: {
color: colors.light.background,
fontWeight: 400,
fontSize: fontSizes.large,
},
}));
<ToggleButton value="Wrong identification ">
<Grid
container
justify="center"
alignItems="center"
item
className={classes.boxToggleButton}
>
<Typography className={classes.textToggleButton}>
{"wrongIdentification"}
</Typography>
</Grid>
</ToggleButton>;

Related

How to align startIcon material ui icon at the center?

I'm attempting to keep the icon button center aligned, but something isn't working right. Adding left: "0.5rem" to sx prop is pushing the button icon further. I'm trying not to use makeStyles to override the position. Any tip/direction would be helpful :)
Sandbox link
You can try using style:
<Stack direction="row" style={{display:"flex", justifyContent:"center", alignItems:"center"}}>
<Tooltip title="Delete">
<Button sx={{ minWidth: 0 }} startIcon={<DeleteIcon />} />
</Tooltip>
<Divider orientation="vertical" flexItem />
<Tooltip title="Send">
<Button sx={{ minWidth: 0 }} startIcon={<SendIcon />} />
</Tooltip>
<Tooltip title="Headset">
<Button sx={{ minWidth: 0 }} startIcon={<HeadsetMicOutlined />} />
</Tooltip>
<Divider orientation="vertical" flexItem />
</Stack>
In case you want to set it in you theme here is how you can do it so you don't have to do it in every Button component.
export const theme = createTheme({
...
components: {
MuiButton: {
styleOverrides: {
startIcon: {
margin: '0'
},
},
}
},
});

how to make a height responsive textfield in Material UI?

I need to make a textfield with a responsive height so the height will always adjust itself to the size of the parent grid when the browser is minimized/maximized.
I looked up for a solution and I only found this:
Responsive MaterialUI TextField?
and it only gave me an idea about how to solve the same problem that I had with the width.
it didn't work with the height.
here is my code:
const useStyles = makeStyles({
infogrid: {
width: '30%',
height: '39%',
display: 'inline-block',
position: 'absolute',
right: '8%',
top: '10%',
backgroundColor: '#00ffff',
borderRadius: '5vh',
alignItems: 'center',
justifyContent: 'center',
justifyItems: 'center',
flexDirection: 'column',
overflow: 'auto'
},
label: {
margin: '25%',
marginTop: '0.3%',
marginBottom: '0.3%',
display: 'block',
height: '9%',
width: '50%',
}
})
export default function App() {
const classes = useStyles()
<Grid className={classes.infogrid}>
<TextField fullWidth className={classes.label} id="ovalID" label="1" variant="outlined" />
<TextField fullWidth className={classes.label} id="latitude" label="2" variant="outlined" />
<TextField fullWidth className={classes.label} id="longitude" label="3" variant="outlined" />
<TextField fullWidth className={classes.label} id="ovalRadius1" label="4" variant="outlined" />
<TextField fullWidth className={classes.label} id="ovalRadius2" label="5" variant="outlined" />
<TextField fullWidth className={classes.label} id="angel" label="6" variant="outlined" />
</Grid>
);
}
Just wrap everything inside your <Grid> with a <Stack> component.
Example
<Grid>
<Stack>
<TextField />
<TextField />
<TextField />
<TextField />
</Stack>
</Grid>
Should make your TextField height responsive in the grid items.
I just solved it by changing the parent grid into a div and changing the display setting of the div to 'flex'. Also, I set the marginTop and marginBottom settings in the 'labels' class to be a little bit higher.
As long as the parent div have the flex direction set to 'column'.

Reactjs / Material-UI: className does not work on the ToggleButton component [duplicate]

This question already has an answer here:
Use calc() function in makeStyles
(1 answer)
Closed 1 year ago.
Using React / Material UI, I am trying to apply styles to a group of ToggleButtons .
Currently I can only define the style prop for each ToggleButton to make it work.
I am trying to do className={...} instead, to make the code better.
However, I found that this does not work for ToggleButton components:
import ToggleButton from '#mui/material/ToggleButton';
import ToggleButtonGroup from '#mui/material/ToggleButtonGroup';
import useStyles from './styles';
const DashboardSettings = () = {
const classes = useStyles();
return (
<Fragment>
<Paper className={classes.paper} elevation={10}> // here works
<Typography variant="h4" gutterBottom>
Settings
</Typography>
<br />
<br />
<Grid spacing={3} container>
<Grid xs={12} item>
<Grid container>
<Grid item xs={12}>
<p>Holiday(s): </p>
</Grid>
<Grid item xs={1}></Grid>
<Grid item xs={10}>
<ToggleButtonGroup
// value={formats}
onChange={() => {}}
// fullWidth
aria-label="text formatting"
mt={10}
>
<ToggleButton value="mon" className={classes.toggleButton}> // here it has no effect!
<p>Monday</p>
</ToggleButton>
<ToggleButton value="mon" style={{marginRight: "5px", marginLeft: "5px", backgroundColor: "#FCDC00"}}>
<p>Monday</p>
</ToggleButton>
<ToggleButton value="tue" style={{marginRight: "5px", marginLeft: "5px", backgroundColor: "#FCDC00"}}>
<p>Tuesday</p>
</ToggleButton>
<ToggleButton value="wed" style={{marginRight: "5px", marginLeft: "5px", backgroundColor: "#FCDC00"}}>
<p>Wednesday</p>
</ToggleButton>
<ToggleButton value="thu" style={{marginRight: "5px", marginLeft: "5px", backgroundColor: "#FCDC00"}}>
<p>Thursday</p>
</ToggleButton>
<ToggleButton value="fri" style={{marginRight: "5px", marginLeft: "5px", backgroundColor: "#FCDC00"}}>
<p>Friday</p>
</ToggleButton>
<ToggleButton value="sat" style={{marginRight: "5px", marginLeft: "5px", backgroundColor: "#FCDC00"}}>
<p>Saturday</p>
</ToggleButton>
<ToggleButton value="sun" style={{marginRight: "5px", marginLeft: "5px", backgroundColor: "#FCDC00"}}>
<p>Sunday</p>
</ToggleButton>
</ToggleButtonGroup>
</Grid>
<Grid item xs={1}></Grid>
</Grid>
</Grid>
)
}
./styles.js:
import { makeStyles } from '#material-ui/core';
const useStyles = makeStyles((theme) => ({
paper: {
marginTop: theme.spacing(3),
marginBottom: theme.spacing(3),
padding: theme.spacing(20),
[theme.breakpoints.up(600 + theme.spacing(3) * 2)]: {
marginTop: theme.spacing(6),
marginBottom: theme.spacing(6),
padding: theme.spacing(3),
},
},
toggleButton: {
marginRight: "5px",
marginLeft: "5px",
color: "#000000",
backgroundColor: "#FFFFFF"
},
}));
export default useStyles;
Why does it not work?
here is a view of it:
How can I apply styles to these buttons in a neater way then?
these components are not dom(document object model) elements. instead, they are a wrapper around the dom-elements and they pass some of the values to element attributes through props. so if props aren't.
Also it might be the case that when you provide styles through className in that scenarios style directly provided to element have more specificity than you class-Styles. it's better to use decorators like !important.
ex.
height: '120px !important',
export const ToggleButton = (props) => {
//you can only pass those props which is being used here.
// here i am using className as props and further i have passed to original elements.
// in your case there is no className attribute.
const {onClick, style, className, children, text, type} = props;
return (
<button onClick={onClick} style={style} className={className} type={type}>
{children || text}
</button>
)
}
available then you can't pass that to the component as there is no implementation for the same.

Material Ui TextField how to change color of border

I haven't been able to find a way to change the color of the TextField border to something other than the grey color it starts off with. I want to change it to black.
Here is my UpdatePage.js file (simplified) :
const useStyles = makeStyles(() => ({
updatePage: {
display: 'flex',
justifyContent: 'center',
marginTop: '100px'
},
updateMovementDiv: {
background: '#00BFFF',
fontFamily: 'PT Sans Caption',
fontSize: '18px',
borderRadius: '10px',
padding: '20px',
marginTop: '50px',
},
textDiv: {
background: '#ffffff',
padding: '8px',
borderRadius: '10px',
},
}));
const UpdatePage = () => {
const classes = useStyles();
return (
<div>
<Header title="Update Movement" />
<div className={classes.updatePage}>
<div className={classes.updateMovementDiv}>
<form onSubmit={handleSubmit} >
<div className={classes.textDiv}>
<TextField
name="movementName"
variant="outlined"
label="Movement Name"
style={{ width:200 }}
value={move.movementName}
onChange={(e) => setMoveData({ ...moveData, movementName: e.target.value })}
/>
<TextField
name="movementWeight"
variant="outlined"
label="New One Rep Max"
style={{ width:200 }}
InputProps={{endAdornment: <InputAdornment position="end">lb</InputAdornment>}}
onChange={(e) => setMoveData({ ...moveData, movementWeight: e.target.value })}
/>
</div>
<div className={classes.buttonDiv}>
<Button className={classes.updateButton} variant="contained" type="submit" fullWidth endIcon={<LoopIcon />} >Update</Button>
</div>
</form>
</div>
</div>
</div>
);
};
export default UpdatePage;
I have tried adding a classname to my TextField tags and changing the color from there.
I have also tried adding the input property to the class like so:
textField: {
input: {
color: 'white'
}
and then adding:
InputProps={{
className: classes.input,
}}
None of these have worked, the TextField border stays grey. Any help would be appreciated.
I guess you are writing just 'color' property and the property for border color is 'borderColor', change it to borderColor from color and then check it.

Material UI - remove border from one of the textfields

I'm trying to make an input with an icon on the left. something like this:
I've already created the input. my plan is to add the input and icon in a grid. remove input's border and add it to the grid container.
I'm not able to remove the border. I've tried using the spread operator but I was getting errors.
there are other inputs using the same styles and they need the border.
How can I remove border for this input ?
const useStylesCustom = makeStyles((theme) => ({
root: {
border: '1px solid #e2e2e1',
overflow: 'hidden',
borderRadius: 2,
backgroundColor: '#fff',
transition: theme.transitions.create(['border-color', 'box-shadow']),
'&:hover': {
backgroundColor: '#fff',
},
'&$focused': {
backgroundColor: '#fff',
borderColor: '#DFDFDF',
},
'&$error': {
borderColor: theme.palette.error.main,
},
},
focused: {},
error: {},
}));
<Grid container>
<Grid xs={2} style={{ alignSelf: 'center', textAlign: 'center' }} item>
<img src="/img/usa_flag.svg" height="25" />
</Grid>
<Grid item xs={10}>
<TextField
fullWidth
label='Phone number'
variant='filled'
value={lastName}
style={{ border: '0' }}
InputProps={{ classes, disableUnderline: true }}
onChange={(e) => setLastName(e.target.value)}
/>
</Grid>
</Grid>
You can remove the variant and no border will be displayed:
<Grid container style={{border: "1px solid black"}}>
<Grid xs={2} style={{ alignSelf: 'center', textAlign: 'center' }} item>
<img src="/img/usa_flag.svg" height="25" />
</Grid>
<Grid item xs={10}>
<TextField
fullWidth
label='Phone number'
value={"lastName"}
style={{ border: '0' }}
InputProps={{ classes, disableUnderline: true }}
/>
</Grid>
</Grid>
Here is a sandbox.

Resources