Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the width of material UI select based on label size

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)

like image 861
lowcrawler Avatar asked Jul 18 '18 18:07

lowcrawler


People also ask

How do I change the text field width in material UI?

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.

How do I set the width of a react select?

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.

How do I use material UI select?

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 (!


1 Answers

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>
like image 196
CaseyC Avatar answered Oct 22 '22 07:10

CaseyC