-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectCheckbox.jsx
63 lines (62 loc) · 1.93 KB
/
selectCheckbox.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @Author: hys
* @Date: 2022-03-16 16:42:35
* @LastEditors: hys
* @LastEditTime: 2022-03-16 18:12:56
* @Description: 结合Select和Checkbox实现的自定义下拉多选框组件,可以根据传入的属性值得到不同的下拉多选框
* @Props:
* values: 选中的值
* setValues: 改变values的方法
* defaultValue: 多选框的选项
* type: 目前1代表节点类型,2代表边类型
*/
import React, { useState, useEffect } from 'react';
import { Select, Checkbox } from 'antd';
const { Option } = Select;
const CheckboxGroup = Checkbox.Group;
const SelectCheckbox = (props) => {
const { values, setValues, defaultValue, type } = props;
const [indeterminate, setIndeterminate] = useState(false);
const [checkAll, setCheckAll] = useState(true);
// 处理全选
const onCheckAllChange = (e) => {
setCheckAll(e.target.checked);
setValues(e.target.checked ? defaultValue : []);
setIndeterminate(false);
};
// 处理勾选
const onChange = (list) => {
setCheckAll(list.length === 2);
setValues(list);
setIndeterminate(!!list.length && list.length < 2);
};
return (
<Select
style={{ width: '120px' }}
placeholder={type === '1' ? '请选择节点类型' : '请选择边类型'}
value={type === '1' ? '节点类型' : '边类型'}
>
<Option value="">
<div className="type-item">
<Checkbox
indeterminate={indeterminate}
checked={checkAll}
onChange={(e) => onCheckAllChange(e)}
>
全选
</Checkbox>
</div>
<CheckboxGroup value={values} onChange={onChange}>
{defaultValue.map((item) => (
<div className="type-item">
<Checkbox value={item}>
<span>{item}</span>
</Checkbox>
</div>
))}
</CheckboxGroup>
</Option>
</Select>
);
};
export default SelectCheckbox;