1 | "use client";
|
2 |
|
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 RcCheckbox from 'rc-checkbox';
|
14 | import { composeRef } from "rc-util/es/ref";
|
15 | import { devUseWarning } from '../_util/warning';
|
16 | import Wave from '../_util/wave';
|
17 | import { TARGET_CLS } from '../_util/wave/interface';
|
18 | import { ConfigContext } from '../config-provider';
|
19 | import DisabledContext from '../config-provider/DisabledContext';
|
20 | import { FormItemInputContext } from '../form/context';
|
21 | import RadioGroupContext, { RadioOptionTypeContext } from './context';
|
22 | import useStyle from './style';
|
23 | import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
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 |
|
60 | const rootCls = useCSSVarCls(radioPrefixCls);
|
61 | const [wrapCSSVar, hashId, cssVarCls] = useStyle(radioPrefixCls, rootCls);
|
62 | const radioProps = Object.assign({}, restProps);
|
63 |
|
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( React.createElement(Wave, {
|
79 | component: "Radio",
|
80 | disabled: radioProps.disabled
|
81 | }, 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 | }, 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 ? React.createElement("span", null, children) : null)));
|
93 | };
|
94 | const Radio = React.forwardRef(InternalRadio);
|
95 | if (process.env.NODE_ENV !== 'production') {
|
96 | Radio.displayName = 'Radio';
|
97 | }
|
98 | export default Radio; |
\ | No newline at end of file |