react-select - 删除动态添加的项目 [react-select]
问题描述
我正在使用反应选择控件。下拉列表中显示的项目是固定项目加上动态添加的项目的组合。
我希望能够通过在标签旁边添加一个图标直接在下拉面板中删除动态生成的项目。单击此按钮应删除该项目。
我知道以编程方式添加/删除项目的代码。这只是更新状态的一个例子。我坚持的事情是如何将 UI 添加到 react-select 下拉面板并在单击时触发单击事件。
解决方案
import React from 'react';
import Select from 'react-select';
const CustomOption = ({ innerProps }) =>
<div {...innerProps}>{/* your component internals */}</div>
class Component extends React.Component {
render() {
return <Select components={{ Option: CustomOption }} />;
}
}
这样,您可以将图标添加<span onClick={() => this.deleteOption(optionId)}>×</span>
到 CustomOption 组件并使用 cssposition: absolute
等将其放置在您想要的位置并设置样式,最好是通过className
推荐阅读
- javascript - 如何将表格数据和行动态添加到html表格中?
- javascript - Node.js multer 无法存储文件和字符串数据
- bash - 自动从github克隆到ubuntu
- scala - 如何从 StructType 数组中删除元素?
- ojs - 你知道如何正确地从 OJS 3.2.1-2 升级到 OJS 3.3.0-7 吗?
- typescript - Testcafe Role 构造函数不执行认证功能
- excel - 添加动态范围的总和
- c# - 在 Asp.Net 中更改 HttpResponse 消息和异常的文化
- python - 在 python 中处理 VBA 宏提示
- regex-group - 正则表达式:具有重复捕获组的 findall