Unable to set disabled option as defaultValue for select element
See original GitHub issueWhat is the current behavior?
I want to render a selectbox including a disabled option, which should be selected by default. Currently, my code looks pretty much like this:
<select name='country' defaultValue='no-value'>
<option value='no-value' disabled>Please select</option>
<option value='o1'>Option 1</option>
<option value='o2'>Option 2</option>
<option value='o3'>Option 3</option>
</select>
When only using react, everything is like expected: The selectbox is rendered, with the option “Please select” as default value. Here is a codepen including this example
However, as soon as I include redux-form, I’m unable to set a disabled option as defaultValue. It has to either, not be disabled, or not beeing a redux form. I’ve also tried using the initialValues, but they seem to ignored on select elements?
What is the expected behavior?
Form / Selectbox should get rendered having the disabled option selected by default.
What’s your environment?
Chrome: 52.0.2743 @ Linux React: 15.3.2 Redux-Form: 6.1.1
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
Top Results From Across the Web
option disabled selected not set as default value in React js ...
So how can I show a Select City option that is disabled? This is just to prompt them to make a selection but...
Read more >API - React Select
A flexible and beautiful Select Input control for ReactJS with multiselect, autocomplete and ajax support.
Read more >option disabled selected not set as default value in React js ...
Coding example for the question option disabled selected not set as default value in React js dropdown-Reactjs.
Read more >Element: <oj-select-many> - Oracle
Set focus to the select. If hints, title or messages exist in a notewindow, pop up the notewindow. Disabled option items receive no...
Read more >HTML option disabled Attribute - W3Schools
The disabled attribute can be set to keep a user from selecting the option until some other condition has been met (like selecting...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It works if you do the following: https://jsfiddle.net/7teq3f04/
If you don’t use
initialValues
, then the behavior is really strange. It changes to thedefaultValue
only when you focus the select…I was able to get this working by adding an
input
property passed into the renderSelect object and then changing my select element to:<select id={id} name={name} {...input}>
I’m not really sure why the input works (my own inexperience) but it does. I’m leaving my follow-up here to hopefully help other devs that have stumbled with this like I have.
On a slightly different note, my error coming back was an object (because of how our project handles validation) so I handle it differently than in the examples by using a react-intl component:
{touched && error && <div><FormattedMessage {...error} /></div>}