-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to include placeholder in select component in Material UI@Next #11069
How to include placeholder in select component in Material UI@Next #11069
Comments
@NayanSrivastav This concern has already been raised in #8875. As far as the discussion went, the conclusion was that the placeholder property for a select component doesn't make sense. With the given information, it's not something we plan on addressing. Instead, @sakulstra has raised some great alternatives: #8875 (comment) import React from "react";
import { render } from "react-dom";
import { Select } from "material-ui";
import { withStyles } from "material-ui/styles";
import { MenuItem } from 'material-ui/Menu';
import classNames from "classnames";
class App extends React.Component {
state = {
select: 'none'
}
handleChange = field => e => {
this.setState({[field]: e.target.value})
}
render() {
return (
<div>
<Select native defaultValue='none'>
<option value="none" disabled>
uncontrolled Native placeholder
</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</Select>
<br />
<Select native value={this.state.select} onChange={this.handleChange('select')}>
<option value="none" disabled>
controlled Native placeholder
</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</Select>
<br />
<Select native value={this.state.select} onChange={this.handleChange('select')}>
{this.state.select === 'none' && <option value="none" disabled>
Autohide after selection
</option>}
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</Select>
<br />
<Select value={this.state.select} onChange={this.handleChange('select')}>
<MenuItem value="none" disabled>
controlled Non native placeholder
</MenuItem>
<MenuItem value="1">Option 1</MenuItem>
<MenuItem value="2">Option 2</MenuItem>
<MenuItem value="3">Option 3</MenuItem>
</Select>
</div>
);
}
}
render(<App />, document.getElementById("root")); |
@NayanSrivastav Now, maybe it's a matter on improving the documentation or adding some warnings when people are trying to provide a placeholder. |
@oliviertassinari I think alternatives suggested by @sakulstra are great and yeah document must be improved so that folks coming from stable Thanks for prompt reply and closing it now. |
@oliviertassinari this is a great alternative, |
@hadasmaimon I'm trying to do this as well, did you find a solution? |
@alexanderkho
|
You can override the theme styling with the following code: this will, however, make any selected item grey also, but that is my use-case so I'm ok with that 👍 |
Great ideas; I got your solution up and running on my own project. I like the idea of using React's useState() and useEffect() to dynamically change "useStyles()" properties (which I am assuming is what you did, although it is not in your code). |
you can use the following
|
I came here from TextField with the select props set to true. I was wondering why the placeholder doesn't show up. Apparently it's not supported. Maybe the docs in the TextField section needs to be updated too. |
This is actually a very minimal and nice workaround, nice one! |
I prefer more native looking placeholder with InputLabel :
|
just provide following attributes into select |
This should instead be the following:
Because the example you provided will cause the value not to show when it is selected. Also making the placeholder functional did not work for me. It should also be mentioned that this only renders the value of the component. Not the label. |
but if your label is different than your value - it will show the value instead of the label |
Thank you all for your solutions!
|
I don't want to overwrite <Select
sx={{
'& .MuiSelect-select .notranslate::after': placeholder
? {
content: `"${placeholder}"`,
opacity: 0.42,
}
: {},
}}
>
{options}
</Select> |
It doesn't work for me. An error appears. So I use
|
This seems like a typescript error, try this instead: displayEmpty
renderValue={(value: string): React.ReactNode =>
(value !== '' ? value : 'Placeholder text') as string
} |
Thank you! But, I have still an unresolved thing.
It doesn't be reproduced. For me, the value is shown when it's selected |
This, in my opinion, is an elegant solution to this, unfortunately, it does not work because for some reason the input is not the first element in the Select component. |
I have some related issue. If I am using disabled Even in your example https://mui.com/material-ui/react-select/#placeholder the first option after placeholder is focused on select opening. |
I think there is a clear need for this and not having this prop implemented already is very strange and arbitrary decision.
|
Completely agree. For such a robust and heavy library, this is an use case to be expected and implemented. The example provided in the documentation shows this is a flaw and is anything but elegant. The solutions provided here might work but it doesn't feel correct using such workarounds in corporate apps considering readability. |
Here's my neat little trick, just render this alongside the Select component:
This will make the select component's border box to have a placeholder at all times, so wrap the above code in a condition to only be rendered if the select is empty. |
@damjanvucina Although not ideal, I think your soluton might serve as a temporary hack. I just modified it a bit to fit better (one thing is that FormLabel renders {!value && !label && placeholder && (
<div
style={{
top: "0.25em",
zIndex: 2,
backgroundColor: "transparent",
position: "absolute",
fontSize: "1em",
color: "#BBB",
fontWeight: "400",
pointerEvents: "none",
}}
>
{placeholder}
</div>
)} |
Thanks😊. Works for me. I styled it after wrapping the string "placeholder" into span tag. |
Here is an easy workaround
|
Can you explain please why you said MUI select placeholder doesn't make sens? |
Expected Behavior
Select field should show Placeholder text.
Current Behavior
Placeholder text should be visible when passed to
Input
tag.Your Environment
The text was updated successfully, but these errors were encountered: