Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MUI - Outlined select label is not rendering properly

As per the demo, the label for a MUI outlined select input should sit on top of the top border of the select box.

enter image description here

However, in my application, the z-index of the label seems to be placing it behind the top border and thus it looks like a line is cutting through the label.

enter image description here

I have pretty much taken the code exactly from the documentation, and as far as I know, do not have any styles conflicting with this input element. I have compared the styles in the debugger to what I have and what is present in the documentation, and I do not see any of my first party CSS files causing a different style to be set on the element.

Any idea what might be going wrong here?

Here is the source code:

<FormControl variant='outlined' style={{ width: '100%' }} margin={'1'}>
  <InputLabel id='test-select-label'>Label</InputLabel>
  <Select
    labelId='test-select-label'
    id='test-select'
    value={'test1'}
    onChange={e => setTest(e.target.value)}
    size='small'
  >
    <MenuItem key={1} value={'test'} >Test 1</MenuItem>
    <MenuItem key={2} value={'test2'} >Test 2</MenuItem>
  </Select>
</FormControl>
like image 972
pickle Avatar asked Apr 12 '21 19:04

pickle


People also ask

Where should the label for a Mui outlined select input sit?

As per the demo, the label for a MUI outlined select input should sit on top of the top border of the select box. However, in my application, the z-index of the label seems to be placing it behind the top border and thus it looks like a line is cutting through the label.

Is the material-UI inputlabel easier to style than the textfield?

The Material-UI InputLabel is easier to style than the TextField label because it is a standalone component. If you compose your own input using FormControl/InputLabel/Input components, you can style each piece independently. It likely would be easier for a beginner to style, but it requires more code than using a pre-composed TextField.

What are material-UI labels?

Material-UI labels provide visual information in a UI, but understanding the different label use cases can be challenging. For example, a TextField is composed of an InputLabel component plus other components.

How do I remove a label from a textfield in Mui?

After a user clicks, the Mui-focused class is automatically added to the TextField. I targeted the label plus that class in order to strip margin when the label performs its default animation of moving up on focus. If you want to remove the label from a TextField, simply don’t pass a value in the label prop.


Video Answer


3 Answers

Solution 1: Use TextField

This is what TextField is for. It uses FormControl and InputLabel internally and make sure they work well together. You can tell TextField to render select instead input by overriding the select props:

<TextField
  value={value}
  onChange={(e) => setValue(e.target.value)}
  select // tell TextField to render select
  label="Label"
>
  <MenuItem key={1} value="test">
    Test 1
  </MenuItem>
  <MenuItem key={2} value="test2">
    Test 2
  </MenuItem>
</TextField>

For more detail about how TextField works, see this answer.

Solution 2: Use Select

If you decide to use Select, you need to write more code to do the same amount of work:

Pass the label text as InputLabel children

<InputLabel id="test-select-label">Label</InputLabel>

This label text will be rendered on the screen as the Select label when put inside FormControl and next to the Select component.

Pass the label text to the label props of the Select component

This label text is hidden and used to override and remove the part of the border-top where the real label is occupied when the Select label is shrinked.

<Select labelId="test-select-label" label="Label">

Putting it together we'll have something like below, note that with this approach we need to set the label in 2 different places which is not very DRY, so I'd prefer the first approach.

<FormControl>
  <InputLabel id="test-select-label">Label</InputLabel>
  <Select
    value={value}
    onChange={(e) => setValue(e.target.value)}
    labelId="test-select-label"
    label="Label"
  >
    <MenuItem key={1} value="test">
      Test 1
    </MenuItem>
    <MenuItem key={2} value="test2">
      Test 2
    </MenuItem>
  </Select>
</FormControl>

Live Demo

Codesandbox Demo

like image 79
NearHuscarl Avatar answered Oct 16 '22 13:10

NearHuscarl


If you add the label property to your select component your problem should disappear.

...
<Select
  value={value}
  onChange={(e) => setValue(e.target.value)}
  label="Label" // add this 
>
  <MenuItem key={1} value="test">
    Test 1
  </MenuItem>
  <MenuItem key={2} value="test2">
    Test 2
  </MenuItem>
</Select>
...

Here is a live demo where you can see the difference:

Edit material-ui-outlined-select-label-is-not-rendering-properly

like image 32
yun_jay Avatar answered Oct 16 '22 12:10

yun_jay


Try this method, it worked for me.

JSX:

<TextField select variant={"outlined"} style={{width: "100%"}} label="Label">
    <MenuItem key={1} value="test">
        Test 1
    </MenuItem>
    <MenuItem key={2} value="test2">
        Test 2
    </MenuItem>
</TextField>

CSS:

.MuiSelect-outlined.MuiSelect-outlined, .MuiSelect-outlined.MuiSelect-outlined:active, .MuiSelect-outlined.MuiSelect-outlined:focus  {
    background-color: transparent;
    text-align: left;
    font-family: sans-serif !important;
    font-size: 14px !important;
    font-weight: 400 !important;
}
like image 3
Sathnindu Kottage Avatar answered Oct 16 '22 14:10

Sathnindu Kottage