Checkbox.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import classNames from 'classnames';
  12. import RcCheckbox from 'rc-checkbox';
  13. import { composeRef } from "rc-util/es/ref";
  14. import { devUseWarning } from '../_util/warning';
  15. import Wave from '../_util/wave';
  16. import { TARGET_CLS } from '../_util/wave/interface';
  17. import { ConfigContext } from '../config-provider';
  18. import DisabledContext from '../config-provider/DisabledContext';
  19. import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
  20. import { FormItemInputContext } from '../form/context';
  21. import GroupContext from './GroupContext';
  22. import useStyle from './style';
  23. import useBubbleLock from './useBubbleLock';
  24. const InternalCheckbox = (props, ref) => {
  25. var _a;
  26. const {
  27. prefixCls: customizePrefixCls,
  28. className,
  29. rootClassName,
  30. children,
  31. indeterminate = false,
  32. style,
  33. onMouseEnter,
  34. onMouseLeave,
  35. skipGroup = false,
  36. disabled
  37. } = props,
  38. restProps = __rest(props, ["prefixCls", "className", "rootClassName", "children", "indeterminate", "style", "onMouseEnter", "onMouseLeave", "skipGroup", "disabled"]);
  39. const {
  40. getPrefixCls,
  41. direction,
  42. checkbox
  43. } = React.useContext(ConfigContext);
  44. const checkboxGroup = React.useContext(GroupContext);
  45. const {
  46. isFormItemInput
  47. } = React.useContext(FormItemInputContext);
  48. const contextDisabled = React.useContext(DisabledContext);
  49. const mergedDisabled = (_a = (checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.disabled) || disabled) !== null && _a !== void 0 ? _a : contextDisabled;
  50. const prevValue = React.useRef(restProps.value);
  51. const checkboxRef = React.useRef(null);
  52. const mergedRef = composeRef(ref, checkboxRef);
  53. if (process.env.NODE_ENV !== 'production') {
  54. const warning = devUseWarning('Checkbox');
  55. process.env.NODE_ENV !== "production" ? warning('checked' in restProps || !!checkboxGroup || !('value' in restProps), 'usage', '`value` is not a valid prop, do you mean `checked`?') : void 0;
  56. }
  57. React.useEffect(() => {
  58. checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.registerValue(restProps.value);
  59. }, []);
  60. React.useEffect(() => {
  61. if (skipGroup) {
  62. return;
  63. }
  64. if (restProps.value !== prevValue.current) {
  65. checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.cancelValue(prevValue.current);
  66. checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.registerValue(restProps.value);
  67. prevValue.current = restProps.value;
  68. }
  69. return () => checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.cancelValue(restProps.value);
  70. }, [restProps.value]);
  71. React.useEffect(() => {
  72. var _a;
  73. if ((_a = checkboxRef.current) === null || _a === void 0 ? void 0 : _a.input) {
  74. checkboxRef.current.input.indeterminate = indeterminate;
  75. }
  76. }, [indeterminate]);
  77. const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
  78. const rootCls = useCSSVarCls(prefixCls);
  79. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
  80. const checkboxProps = Object.assign({}, restProps);
  81. if (checkboxGroup && !skipGroup) {
  82. checkboxProps.onChange = (...args) => {
  83. if (restProps.onChange) {
  84. restProps.onChange.apply(restProps, args);
  85. }
  86. if (checkboxGroup.toggleOption) {
  87. checkboxGroup.toggleOption({
  88. label: children,
  89. value: restProps.value
  90. });
  91. }
  92. };
  93. checkboxProps.name = checkboxGroup.name;
  94. checkboxProps.checked = checkboxGroup.value.includes(restProps.value);
  95. }
  96. const classString = classNames(`${prefixCls}-wrapper`, {
  97. [`${prefixCls}-rtl`]: direction === 'rtl',
  98. [`${prefixCls}-wrapper-checked`]: checkboxProps.checked,
  99. [`${prefixCls}-wrapper-disabled`]: mergedDisabled,
  100. [`${prefixCls}-wrapper-in-form-item`]: isFormItemInput
  101. }, checkbox === null || checkbox === void 0 ? void 0 : checkbox.className, className, rootClassName, cssVarCls, rootCls, hashId);
  102. const checkboxClass = classNames({
  103. [`${prefixCls}-indeterminate`]: indeterminate
  104. }, TARGET_CLS, hashId);
  105. // ============================ Event Lock ============================
  106. const [onLabelClick, onInputClick] = useBubbleLock(checkboxProps.onClick);
  107. // ============================== Render ==============================
  108. return wrapCSSVar(/*#__PURE__*/React.createElement(Wave, {
  109. component: "Checkbox",
  110. disabled: mergedDisabled
  111. }, /*#__PURE__*/React.createElement("label", {
  112. className: classString,
  113. style: Object.assign(Object.assign({}, checkbox === null || checkbox === void 0 ? void 0 : checkbox.style), style),
  114. onMouseEnter: onMouseEnter,
  115. onMouseLeave: onMouseLeave,
  116. onClick: onLabelClick
  117. }, /*#__PURE__*/React.createElement(RcCheckbox, Object.assign({}, checkboxProps, {
  118. onClick: onInputClick,
  119. prefixCls: prefixCls,
  120. className: checkboxClass,
  121. disabled: mergedDisabled,
  122. ref: mergedRef
  123. })), children !== undefined && children !== null && (/*#__PURE__*/React.createElement("span", {
  124. className: `${prefixCls}-label`
  125. }, children)))));
  126. };
  127. const Checkbox = /*#__PURE__*/React.forwardRef(InternalCheckbox);
  128. if (process.env.NODE_ENV !== 'production') {
  129. Checkbox.displayName = 'Checkbox';
  130. }
  131. export default Checkbox;