I Have a Dropdown and that Dropdown have some list of items, Here I need to disable some dropdown options, Disable options means the dropdown options should become disable i.e. not clickable.
I tried like this, But it will only disable 1st and 3rd options, So in further options may change, So below code dosen’t work…..
` ` options={data}
getOptionDisabled={(option) => {
// Mention options which needs to be disable
return option === data[1] || option === data[3]
}``
So Can we get a list of Items and can we Disable those list of Items??? Is that will work???
I am requesting anyone having any Idea to disable some dropdown options.`
Here is my Code:
`import React from 'react'
import { inputs } from '../Common/Data';
`const Dropdown = () => {
const data = inputs;
//console.log(data.purpose);
return (
<>
<div className='form-group'>
<div className="col-4">
<select className="form-select" aria-label="Disabled select example">
<option selected>Select an Option</option>
{data.purpose.map((items) => (
//console.log(items.value);
<option >{items.value}</option>
))}
</select>
</div>
</div>
</>
)
}`
export default Dropdown
3
Answers
why you don’t want to use the
disabled
attribute?like:
<option disabled={theFuncReturnedTrueOrFalse} >value</option>
so if you have to wait for data from API,
i think you can use the optional chain
?
:-_-
updated code:
https://codesandbox.io/s/bold-lucy-753vqk?file=/src/App.js
You can change your array accordingly to disable filed value.