[Select] Empty value does not render
I have a very simple use case for the new <Select> component to select from multiple items. No form control or label is required - just the raw <Select> is good enough. The value of my first option is an empty string:
<Select
value={this.state.selectedFruit}
onChange={this.handleChange}
input={<Input />}
>
<MenuItem value="">All Fruits</MenuItem>
<MenuItem value="apple">Apple</MenuItem>
<MenuItem value="banana">Banana</MenuItem>
<MenuItem value="orange">Orange</MenuItem>
</Select>
When I click on the component, the dropdown shows all 4 options, however when I select the first option with the blank value, the dropdown goes away (as expected) but the first item does not get rendered in the control. In other words, when the value is blank, the corresponding menu item is not rendered in the input part of the component. This should be possible, just like a normal select:
<select
value={this.state.selectedFruit}
onChange={this.handleChange}
>
<option value="">All Fruits</option>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="orange">Orange</option>
</select>
This is a limitation of the Select by the design of the material specification.
The floating label must be shown when the select is empty. Changing the behavior to follow your expectation will break this behavior. We can't have both.
Instead, we can add a displayEmpty property.
displayEmpty property works for me, @oliviertassinari. Thanks for the quick response.
return (
<div>
<FormControl sx={{ m: 1, minWidth: 120 }}>
<InputLabel shrink id="demo-simple-select-helper-label">
Age
</InputLabel>
<Select
labelId="demo-simple-select-helper-label"
id="demo-simple-select-helper"
displayEmpty
value={age}
input={<OutlinedInput notched label="Age" />}
onChange={handleChange}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
<FormControl sx={{ m: 1, minWidth: 120 }}>
<Select
value={age}
onChange={handleChange}
displayEmpty
inputProps={{ "aria-label": "Without label" }}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
</div>
);
https://codesandbox.io/s/selectlabels-material-demo-forked-u93kb?file=/demo.tsx
In the first demo, label sets the width of the gap, notched tell the component to activate the gap, shrink tells the label to render at the top, displayEmpty tells the select to render the empty value.
I really don't understand what is going on here but I had to create a hack to work around this issue. I created a Picklist wrapper and replaced value with __none__ in the render method. Then in the onChange I converted it back to '' (empty string).
displayEmpty was causing a bug where the floating label was overlaying on top of the label and looked horrible.
Here's the code I used for those that are curious:
https://gist.github.com/brennancheung/b2c9c2d85e698a3c8cf52917ed1e2f23
I really don't understand what is going on here but I had to create a hack to work around this issue. I created a
Picklistwrapper and replacedvaluewith__none__in therendermethod. Then in theonChangeI converted it back to''(empty string).
displayEmptywas causing a bug where the floating label was overlaying on top of thelabeland looked horrible.Here's the code I used for those that are curious:
https://gist.github.com/brennancheung/b2c9c2d85e698a3c8cf52917ed1e2f23
I'm having the label problem, too.
Also having the label problem. I used @brennancheung's workaround.
Using displayEmpty on select with shrink in label solves the issue.
shrink doesn't help when using outline variant. The label has a line crossed through it.
shrinkdoesn't help when using outline variant. The label has a line crossed through it.
I ran into this same issue, and it took me a little while to come up with a fix
I'm working with the outlined variant of a Select, and passing an empty string for the default values that I want to show. Here are the changes I had to make to get this to work:
1.) Added the 'input' prop to the Select component and passed in an OutlinedInput
<Select
// NOTE that the displayEmpty prop is added
displayEmpty
value={selectedValue}
onChange={handleChange}
name={inputName}
input={
<OutlinedInput
label={label}
classes={{
// NOTE: added a class for the notchedOutline fieldset label which I'll show later
notchedOutline: classes.notchedOutline
}}
/>
2.) Created a classes object with makeStyles / useStyles, passed into the notchedOutline property as seen above
const useStyles = makeStyles((theme) => ({
notchedOutline: {
// NOTE: the legend element is a child of the notchedOutline component
"& legend": {
maxWidth: "1000px"
}
}
}))
The 'maxWidth' property is what keeps the proper notched style and occludes the 'strikethrough' effect.
3.) Make sure the InputLabel has the 'shrink' prop
<FormControl fullWidth variant="outlined">
<InputLabel shrink>{label}</InputLabel>
I haven't investigated much further, since this has fixed the issue for me. Hopefully it helps anyone else looking for a quick fix until this is sorted in the upcoming release.
You saved me @jacobedawson, thank you
You saved me too @jacobedawson. Thank you!
Awesome @jacobedawson! Should be in the docs haha thank you!