reactjs - react-select 在 MenuList 中添加搜索输入
问题描述
我正在使用 react-select (https://react-select.com),我试图在 MenuList 本身(顶部)内添加搜索输入,而不是从容器中搜索的默认行为。
<Select
...
components={{
...
MenuList: (props: any) => {
return (
<components.MenuList {...props}>
<div className="search-wrapper">
<input
value={search}
onChange={searchChange}
placeholder="Search"/>
</div>
{props.children}
</components.MenuList>
);
},
}}
>
</Select>
它正在工作,我在菜单顶部得到了一个很好的输入:
但输入似乎被禁用。(在输入中添加 disabled=false 不会产生任何影响)
有什么想法吗?为什么会这样?实现此类功能的正确方法是什么?
解决方案
这个变体对我有用。
首先你应该innerProps
从components.Menu
. 我没有尝试过components.MenuList
,但我想它也应该可以。
<components.MenuList {...props} innerProps={undefined}>
然后您将能够使用输入,但菜单会在每次单击时关闭,因此: 其次,您应该使用menuIsOpen
prop 控制菜单状态。首先你可以覆盖 SelectContainer:
const SelectContainer: React.FC<CommonProps<object, false>> = ({ children, ...props }) => (
<components.SelectContainer {...props}>
<div onClick={() => setMenuOpen(true)}>
{children}
</div>
</components.SelectContainer>
);
然后添加一个处理程序以在单击内部菜单时关闭菜单。
推荐阅读
- python - ValueError: list.remove(x): x not in list 当我尝试从列表中删除元素时
- javascript - 防止在 ::before 伪元素下出现换行符
- python - 熊猫数据框如何制作散点图以将值列表聚类到一组组中
- azure - 在 IIS 上运行 FastAPI 应用程序时出现网关错误
- git - 为什么我的文件从本地推送后没有出现在 Github 中?
- amazon-ecs - 运行 pgadminin ecs 时运行状况检查请求超时
- javascript - ajax json表格分组行
- python - Python - 特定网页上的 WebDriverException 问题
- google-bigquery - 使用 Cloud Functions 将 Pub/Sub 消息发送到 BigQuery 的有效方式
- reactjs - 为什么 Paypal 拒绝接受描述和货币价值参数?