I am trying to set the MUI Select
component's width. For this I have to provide a class to the FormControl
component, such as mw-120
which links to a CSS class defining the min-width
of 120px.
Material UI Select component:
<FormControl className='mw-120'> <InputLabel htmlFor='selected-language'>Language</InputLabel> <Select value={this.state.selectedLanguage} onChange={(e) => this.onLanguageChange(e.target.value)} inputProps={{ name: 'language', id: 'selected-language', }}> {menuItems} </Select> </FormControl>
CSS class:
.mw-120 { min-width: 120px; }
While I would expect the size of the Select
component to now have a width of 120px minimum, the component remains exactly the same after rendering, as shown in the following picture. In other words, it is to narrow. The width is not big enough. The width should be greater than the label Language
.
An element analysis within the Chrome Developer Tools has shown that the main DIV element of that component has a class .MuiFormControl-root-234
that contains min-width: 0;
, and it is located/ranked higher than my .mw-120
class. I guess this overrides my .mw-120
class, right? Is there any other way to influence the width of the Material UI Select component? There are no helpful examples of influencing the width of this component on the Material UI Select component page.
Instead of declaring FormControl , InputLabel and Select manually and pass the size props to FormControl , you should create a selectable TextField and change the TextField size props.
Set MUI Width and Height with 'InputProps' Another method for setting Material-UI TextField width and height is a combination of sx at the TextField root and passing sx styling values to the composing Input component via InputProps . The width is set with TextField prop sx={{width: 300}} .
The MUI Select component is an input/dropdown combo that comes with dozens of configurable props. In this tutorial I will customize the dropdown position, the default and placeholder values, add multiselect, and add labels and helper text, and more. MUI Select with Dropdown Offset and Placeholder Value.
If you are doing something a one-off styling, you can use the inline style, it worked for me.
<FormControl style={{minWidth: 120}}> // this line <InputLabel htmlFor='selected-language'>Language</InputLabel> <Select value={this.state.selectedLanguage} onChange={(e) => this.onLanguageChange(e.target.value)} inputProps={{ name: 'language', id: 'selected-language', }}> {menuItems} </Select> </FormControl>
If you would reuse it in more code and want to avoid code duplication, you will probably want to work with Themes
For potential re-use, the official doc samples accomplish this with makeStyles
like this:
import { makeStyles } from '@material-ui/core/styles'; const useStyles = makeStyles((theme) => ({ formControl: { margin: theme.spacing(1), minWidth: 120, }, }));
Then useStyles
generates class names like this:
const classes = useStyles();
Then add to your FormControl
component like this:
<FormControl className={classes.formControl}>
const { FormControl, InputLabel, Select, MenuItem, makeStyles } = MaterialUI; const App = function () { const useStyles = makeStyles((theme) => ({ formControl: { margin: theme.spacing(1), minWidth: 120, }, })); const classes = useStyles(); return ( <div className="App"> <FormControl className={classes.formControl}> <InputLabel id="demo-simple-select-label">Age</InputLabel> <Select labelId="demo-simple-select-label" id="demo-simple-select" value={''} > <MenuItem value={10}>Ten</MenuItem> <MenuItem value={20}>Twenty</MenuItem> <MenuItem value={30}>Thirty</MenuItem> </Select> </FormControl> </div> ) } ReactDOM.render( <App />, document.getElementById('root') );
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script> <script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script> <script src="https://unpkg.com/@material-ui/[email protected]/umd/material-ui.development.js"></script> <div id="root"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With