当前位置:网站首页>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;
边栏推荐
- PM也要学会每天反省
- 【R语言数据科学】(十四):随机变量和基本统计量
- Jerry's serial port receiving IO needs to set the digital function [chapter]
- Vim 常用快捷键
- Operation of simulated examination platform of examination question bank for B certificate of Jiangxi provincial safety officer in 2022
- 2022年江西省安全员B证考试题库模拟考试平台操作
- Google Earth Engine——1999-2019年墨累全球潮汐湿地变化 v1 数据集
- Developer survey: rust/postgresql is the most popular, and PHP salary is low
- Kotlin language features
- 2022质量员-设备方向-通用基础(质量员)考试题及答案
猜你喜欢

AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)

HarmonyOS-3

10 个 Reduce 常用“奇技淫巧”

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

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

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

Cloud native essay solicitation progress case practice

Usage of multimeter

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

2022 Quality Officer - Equipment direction - post skills (Quality Officer) recurrent training question bank and online simulation examination
随机推荐
Activity生命周期
Kotlin interface generic covariant inversion
【R语言数据科学】(十四):随机变量和基本统计量
kotlin 组合挂起函数
项目经理的晋级之路
Android kotlin Encyclopedia
Seven challenges faced by data scientists and Solutions
Gatling performance test
The real project managers are all closed-loop masters!
HarmonyOS-3
【5G NR】5G NR系统架构
如何在物联网低代码平台中进行任务管理?
[5g NR] 5g NR system architecture
Tupu software is the digital twin of offshore wind power, striving to be the first
服务可见可观测性
HarmonyOS.2
Talk about GC of JVM
万用表的使用方法
System status identifier 'hum' for SAP QM inspection lot
MIT-6.824-lab4A-2022(万字讲解-代码构建)