UNPKG

4.58 kBJavaScriptView Raw
1"use client";
2
3var __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};
11import * as React from 'react';
12import classNames from 'classnames';
13import RcCheckbox from 'rc-checkbox';
14import { composeRef } from "rc-util/es/ref";
15import { devUseWarning } from '../_util/warning';
16import Wave from '../_util/wave';
17import { TARGET_CLS } from '../_util/wave/interface';
18import { ConfigContext } from '../config-provider';
19import DisabledContext from '../config-provider/DisabledContext';
20import { FormItemInputContext } from '../form/context';
21import RadioGroupContext, { RadioOptionTypeContext } from './context';
22import useStyle from './style';
23import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
24const 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 }, radio === null || radio === void 0 ? void 0 : radio.className, className, rootClassName, hashId, cssVarCls, rootCls);
78 return wrapCSSVar( /*#__PURE__*/React.createElement(Wave, {
79 component: "Radio",
80 disabled: radioProps.disabled
81 }, /*#__PURE__*/React.createElement("label", {
82 className: wrapperClassString,
83 style: Object.assign(Object.assign({}, radio === null || radio === void 0 ? void 0 : radio.style), style),
84 onMouseEnter: props.onMouseEnter,
85 onMouseLeave: props.onMouseLeave,
86 title: title
87 }, /*#__PURE__*/React.createElement(RcCheckbox, Object.assign({}, radioProps, {
88 className: classNames(radioProps.className, !isButtonType && TARGET_CLS),
89 type: "radio",
90 prefixCls: prefixCls,
91 ref: mergedRef
92 })), children !== undefined ? /*#__PURE__*/React.createElement("span", null, children) : null)));
93};
94const Radio = /*#__PURE__*/React.forwardRef(InternalRadio);
95if (process.env.NODE_ENV !== 'production') {
96 Radio.displayName = 'Radio';
97}
98export default Radio;
\No newline at end of file