admin管理员组文章数量:1405117
I want to use an Autoplete (from the Material UI library) ponent from material ui to select multiple options, and those options should not be able to be removed (directly) by the user.
The problem I'm facing is that the user can delete the options from the Autoplete if they focus the ponent and press backspace as if they are deleting text.
Code
This is the ponent I'm using:
<Autoplete multiple
options={options}
getOptionLabel={option => option.title}
renderInput={params =>
<TextField {...params} label="Autoplete" variant="outlined" />
}
onChange={this.onAutopleteChange.bind(this)}
getOptionSelected={(option: Option, value: Option) => option.value === value.value}
filterSelectedOptions={true}
renderTags={(tagValue, getTagProps) =>
tagValue.map((option, index) => (
<Chip key={option.value} label={option.title} color="primary" />
))
}
disableClearable={true}
/>
What I tried
- Disabling the TextField from the renderInput prop with
disable={true}
has no effect. - Adding
InputProps={{ disabled: true }}
orInputProps={{ readOnly: true }}
to TextField disables the Autoplete pletely. - Adding
ChipProps={{ disabled: true }}
to the Autoplete has no effect.
Thanks for reading!
I want to use an Autoplete (from the Material UI library) ponent from material ui to select multiple options, and those options should not be able to be removed (directly) by the user.
The problem I'm facing is that the user can delete the options from the Autoplete if they focus the ponent and press backspace as if they are deleting text.
Code
This is the ponent I'm using:
<Autoplete multiple
options={options}
getOptionLabel={option => option.title}
renderInput={params =>
<TextField {...params} label="Autoplete" variant="outlined" />
}
onChange={this.onAutopleteChange.bind(this)}
getOptionSelected={(option: Option, value: Option) => option.value === value.value}
filterSelectedOptions={true}
renderTags={(tagValue, getTagProps) =>
tagValue.map((option, index) => (
<Chip key={option.value} label={option.title} color="primary" />
))
}
disableClearable={true}
/>
What I tried
- Disabling the TextField from the renderInput prop with
disable={true}
has no effect. - Adding
InputProps={{ disabled: true }}
orInputProps={{ readOnly: true }}
to TextField disables the Autoplete pletely. - Adding
ChipProps={{ disabled: true }}
to the Autoplete has no effect.
Thanks for reading!
Share Improve this question edited Apr 4 at 13:17 Olivier Tassinari 8,6916 gold badges25 silver badges28 bronze badges asked Aug 4, 2020 at 17:10 LeonMarchettiLeonMarchetti 1053 silver badges9 bronze badges1 Answer
Reset to default 6To control this aspect, you need to use a controlled approach to the Autoplete's value as demonstrated in this demo.
In the documentation for the onChange
prop you will find the following:
onChange Callback fired when the value changes.
Signature:
function(event: object, value: T | T[], reason: string) => void
event: The event source of the callback.
value: The new value of the ponent.
reason: One of "create-option", "select-option", "remove-option", "blur" or "clear".
The third argument to onChange
is the "reason" for the change. In your case, you want to ignore all of the "remove-option" changes:
onChange={(event, newValue, reason) => {
if (reason !== "remove-option") {
setValue(newValue);
}
}}
Here's a full working example:
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autoplete from "@material-ui/lab/Autoplete";
import Chip from "@material-ui/core/Chip";
const options = ["Option 1", "Option 2"];
export default function ControllableStates() {
const [value, setValue] = React.useState([options[0]]);
const [inputValue, setInputValue] = React.useState("");
return (
<div>
<div>{`value: ${value !== null ? `'${value}'` : "null"}`}</div>
<div>{`inputValue: '${inputValue}'`}</div>
<br />
<Autoplete
multiple
value={value}
disableClearable
onChange={(event, newValue, reason) => {
if (reason !== "remove-option") {
setValue(newValue);
}
}}
inputValue={inputValue}
onInputChange={(event, newInputValue) => {
setInputValue(newInputValue);
}}
id="controllable-states-demo"
options={options}
style={{ width: 300 }}
renderInput={params => (
<TextField {...params} label="Controllable" variant="outlined" />
)}
renderTags={(tagValue, getTagProps) =>
tagValue.map((option, index) => (
<Chip key={option} label={option} color="primary" />
))
}
/>
</div>
);
}
本文标签: javascriptDisable backspace deleting of options in AutocompleteStack Overflow
版权声明:本文标题:javascript - Disable backspace deleting of options in Autocomplete - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744320271a2600462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论