material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[Select] autoWidth=true does not adjust width to the largest option

Open fzaninotto opened this issue 9 years ago • 9 comments

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

<TextField
     select
     SelectProps={{ autoWidth: true }}
>
   <MenuItem value=""><em>None</em></MenuItem>
   <MenuItem value={10}>Short</MenuItem>
   <MenuItem value={20}>A bit longer</MenuItem>
   <MenuItem value={30}>Very, very, very long</MenuItem>
</TextField>

Should display a long textField, as long as the "Very, very, very long" option.

Current Behavior

image

The TextField is very short, it uses the width of the selected option. When nothing is selected, it is just not usable.

Besides, having the width of the field change according to the selected element is disturbing.

Steps to Reproduce (for bugs)

https://codesandbox.io/s/mj7zw8plx8

Context

I'm trying to migrate admin-on-rest to material-ui 1.0

Your Environment

Tech Version
Material-UI 1.0.0-beta.17
React 15.6
browser Chrome
etc

fzaninotto avatar Oct 20 '17 22:10 fzaninotto

The autoWidth feature was discussed in this issue #8286. It's disabled by default. It's not about making the input following the menu width. It's the other way around. This property is about how is the input width impacts the menu width. For your use case, you ganna have to set a width (or min-width) to the input. It's something we do in the demo. I would have loved to fix your codesandbox, but for some reason, your example doesn't compile.

oliviertassinari avatar Oct 22 '17 14:10 oliviertassinari

My bad, the documentation was clear about it.

But then I have another grief: why doesn't the <TextField select> have a reasonable default width? It used to be in 0.x.

fzaninotto avatar Oct 23 '17 11:10 fzaninotto

@fzaninotto As far as I know, we don't set any width by default to the input. It's behaving like a native input. Well, it's a good question. I haven't though about it. So far the logic is to be as close as possible to the native implementation.

oliviertassinari avatar Oct 23 '17 12:10 oliviertassinari

How do you set 100% width on the select so that it fits the width of container ?

shamseerahammedm avatar Jun 26 '20 20:06 shamseerahammedm

@shamseerahammedm Add style to formControl like this

const useStyles = makeStyles((theme) => ({
  formControl: {
    margin: theme.spacing(1),
    minWidth: 'calc(100%)',
    maxWidth: 300,
  }
}));

abitgen avatar Dec 11 '20 11:12 abitgen

@abitgen there is fullWidth prop in FormControl API: <FormControl fullWidth>

alexicum avatar Jan 11 '21 15:01 alexicum

@fzaninotto How did you fix the issue?

JanakaSandaruwan avatar Sep 06 '21 18:09 JanakaSandaruwan

@shamseerahammedm Add style to formControl like this

const useStyles = makeStyles((theme) => ({
  formControl: {
    margin: theme.spacing(1),
    minWidth: 'calc(100%)',
    maxWidth: 300,
  }
}));

This didn't work for me, but I managed to make the select component fit the list size like this:

<FormControl sx={{
     minWidth: 340,
     maxWidth: '100%',
}}>
     <Select ...>
</FormControl>

Hope this help.

yurischulz avatar Apr 10 '23 15:04 yurischulz

Screenshot 2024-04-28 at 22 46 44 Screenshot 2024-04-29 at 15 32 17

How Can I align the width?

fatemehqasemkhani avatar Apr 29 '24 13:04 fatemehqasemkhani