当前位置:网站首页>Antd checkbox, limit the selected quantity
Antd checkbox, limit the selected quantity
2022-06-24 13:58:00 【Liz Liz】
demand
checkbox Select at most two in the multiple selection box , When two are selected, the remaining options are grayed out ; After canceling one or two options , All items are optional .
I found it on the Internet , There is this requirement but it has not been realized , So here is a rewrite , Convenient for future use .
operation
1、 Select an option 
2、 Check the two options , Two items have been selected , No option grayed out 
3、 Cancel an option , At this time, all options are optional 
Code
/** * checkout multi-select , Select up to two , Other options are not clickable ; Uncheck the item , All options can be clicked */
import React, {
Component } from 'react';
import {
Checkbox } from 'antd';
import './index.scss';
class MyCheckbox extends Component {
constructor(props) {
super(props);
this.state = {
options: [
{
label: ' Arbutus ', value: ' Arbutus ', disabled: false},
{
label: ' Coconut Latte ', value: ' Coconut Latte ', disabled: false},
{
label: ' strawberry ', value: ' strawberry ', disabled: false},
{
label: ' sukiyaki ', value: ' sukiyaki ', disabled: false},
{
label: ' lemon ', value: ' lemon ', disabled: false},
{
label: ' Sprite ', value: ' Sprite ', 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;
边栏推荐
- Telecommuting: camping at home office gadgets | community essay solicitation
- Kotlin asynchronous flow
- Jerry's test mic energy automatic recording automatic playback reference [article]
- 龙蜥开发者说:首次触电,原来你是这样的龙蜥社区? | 第 8 期
- redis 数据类型详解
- Docker安装redis
- Docker installation redis
- Simulated 100 questions and answers of fluorination process examination in 2022
- kotlin 接口 泛型 协变 逆变
- The hidden corner of codefarming: five things that developers hate most
猜你喜欢

【R语言数据科学】(十四):随机变量和基本统计量

HarmonyOS-3

Google waymo proposed r4d: remote distance estimation using reference target

一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?

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

Eight major trends in the industrial Internet of things (iiot)

The research on the report "market insight into China's database security capabilities, 2022" was officially launched

Daily question 8-515 Find the maximum value in each tree row

How to avoid serious network security accidents?

万用表测量电阻图解及使用注意事项
随机推荐
国内首款开源MySQL HTAP数据库即将发布,三大看点提前告知
Usage of multimeter
发扬连续作战优良作风 全力以赴确保北江大堤安全
**Unity中莫名其妙得小问题-灯光和天空盒
杰理之红外滤波【篇】
Vulnerability management mistakes that CIOs still make
Jerry's serial port receiving IO needs to set the digital function [chapter]
Kotlin keyword extension function
知识经济时代,教会你做好知识管理
Docker安装redis
kotlin 继承、类、重载
Operation of simulated examination platform for examination questions of coal production and operation units (safety production management personnel) in 2022
Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
Kotlin language features
真正的项目经理强者,都是闭环高手!
MIT-6.824-lab4A-2022(万字讲解-代码构建)
美国会参议院推进两党枪支安全法案
万用表测量电阻图解及使用注意事项
杰理之检测 MIC 能量自动录音自动播放参考【篇】
杰理之.睡眠以后定时唤醒系统继续跑不复位【篇】