Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally disabled select option in React

I built my own React select component and would like to have an option to set the default value to be disabled

The component basically looks like this:

<select
    id={this.props.id}
    name={this.props.name}
    value={this.props.value}
    defaultValue={this.props.default}
    onClick={this.handleFieldClick}
    onChange={this.handleFieldChange} >

         <option value="" disabled={this.defaultDisabled ? true : false} >{this.props.defaultLabel}</option>

         { Object.keys(this.props.options).map((val, i) => ( 
             <option key={i} value={val}>{this.props.options[val]}</option>
         ))}

 </select>

This line giving me the issue is the following:

<option value="" disabled={this.defaultDisabled ? true : false} >{this.props.defaultLabel}</option>

The "disabled" option is still selectable and renders like this:

<option value="" selected="">Default Option</option>

I believe it's because the correct syntax for a disabled option is <option disabled ></option>, not <option disabled="true" ></option>

But when I format my JSX as follows:

<option value="" {this.defaultDisabled ? "disabled" : ""} >{this.props.defaultLabel}</option>

I get an error that crashes the app.

What is the correct syntax for conditionally writing a directive value into a tag with JXS and/or conditionally set a disabled option in React?

like image 532
yevg Avatar asked Jul 04 '18 02:07

yevg


People also ask

How do you disable a select in React?

How to disable select till a value is given in previous selects. *Actual behavior*All selects remain active. On other frameworks, I can simply give an option of "disabled = true" and change it with a state event change to "disabled = false" but does not exist on MDB-Bootstrap-React.

How do you handle the Select option in React?

To handle the onChange event on a select element in React: Set the onChange prop on the select element. Keep the value of the selected option in a state variable. Every time the user changes the selected option, update the state variable.

How do you remove selected value from dropdown in React?

By clicking on the clear icon which is shown in DropDownList element, you can clear the selected item in DropDownList through interaction.

Is Clearable in React select?

We can programmatically clear a React-Select dropdown by resetting the value that is set as the value of the value prop to null . Then the dropdown will be cleared. We have the options array with the value and label properties.


2 Answers

You missed a .props. And you can also use null instead of false.

<option value="" disabled={this.props.defaultDisabled ? true : null} >{this.props.defaultLabel}</option>
like image 149
Neo Choi Avatar answered Sep 18 '22 13:09

Neo Choi


After a couple of time, I finally fixed the issue.

It seems that in React to use selected attribute on the HTML tag is not recommended. The best ways to set a default value is to use defaultValue If you use it, it will throw an error in development and keep quiet in production.

More info can be found here

How to avoid such error

  • Step one

    1. Give default value to HTML select Tag
     <select
       name="originId"
       defaultValue="Choose-origin"
     >
  1. Give option tag the value which is equal to defaultValue of select
     <option
        value="Choose-origin"     // normally in VanillaJS you would use "selected"
        disabled
     >Choose origin</option>

  1. Remember to disable the option tags so that you deny the user to click it.

Thanks, to @Neo Choi and @Bernard Leech for help.

like image 27
Niyongabo Eric Avatar answered Sep 20 '22 13:09

Niyongabo Eric