Group.js
4.79 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import classNames from 'classnames';
import omit from 'omit.js';
import Checkbox from './Checkbox';
import { ConfigContext } from '../config-provider';
export var GroupContext = /*#__PURE__*/React.createContext(null);
var CheckboxGroup = function CheckboxGroup(_a) {
var defaultValue = _a.defaultValue,
children = _a.children,
_a$options = _a.options,
options = _a$options === void 0 ? [] : _a$options,
customizePrefixCls = _a.prefixCls,
className = _a.className,
style = _a.style,
onChange = _a.onChange,
restProps = __rest(_a, ["defaultValue", "children", "options", "prefixCls", "className", "style", "onChange"]);
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var _React$useState = React.useState(restProps.value || defaultValue || []),
_React$useState2 = _slicedToArray(_React$useState, 2),
value = _React$useState2[0],
setValue = _React$useState2[1];
var _React$useState3 = React.useState([]),
_React$useState4 = _slicedToArray(_React$useState3, 2),
registeredValues = _React$useState4[0],
setRegisteredValues = _React$useState4[1];
React.useEffect(function () {
if ('value' in restProps) {
setValue(restProps.value || []);
}
}, [restProps.value]);
var getOptions = function getOptions() {
return options.map(function (option) {
if (typeof option === 'string') {
return {
label: option,
value: option
};
}
return option;
});
};
var cancelValue = function cancelValue(val) {
setRegisteredValues(function (prevValues) {
return prevValues.filter(function (v) {
return v !== val;
});
});
};
var registerValue = function registerValue(val) {
setRegisteredValues(function (prevValues) {
return [].concat(_toConsumableArray(prevValues), [val]);
});
};
var toggleOption = function toggleOption(option) {
var optionIndex = value.indexOf(option.value);
var newValue = _toConsumableArray(value);
if (optionIndex === -1) {
newValue.push(option.value);
} else {
newValue.splice(optionIndex, 1);
}
if (!('value' in restProps)) {
setValue(newValue);
}
if (onChange) {
var opts = getOptions();
onChange(newValue.filter(function (val) {
return registeredValues.indexOf(val) !== -1;
}).sort(function (a, b) {
var indexA = opts.findIndex(function (opt) {
return opt.value === a;
});
var indexB = opts.findIndex(function (opt) {
return opt.value === b;
});
return indexA - indexB;
}));
}
};
var prefixCls = getPrefixCls('checkbox', customizePrefixCls);
var groupPrefixCls = "".concat(prefixCls, "-group");
var domProps = omit(restProps, ['value', 'disabled']);
if (options && options.length > 0) {
children = getOptions().map(function (option) {
return /*#__PURE__*/React.createElement(Checkbox, {
prefixCls: prefixCls,
key: option.value.toString(),
disabled: 'disabled' in option ? option.disabled : restProps.disabled,
value: option.value,
checked: value.indexOf(option.value) !== -1,
onChange: option.onChange,
className: "".concat(groupPrefixCls, "-item"),
style: option.style
}, option.label);
});
}
var context = {
toggleOption: toggleOption,
value: value,
disabled: restProps.disabled,
name: restProps.name,
// https://github.com/ant-design/ant-design/issues/16376
registerValue: registerValue,
cancelValue: cancelValue
};
var classString = classNames(groupPrefixCls, _defineProperty({}, "".concat(groupPrefixCls, "-rtl"), direction === 'rtl'), className);
return /*#__PURE__*/React.createElement("div", _extends({
className: classString,
style: style
}, domProps), /*#__PURE__*/React.createElement(GroupContext.Provider, {
value: context
}, children));
};
export default /*#__PURE__*/React.memo(CheckboxGroup);