I've got a select
:
<FormControl className={classes.formControl}>
<InputLabel htmlFor="age-native-simple">{this.props.label}</InputLabel>
<Select
native
value={this.state.value}
onChange={this.handleSelectChange('value')}
inputProps={{
name: this.props.label,
id: this.props.id,
}}
>
{this.buildSelectOptions(this.props.options)}
</Select>
{(this.props.helperText)?<FormHelperText>{this.props.helperText}</FormHelperText>:null}
</FormControl>
The formControl class looks like this:
formControl: {
margin: 0,
fullWidth: true,
backgroundColor: '#9ee',
wrap: 'nowrap'
},
It works reasonably well - sizing itself to be as small as possible while still showing the largest possible option
value.
Unfortunately, when the InputLabel
is longer than the contents of the select options
... the label wraps and looks terrible.
How can I keep my select from wrapping the input label? I'd like it to simply expand the select as needed, as it does when there is a long entry in the option
list if possible. Setting a "minWidth" isn't my preferred solution for many reasons (mainly that this component is used by a bunch of different things and calculating the minWidth of them all would be difficult)
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 height is set by passing sx: { height: 80 } to the InputProps prop.
When selected_value changes, so does its length , and therefore the width of the div gets updated with the new total. Example: if the selected value is now Lorem Ipsum dolor sit amet , the length is now 26 . By applying the formula we get a larger width of : (8px * 26 letters) + 100px = 308px . to your component.
Second way: with Select componentimport { InputLabel, Select } from "@material-ui/core"; Now we create our FormControl using the Select component: return( <FormControl> <InputLabel htmlFor="trinity-select"> Choose one Person of trinity </InputLabel> <Select id="trinity-select" inputProps={{ inputRef: (ref) => { if (!
Edit: I misunderstood the question here. I thought the issue was with menu items wrapping which my answer would fix. The correct and simple fix to the question is that you are missing display: 'flex'
from your formControl class.
Like this:
formControl: {
margin: 0,
fullWidth: true,
backgroundColor: '#9ee',
display: 'flex',
wrap: 'nowrap'
},
Here's a working sandbox
It looks like <Select/>
has an autoWidth
property that you can set to achieve this. It's default is false
.
From the docs:
If true, the width of the popover will automatically be set according to the items inside the menu, otherwise it will be at least the width of the select input.
It's used like this:
<Select
native
autoWidth={true}
value={this.state.value}
onChange={this.handleSelectChange('value')}
inputProps={{
name: this.props.label,
id: this.props.id,
}}
>
{this.buildSelectOptions(this.props.options)}
</Select>
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