当前位置:网站首页>AntD checkbox,限制选中数量
AntD checkbox,限制选中数量
2022-06-24 12:59:00 【莉兹Liz】
需求
checkbox多选框最多选中两个,选中两个后其余选项置灰;取消一个或两个选项后,所有项可选。
在网上找了一下,有这个需求但是没有实现,于是这里重写了一下,方便以后使用。
操作
1、选中一个选项
2、选中两个选项,此时已选两项,未选项置灰
3、取消一个选项,此时所有选项可选
代码
/** * checkout多选,最多选中两个,其余选项不可点击;取消选中后的项,所有选项可点击 */
import React, {
Component } from 'react';
import {
Checkbox } from 'antd';
import './index.scss';
class MyCheckbox extends Component {
constructor(props) {
super(props);
this.state = {
options: [
{
label: '杨梅', value: '杨梅', disabled: false},
{
label: '椰汁拿铁', value: '椰汁拿铁', disabled: false},
{
label: '草莓', value: '草莓', disabled: false},
{
label: '寿喜锅', value: '寿喜锅', disabled: false},
{
label: '柠檬', value: '柠檬', disabled: false},
{
label: '雪碧', value: '雪碧', disabled: false},
],
}
}
onChange = (checkedList) => {
const {
options } = this.state;
let valueList = options.map(item => item.value);
let disList = [];
valueList.forEach(item => {
if(!checkedList.includes(item)) {
disList.push(item);
}
});
if(checkedList.length === 2) {
for(let i in options) {
for(let j in disList) {
if(options[i].value === disList[j]) {
options[i].disabled = true;
}
}
}
} else {
for(let i in options) {
options[i].disabled = false;
}
}
}
render() {
const {
options } = this.state;
return(<div className='my-checkbox'>
<Checkbox.Group
options={
options}
onChange={
this.onChange}
/>
</div>)
}
}
export default MyCheckbox;
边栏推荐
- What is the difference between sap QM and UD for inspection lots with hum?
- 龙蜥开发者说:首次触电,原来你是这样的龙蜥社区? | 第 8 期
- 《中国数据库安全能力市场洞察,2022》报告研究正式启动
- Android kotlin Encyclopedia
- kotlin 初始化块
- HarmonyOS. two
- The real project managers are all closed-loop masters!
- 知识经济时代,教会你做好知识管理
- **Puzzling little problem in unity - light and sky box
- Rasa 3.x 学习系列-非常荣幸成为 Rasa contributors 源码贡献者,和全世界的Rasa源码贡献者共建共享Rasa社区!
猜你喜欢

杰理之串口接收 IO 需要设置数字功能【篇】

硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件

The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance

MIT-6.824-lab4A-2022(万字讲解-代码构建)

Activity生命周期

Golden age ticket: Web3.0 Security Manual

10 reduce common "tricks"

Research and development practice of Kwai real-time data warehouse support system

**Unity中莫名其妙得小问题-灯光和天空盒

Home office should be more efficient - automated office perfectly improves fishing time | community essay solicitation
随机推荐
黄楚平主持召开定点联系珠海工作视频会议 坚决落实省委部署要求 确保防疫情、稳经济、保安全取得积极成效
**Puzzling little problem in unity - light and sky box
Redis scenario
项目经理搭建团队,需要看6个特征
Kotlin interface generic covariant inversion
kotlin 匿名函数 与 Lambda
Yyds dry goods counting solution sword finger offer: adjust the array order so that odd numbers precede even numbers (2)
**Unity中莫名其妙得小问题-灯光和天空盒
群晖向阿里云OSS同步
Talk about GC of JVM
Docker安装redis
Operation of simulated test question bank and simulated test platform for safety production management personnel of fireworks and firecrackers production units in 2022
Gatling performance test
金鱼哥RHCA回忆录:DO447管理项目和开展作业--为ansible剧本创建一个项目
Vulnerability management mistakes that CIOs still make
《中国数据库安全能力市场洞察,2022》报告研究正式启动
Tupu software is the digital twin of offshore wind power, striving to be the first
Coinbase will launch the first encryption derivative for individual investors
Golden age ticket: Web3.0 Security Manual
Kotlin asynchronous flow