![](/img/trans.png)
[英]How to render “N items selected” rather than the list of N selected items with react-select
[英]How to render amount selected items with react-select v2
您可以使用自定义components
执行以下操作:
使用@ lisdey89打开菜单行为更新
const ValueContainer = ({ children, ...props }) => {
const { getValue, hasValue } = props;
const nbValues = getValue().length;
if (!hasValue) {
return (
<components.ValueContainer {...props}>
{children}
</components.ValueContainer>
);
}
return (
<components.ValueContainer {...props}>
{`${nbValues} items selected`}
</components.ValueContainer>
);
};
const options = [
{ label: "label 1", value: 1 },
{ label: "label 2", value: 2 },
{ label: "label 3", value: 3 },
{ label: "label 4", value: 4 }
];
function App() {
const components = { ValueContainer };
return <Select isMulti components={components} options={options} />;
}
这是一个实例 。
在我继续根据@Laura的答案进行搜索之后,我最终得到了这个解决方案,以显示所选项目的数量并保持ValueContainer的默认行为,也许其他人可能会发现它很有用:
const ValueContainer = ({ children, ...props }) => {
const { getValue, hasValue } = props;
const nbValues = getValue().length;
if (!hasValue) {
return (
<components.ValueContainer {...props}>
{children}
</components.ValueContainer>
);
}
return (
<components.ValueContainer {...props}>
{
`${nbValues} items selected`
}
</components.ValueContainer>
);
};
这是一个例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.