javascript - React select - 更改所选值的显示方式
问题描述
我在基于 ReactJs 的应用程序中使用反应选择控件。我将其用作多选控件。但是一旦用户选择了多个值,而不是显示所有选定的值,我想显示第一个选定的值 + N。所以如果选择了两个值,我想说'XYZ'+ 1。如果只有一个值被选中我会说'XYZ'。这是一个工作示例
解决方案
您需要像下面那样覆盖 ValueContainer。工作沙箱
const ValueContainer = props => {
let length = props.getValue().length;
return (
<components.ValueContainer {...props}>
{length > 1 ? (
<>
{props.children[0][0]}
{!props.selectProps.menuIsOpen && `${length - 1} Items`}
{React.cloneElement(props.children[1])}
</>
) : (
<>{props.children}</>
)}
</components.ValueContainer>
);
};
在选择中,您需要覆盖
<Select
components={{ValueContainer}}
hideSelectedOptions={false}
...
/>
推荐阅读
- mysql - 使用 mdbtools 将 access db 转换为 sql
- python - 如何使用pytorch同时迭代两个数据加载器?
- c++ - Tesseract/Leptonica 处理单页和多页图像的正确方法?
- android - 我应该对片段中的每个方法进行单元测试吗?
- symfony - Symfony 外键(我如何编辑它们?)
- gun - 监听 Gun 服务器中的数据变化
- c# - 方法重载c#的替代方法
- c++ - 如何在Qt中围绕固定点制作动画?
- sql - 第一个星期日的Oracle查询
- java - 使用 SMTP 发送电子邮件时出错:javax.mail.NoSuchProviderException:无法找到协议提供者:smtp