Resizing components in Material UI without deformatting - css

I have been trying to resize a select component from material UI. The problem that arrised was the fact that I could not resize the component properly with the help of the className property, the label was not in the right place and the selection shade height was greater than the one from the select box I resized (height: 24px). This is what i tried:
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel id="demo-simple-select-outlined-label">Age</InputLabel>
<Select
labelId="demo-simple-select-outlined-label"
id="demo-simple-select-outlined"
value={age}
onChange={handleChange}
label="Age"
className={classes.MuiSelect}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
And this was the makestyles I used:
const useStyles = makeStyles((theme: Theme) =>
createStyles({
formControl: {
margin: theme.spacing(1),
minWidth: 120,
},
selectEmpty: {
marginTop: theme.spacing(2),
},
MuiSelect: {
width: "338px",
height: "24px"
}
}),
);
The link to the sandbox with the example: codesandbox
Finally, this was the result:
Box with the label in the wrong place
The shade of the selection is bigger than the actual box

As you have changed the size of the select element, you should also modify the .MuiInputLabel-outlined of the related InputLabel element, as well as the MuiSelect-outlined to fit the shadows of the select box to its width and height sandbox
First please add these outlined and selectOutlined objects to the useStyles
const useStyles = makeStyles((theme: Theme) =>
createStyles({
formControl: {
margin: theme.spacing(1),
minWidth: 120
},
selectEmpty: {
marginTop: theme.spacing(2)
},
MuiSelect: {
width: "338px",
height: "24px"
},
outlined: {
transform: "translate(4px, 3px)"
},
selectOutlined: {
padding: "0px 14px"
}
})
);
And then add them to the InputLabel and Select elements
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel
className={classes.outlined}
id="demo-simple-select-outlined-label"
>
Age
</InputLabel>
<Select
labelId="demo-simple-select-outlined-label"
id="demo-simple-select-outlined"
value={age}
onChange={handleChange}
label="Age"
classes={{
root: classes.MuiSelect,
outlined: classes.selectOutlined
}}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>

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 style TextField used as Select of muiv5?

How to reduce the padding of TextField used as Select of muiv5. And Also the max-height of dropdown option plus the padding of individual list item of that dropdown.
I can't wrap my head around sx prop. Can someone please help me understand how to override styles uisng sx props. Thanks in advance.
const pageList = useMemo(() => {
return range(1, 100).map((item, i) => {
return (
<MenuItem
key={i}
value={item}
sx={{
'& .MuiMenuItem-root.Mui-selected': {
padding: '0px 40%',
},
}}
>
{item}
</MenuItem>
);
});
}, [lastPage]);
<TextField
InputLabelProps={{ shrink: false }}
margin="dense"
variant="outlined"
size="small"
select
value={currentPage}
onChange={(e) => {
const page = e.target.value ? Number(e.target.value) : 0;
onPageChange(page);
}}
sx={{
'&.MuiInputBase-input': {
padding: '10px',
},
'&.MuiList-root': {
maxHeight: '100px',
},
}}
>
{pageList}
</TextField>

how to remove border in textfield fieldset in material ui

