react-bootstrap-typeahead - React-Bootstrap-Typeahead:在将选择设置为框之前捕获时刻(以允许有条件的拒绝)
问题描述
在 React-Boostrap-Typeahead 中,我需要在单击鼠标之后、菜单选择加载到 Typeahead 框之前捕获这一时刻。
这是因为我需要检查正在选择的项目,并且对于其中一些项目,我需要显示带有是/否警告的警报。只有单击是,我才能继续将该值设置到框中。否则我需要拒绝选择并保留它在鼠标单击之前的任何内容。
- 我不能使用
onChange
,因为那时选择已经在框中。 - 我不能使用
onInputChange
,因为那是用于打字而不是用于菜单选择。我需要后菜单选择,前框更改。
如果有任何解决方法,请告诉我。
解决方案
您应该能够在使用后实现您的目标onChange
:
const ref = useRef();
const [selected, setSelected] = useState([]);
return (
<Typeahead
id="example"
onChange={(selections) => {
if (!selections.length || window.confirm('Are you sure?')) {
return setSelected(selections);
}
ref.current.clear();
}}
options={options}
ref={ref}
selected={selected}
/>
);
推荐阅读
- kubernetes - 如何使用文件升级 helm chart 部署
- c# - 尝试使用 Ionic4 连接到 WCF Web 服务器
- macos - /bin/bash: --timeout: 在 macOS 上找不到命令
- java - 如何通过反射从父类调用子类方法
- android - 任务“:app:transformClassesWithDesugarForDebug”执行失败
- javascript - 使用量角器在 Safari 上的 iframe 中找不到元素
- hibernate - spring data 和 hibernate 将字符串参数更改为 unicode\ASCII
- machine-learning - 如何向 keras Retina 网络添加 dropout 技术?
- javascript - 我应该如何在前端处理 tslint no-null-keyword?
- html - 下拉菜单只在每个按钮悬停时打开一个下拉框