Group.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use client";
  2. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  3. var __rest = this && this.__rest || function (s, e) {
  4. var t = {};
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  8. }
  9. return t;
  10. };
  11. import * as React from 'react';
  12. import classNames from 'classnames';
  13. import omit from "rc-util/es/omit";
  14. import { ConfigContext } from '../config-provider';
  15. import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
  16. import Checkbox from './Checkbox';
  17. import GroupContext from './GroupContext';
  18. import useStyle from './style';
  19. const CheckboxGroup = /*#__PURE__*/React.forwardRef((props, ref) => {
  20. const {
  21. defaultValue,
  22. children,
  23. options = [],
  24. prefixCls: customizePrefixCls,
  25. className,
  26. rootClassName,
  27. style,
  28. onChange
  29. } = props,
  30. restProps = __rest(props, ["defaultValue", "children", "options", "prefixCls", "className", "rootClassName", "style", "onChange"]);
  31. const {
  32. getPrefixCls,
  33. direction
  34. } = React.useContext(ConfigContext);
  35. const [value, setValue] = React.useState(restProps.value || defaultValue || []);
  36. const [registeredValues, setRegisteredValues] = React.useState([]);
  37. React.useEffect(() => {
  38. if ('value' in restProps) {
  39. setValue(restProps.value || []);
  40. }
  41. }, [restProps.value]);
  42. const memoizedOptions = React.useMemo(() => options.map(option => {
  43. if (typeof option === 'string' || typeof option === 'number') {
  44. return {
  45. label: option,
  46. value: option
  47. };
  48. }
  49. return option;
  50. }), [options]);
  51. const cancelValue = val => {
  52. setRegisteredValues(prevValues => prevValues.filter(v => v !== val));
  53. };
  54. const registerValue = val => {
  55. setRegisteredValues(prevValues => [].concat(_toConsumableArray(prevValues), [val]));
  56. };
  57. const toggleOption = option => {
  58. const optionIndex = value.indexOf(option.value);
  59. const newValue = _toConsumableArray(value);
  60. if (optionIndex === -1) {
  61. newValue.push(option.value);
  62. } else {
  63. newValue.splice(optionIndex, 1);
  64. }
  65. if (!('value' in restProps)) {
  66. setValue(newValue);
  67. }
  68. onChange === null || onChange === void 0 ? void 0 : onChange(newValue.filter(val => registeredValues.includes(val)).sort((a, b) => {
  69. const indexA = memoizedOptions.findIndex(opt => opt.value === a);
  70. const indexB = memoizedOptions.findIndex(opt => opt.value === b);
  71. return indexA - indexB;
  72. }));
  73. };
  74. const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
  75. const groupPrefixCls = `${prefixCls}-group`;
  76. const rootCls = useCSSVarCls(prefixCls);
  77. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
  78. const domProps = omit(restProps, ['value', 'disabled']);
  79. const childrenNode = options.length ? memoizedOptions.map(option => (/*#__PURE__*/React.createElement(Checkbox, {
  80. prefixCls: prefixCls,
  81. key: option.value.toString(),
  82. disabled: 'disabled' in option ? option.disabled : restProps.disabled,
  83. value: option.value,
  84. checked: value.includes(option.value),
  85. onChange: option.onChange,
  86. className: classNames(`${groupPrefixCls}-item`, option.className),
  87. style: option.style,
  88. title: option.title,
  89. id: option.id,
  90. required: option.required
  91. }, option.label))) : children;
  92. const memoizedContext = React.useMemo(() => ({
  93. toggleOption,
  94. value,
  95. disabled: restProps.disabled,
  96. name: restProps.name,
  97. // https://github.com/ant-design/ant-design/issues/16376
  98. registerValue,
  99. cancelValue
  100. }), [toggleOption, value, restProps.disabled, restProps.name, registerValue, cancelValue]);
  101. const classString = classNames(groupPrefixCls, {
  102. [`${groupPrefixCls}-rtl`]: direction === 'rtl'
  103. }, className, rootClassName, cssVarCls, rootCls, hashId);
  104. return wrapCSSVar(/*#__PURE__*/React.createElement("div", Object.assign({
  105. className: classString,
  106. style: style
  107. }, domProps, {
  108. ref: ref
  109. }), /*#__PURE__*/React.createElement(GroupContext.Provider, {
  110. value: memoizedContext
  111. }, childrenNode)));
  112. });
  113. export { GroupContext };
  114. export default CheckboxGroup;