I need to remove the border. I used some css from stack overflow but the issue is not fixed yet . If any one please help me to fixed this issue .I shall be very thank full.
what css I write to remove the border.
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="phoneNumber"
disableUnderline={false}
// label="Phone Number"
name="phoneNumber"
autoComplete="phoneNumber"
autoFocus
onChange={handlePhoneNumberChange}
className={classes.textField}
placeholder="Phone Number"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
),
}}
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.1.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.1.1/umd/react-dom.production.min.js"></script>
I was looking for a borderless text-field and this is working for me...
<TextField
variant="standard" // <== changed this
margin="normal"
required
fullWidth
id="phoneNumber"
name="phoneNumber"
autoComplete="phoneNumber"
autoFocus
onChange={handlePhoneNumberChange}
placeholder="Phone Number"
InputProps={{
startAdornment: <AccountCircle />, // <== adjusted this
disableUnderline: true, // <== added this
}}
/>
These 2 props seem to be the key...
variant="standard"
InputProps={{
disableUnderline: true,
}}
InputProps can be passed to the style the variants of the inputs. For outlined input there a class named .MuiOutlinedInput-notchedOutline which sets the border in this question's case. To modify this class, pass the styles to the notchedOutline prop in InputProps.
const useStyles = makeStyles(() => ({
noBorder: {
border: "none",
},
}));
const TextInput = props => {
const { onChange, type} = props;
const classes = useStyles();
return (
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="phoneNumber"
disableUnderline={false}
// label="Phone Number"
name="phoneNumber"
autoComplete="phoneNumber"
autoFocus
classes={{notchedOutline:classes.input}}
// onChange={handlePhoneNumberChange}
className={classes.textField}
placeholder="Phone Number"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
),
classes:{notchedOutline:classes.noBorder}
}}
/>
);
};
Here is the working sandbox link: https://codesandbox.io/s/material-demo-forked-nhlde
to remove border in TextField fieldset in MUI 5,
simply add following.
sx={{
"& fieldset": { border: 'none' },
}}
I tried all the answers here.
Doesn't work
I found this InputBase
It works very nicely.
This is exactly what you should use.
They have provided the sandbox too
Sandbox InputBase
In your textField style add outline: 'none'
As at 2022, if your using MUI >= version 5, you can use some solutions here, and currently there's no where in the doc on how do this in Textfield.
Another nice component MUI provides is the Input, and luckily for us it accepts almost all props passed to Textfield, that's where you can do disableUnderline={false} and it will work as expected.
<Input
disableUnderline={true}
variant="standard"
autoFocus
onChange={yourChangeHandler}
value={value}
placeholder="Title"
fullWidth
/>
Finally, the following css works (2022)
'& .MuiInput-root': {
'&:before, :after, :hover:not(.Mui-disabled):before': {
borderBottom: 0,
},
},
For Outlined TextField
If you want to remove outlines from the outlined text field. Then add this in your TextField
sx={{border: 'none',"& fieldset": { border: 'none' },}}
Here is your code...
<TextField
variant="outlined"
sx={{border: 'none', "& fieldset": { border: 'none' },}}
margin="normal"
required
fullWidth
id="phoneNumber"
disableUnderline={false}
// label="Phone Number"
name="phoneNumber"
autoComplete="phoneNumber"
autoFocus
onChange={handlePhoneNumberChange}
className={classes.textField}
placeholder="Phone Number"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
),
}}
/>
For Standard TextField
If you want to remove underline from the standard text field. Then add this in your TextField
InputProps={{ disableUnderline: true }}
Here is code
<TextField
fullWidth
placeholder="Search..."
InputProps={{ disableUnderline: true }}
/>
Added sx prop with below attributes in TextField and it worked fine for me
<TextField
sx={{
input: {
border: "none",
},
"::placeholder": { color: "white" },
}}
/>
The documentation doesn't offer any good way to do this
So you can select the Mui selector for that element and modify it directly
This worked for me.
You can override the css
<TextField
id="outlined-basic"
label="Outlined"
variant="outlined"
sx={{ "& .MuiOutlinedInput-notchedOutline": { border: "none" } }}
/>
<TextField
id="filled-basic"
label="Filled"
variant="filled"
sx={{
"& .MuiFilledInput-underline:before": {
borderBottom: "none",
},
"& .MuiFilledInput-underline:after": {
borderBottom: "none",
},
"& .MuiFilledInput-underline:hover:not(.Mui-disabled):before": {
borderBottom: "none",
},
}}
/>
Please check it in codesandbox
Try to override style
See example below
disableUnderline
If true, the input will not have an underline.
<Input
variant="standard"
disableUnderline={true}
required
color="info"
fullWidth
margin="dense"
focused
/>
API
Just add
".MuiOutlinedInput-notchedOutline": { border: "none" },
to the sx prop.
This works both for MUI Select and Textfield
If you want to remove the border for all MuiOutlinedInput components, add this to your theme's components object:
export const theme = createTheme({
// ...
components: {
// ...
MuiOutlinedInput: {
styleOverrides: {
notchedOutline: {
border: 'none',
},
},
},
},
})

Dropdown Menu Item in React Material UI

I have a MenuItem in my Dialog2 in my React app. I have a problem displaying the MenuItem. I already put a zIndex: 1900 that is higher than the two dialogs. How come it doesn't appear on the front. It is hiding still on the back of the dialogs?
Pls check my codesandbox here:
CLICK HERE
<DialogContent style={{ width: 300 }}>
<TextField variant="outlined" select label="Gender" fullWidth>
{genders.map((gender, index) => (
<MenuItem key={index} value={gender} style={{ zIndex: 1900 }}>
{gender}
</MenuItem>
))}
</TextField>
</DialogContent>
You've to target the Menu popover z-index
const useStyles = makeStyles({
customMenuPopover: {
// take note of !important because default z-index is applied inline
zIndex: "1900 !important"
}
});
<TextField
SelectProps={{
MenuProps: {
PopoverClasses: {
root: classes.customMenuPopover
}
}
}}
variant="outlined"
select
label="Gender"
fullWidth
>
{genders.map((gender, index) => (
<MenuItem key={index} value={gender} style={{ zIndex: 1900 }}>
{gender}
</MenuItem>
))}
</TextField>

materriel -ui ToggleButton looks like button

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>;

Resources