To enable the Select
to have disabled items, you can add the disabled
property to the respective MenuItem
(as demonstrated with the "Twenty" MenuItem
in the example below).
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import MenuItem from "@material-ui/core/MenuItem";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";
const useStyles = makeStyles(theme => ({
formControl: {
margin: theme.spacing(1),
minWidth: 120
},
selectEmpty: {
marginTop: theme.spacing(2)
}
}));
export default function SimpleSelect() {
const classes = useStyles();
const [age, setAge] = React.useState("");
const handleChange = event => {
setAge(event.target.value);
};
return (
<div>
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel id="demo-simple-select-outlined-label">Age</InputLabel>
<Select
labelId="demo-simple-select-outlined-label"
id="demo-simple-select-outlined"
value={age}
onChange={handleChange}
label="Age"
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem disabled value={20}>
Twenty
</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
</div>
);
}
https://codesandbox.io/s/disabled-menuitem-fvgi8?fontsize=14&hidenavigation=1&theme=dark
For a basic Select
, you should utilize the disabled
attribute for each <option>
:
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";
const useStyles = makeStyles(theme => ({
formControl: {
margin: theme.spacing(1),
minWidth: 120
},
selectEmpty: {
marginTop: theme.spacing(2)
}
}));
export default function SimpleSelect() {
const classes = useStyles();
const [age, setAge] = React.useState("");
const handleChange = event => {
setAge(event.target.value);
};
return (
<div>
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel id="demo-simple-select-outlined-label">Age</InputLabel>
<Select
labelId="demo-simple-select-outlined-label"
id="demo-simple-select-outlined"
value={age}
onChange={handleChange}
label="Age"
native
>
<option aria-label="None" value="" />
<option value={10}>Ten</option>
<option disabled value={20}>
Twenty
</option>
<option value={30}>Thirty</option>
</Select>
</FormControl>
</div>
);
}
https://codesandbox.io/s/disabled-option-flswf?fontsize=14&hidenavigation=1&theme=dark
If your options are contained within an array, there should be a mechanism to identify which options need to be disabled. The following illustration presents one approach where option data includes a flag for disabling the option.
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";
const useStyles = makeStyles(theme => ({
formControl: {
margin: theme.spacing(1),
minWidth: 120
},
selectEmpty: {
marginTop: theme.spacing(2)
}
}));
const options = [
{ value: 10, label: "Ten" },
{ value: 20, label: "Twenty", disabled: true },
{ value: 30, label: "Thirty" }
];
export default function SimpleSelect() {
const classes = useStyles();
const [age, setAge] = React.useState("");
const handleChange = event => {
setAge(event.target.value);
};
return (
<div>
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel id="demo-simple-select-outlined-label">Age</InputLabel>
<Select
labelId="demo-simple-select-outlined-label"
id="demo-simple-select-outlined"
value={age}
onChange={handleChange}
label="Age"
native
>
<option aria-label="None" value="" />
{options.map(option => (
<option value={option.value} disabled={option.disabled}>
{option.label}
</option>
))}
</Select>
</FormControl>
</div>
);
}
https://codesandbox.io/s/disabled-option-7l0bl?fontsize=14&hidenavigation=1&theme=dark