javascript - 如何以编程方式在输入单击时留下 Ant Design 选择?
问题描述
我试图弄清楚如何在输入点击时移除焦点/取消选择/离开 Ant Design Select?我有以下组件:
<Select
className="role-input"
mode="multiple"
value={this.selectedValues[config.key]}
onChange={(value, options) => this.onRoleChange(value, options, index)}
placeholder={intl.formatMessage({ id: config.placeholder })}
size="large"
key={index}
optionFilterProp="children"
filterOption={(input, option) => option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
onInputKeyDown={() => console.log('hi')}
>
{roleOptions}
</Select>
现在我console.log("hi")
有一个inputKeyDown。我想知道如何在 inputKeyDown 上删除焦点/取消选择/保留选择。我看到有一些选择方法,例如blur()
其描述是删除焦点,但是,没有任何示例,我无法弄清楚如何实现它。
解决方案
您需要分配一个ref
to Select
。然后在current
.
const selectRef = React.useRef(null);
const handleInputKeyDown = () => {
if (selectRef.current) {
selectRef.current.blur();
}
};
return (
<Select
ref={selectRef}
onInputKeyDown={handleInputKeyDown}
...
推荐阅读
- javascript - 在同一个项目中加载多个 jquery 版本但不同的页面
- c# - C# - 通用方法不能使用对象的字段
- android - 为什么我发布的游戏的分辨率低于原始版本?
- eclipse - 线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException:无法调用“java.net.URL.toExternalForm()”,因为“位置”为空
- angular - 物料表 - 无法正确实施
- vba - VBA Microsoft Project Professional 2016:如何按日期按资源按任务阅读实际时间和工作(小时)?
- c# - 再次运行应用程序时如何停止关闭的应用程序留下的线程?C#
- python - 尝试使用谷歌云文本转语音 API 时 JWT 无效
- r - 更有效的重塑替代方案?
- android - 如何将用户名获取到下一个活动并使用 textview 显示 HELLO“USER”