question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to set disabled option as defaultValue for select element

See original GitHub issue

What 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:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
gustavohenkecommented, Mar 7, 2017

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 the defaultValue only when you focus the select…

3reactions
fivetwelvecommented, Oct 1, 2017

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>}

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found