radio.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 useBubbleLock from '../checkbox/useBubbleLock';
  18. import { ConfigContext } from '../config-provider';
  19. import DisabledContext from '../config-provider/DisabledContext';
  20. import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
  21. import { FormItemInputContext } from '../form/context';
  22. import RadioGroupContext, { RadioOptionTypeContext } from './context';
  23. import useStyle from './style';
  24. const InternalRadio = (props, ref) => {
  25. var _a, _b;
  26. const groupContext = React.useContext(RadioGroupContext);
  27. const radioOptionTypeContext = React.useContext(RadioOptionTypeContext);
  28. const {
  29. getPrefixCls,
  30. direction,
  31. radio
  32. } = React.useContext(ConfigContext);
  33. const innerRef = React.useRef(null);
  34. const mergedRef = composeRef(ref, innerRef);
  35. const {
  36. isFormItemInput
  37. } = React.useContext(FormItemInputContext);
  38. if (process.env.NODE_ENV !== 'production') {
  39. const warning = devUseWarning('Radio');
  40. process.env.NODE_ENV !== "production" ? warning(!('optionType' in props), 'usage', '`optionType` is only support in Radio.Group.') : void 0;
  41. }
  42. const onChange = e => {
  43. var _a, _b;
  44. (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, e);
  45. (_b = groupContext === null || groupContext === void 0 ? void 0 : groupContext.onChange) === null || _b === void 0 ? void 0 : _b.call(groupContext, e);
  46. };
  47. const {
  48. prefixCls: customizePrefixCls,
  49. className,
  50. rootClassName,
  51. children,
  52. style,
  53. title
  54. } = props,
  55. restProps = __rest(props, ["prefixCls", "className", "rootClassName", "children", "style", "title"]);
  56. const radioPrefixCls = getPrefixCls('radio', customizePrefixCls);
  57. const isButtonType = ((groupContext === null || groupContext === void 0 ? void 0 : groupContext.optionType) || radioOptionTypeContext) === 'button';
  58. const prefixCls = isButtonType ? `${radioPrefixCls}-button` : radioPrefixCls;
  59. // Style
  60. const rootCls = useCSSVarCls(radioPrefixCls);
  61. const [wrapCSSVar, hashId, cssVarCls] = useStyle(radioPrefixCls, rootCls);
  62. const radioProps = Object.assign({}, restProps);
  63. // ===================== Disabled =====================
  64. const disabled = React.useContext(DisabledContext);
  65. if (groupContext) {
  66. radioProps.name = groupContext.name;
  67. radioProps.onChange = onChange;
  68. radioProps.checked = props.value === groupContext.value;
  69. radioProps.disabled = (_a = radioProps.disabled) !== null && _a !== void 0 ? _a : groupContext.disabled;
  70. }
  71. radioProps.disabled = (_b = radioProps.disabled) !== null && _b !== void 0 ? _b : disabled;
  72. const wrapperClassString = classNames(`${prefixCls}-wrapper`, {
  73. [`${prefixCls}-wrapper-checked`]: radioProps.checked,
  74. [`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
  75. [`${prefixCls}-wrapper-rtl`]: direction === 'rtl',
  76. [`${prefixCls}-wrapper-in-form-item`]: isFormItemInput,
  77. [`${prefixCls}-wrapper-block`]: !!(groupContext === null || groupContext === void 0 ? void 0 : groupContext.block)
  78. }, radio === null || radio === void 0 ? void 0 : radio.className, className, rootClassName, hashId, cssVarCls, rootCls);
  79. // ============================ Event Lock ============================
  80. const [onLabelClick, onInputClick] = useBubbleLock(radioProps.onClick);
  81. // ============================== Render ==============================
  82. return wrapCSSVar(/*#__PURE__*/React.createElement(Wave, {
  83. component: "Radio",
  84. disabled: radioProps.disabled
  85. }, /*#__PURE__*/React.createElement("label", {
  86. className: wrapperClassString,
  87. style: Object.assign(Object.assign({}, radio === null || radio === void 0 ? void 0 : radio.style), style),
  88. onMouseEnter: props.onMouseEnter,
  89. onMouseLeave: props.onMouseLeave,
  90. title: title,
  91. onClick: onLabelClick
  92. }, /*#__PURE__*/React.createElement(RcCheckbox, Object.assign({}, radioProps, {
  93. className: classNames(radioProps.className, {
  94. [TARGET_CLS]: !isButtonType
  95. }),
  96. type: "radio",
  97. prefixCls: prefixCls,
  98. ref: mergedRef,
  99. onClick: onInputClick
  100. })), children !== undefined ? /*#__PURE__*/React.createElement("span", {
  101. className: `${prefixCls}-label`
  102. }, children) : null)));
  103. };
  104. const Radio = /*#__PURE__*/React.forwardRef(InternalRadio);
  105. if (process.env.NODE_ENV !== 'production') {
  106. Radio.displayName = 'Radio';
  107. }
  108. export default Radio;