I have an Autocomplete component and I need to prevent it for selecting some kind of values.
const options = ["Option 1", "Option 2", "Option 3"];
const [value, setValue] = React.useState(options[0]);
const [inputValue, setInputValue] = React.useState("");
const change = (event, newValue) => {
event.preventDefault();
event.stopPropagation();
if (newValue !== "Option 3") {
setValue(newValue);
}
};
console.log(value);
return (
<div>
<Autocomplete
value={value}
onChange={change}
inputValue={inputValue}
onInputChange={(event, newInputValue) => {
setInputValue(newInputValue);
}}
id="controllable-states-demo"
disableClearable
options={options}
style={{ width: 300 }}
renderInput={(params) => (
<TextField {...params} label="Controllable" variant="outlined" />
)}
/>
</div>
);
Currently the condition does not change the state, but it does change the value on the component. How can I prevent this to happen?
Here is the sandbox.