UNPKG

linkmore-design

Version:

🌈 πŸš€lmη»„δ»ΆεΊ“γ€‚πŸš€

125 lines (124 loc) β€’ 4.79 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["defaultValue", "children", "options", "prefixCls", "className", "style", "onChange"]; import classNames from 'classnames'; import omit from 'rc-util/lib/omit'; import * as React from 'react'; import { ConfigContext } from "../config-provider"; import Checkbox from "./Checkbox"; export var GroupContext = /*#__PURE__*/React.createContext(null); var InternalCheckboxGroup = function InternalCheckboxGroup(_ref, ref) { var defaultValue = _ref.defaultValue, children = _ref.children, _ref$options = _ref.options, options = _ref$options === void 0 ? [] : _ref$options, customizePrefixCls = _ref.prefixCls, className = _ref.className, style = _ref.style, onChange = _ref.onChange, restProps = _objectWithoutProperties(_ref, _excluded); 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' || typeof option === 'number') { 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); } var opts = getOptions(); onChange === null || onChange === void 0 ? void 0 : onChange(newValue.filter(function (val) { return registeredValues.includes(val); }).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.includes(option.value), onChange: option.onChange, className: "".concat(groupPrefixCls, "-item"), style: option.style }, option.label); }); } // eslint-disable-next-line react/jsx-no-constructed-context-values 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, { ref: ref }), /*#__PURE__*/React.createElement(GroupContext.Provider, { value: context }, children)); }; var CheckboxGroup = /*#__PURE__*/React.forwardRef(InternalCheckboxGroup); export default /*#__PURE__*/React.memo(CheckboxGroup);