当前位置:网站首页>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;
边栏推荐
- Simulated 100 questions and answers of fluorination process examination in 2022
- Jerry's infrared filtering [chapter]
- 在线文本实体抽取能力,助力应用解析海量文本数据
- The research on the report "market insight into China's database security capabilities, 2022" was officially launched
- Kotlin asynchronous flow
- 居家办公更要高效-自动化办公完美提升摸鱼时间 | 社区征文
- Use of kotlin arrays, collections, and maps
- HarmonyOS-3
- The hidden corner of codefarming: five things that developers hate most
- 【R语言数据科学】(十四):随机变量和基本统计量
猜你喜欢

华为 PC 逆势增长,产品力决定一切

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

Activity生命周期

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

谷歌WayMo提出R4D: 采用参考目标做远程距离估计

快手实时数仓保障体系研发实践

SAP Marketing Cloud 功能概述(三)

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

万用表测量电阻图解及使用注意事项

Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
随机推荐
Autorf: learn the radiation field of 3D objects from single view (CVPR 2022)
HarmonyOS.2
kotlin 异步流
Manuel d'entrevue du gestionnaire de l'analyse des sources
吉时利静电计宽测量范围
杰理之开了多种解码格式插卡到播放等待的时间长【篇】
远程办公之:在家露营办公小工具| 社区征文
智源社区周刊#86:Gary Marcus谈大模型研究可借鉴的三个语言学因素;谷歌提出媲美Imgen的文生图模型Parti;OpenAI提出视频预训练模型VPT,可玩MC游戏
The project manager needs to look at six characteristics to build a team
2022质量员-设备方向-通用基础(质量员)考试题及答案
取消冒泡
知识经济时代,教会你做好知识管理
#21Set经典案例
Telecommuting: camping at home office gadgets | community essay solicitation
One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
2022 Quality Officer - Equipment direction - post skills (Quality Officer) recurrent training question bank and online simulation examination
SAP Marketing Cloud 功能概述(四)
如何在物联网低代码平台中进行任务管理?
win10系统问题
Home office should be more efficient - automated office perfectly improves fishing time | community essay solicitation