UNPKG

3.26 kBJavaScriptView Raw
1import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2import _extends from "@babel/runtime/helpers/esm/extends";
3
4var __rest = this && this.__rest || function (s, e) {
5 var t = {};
6
7 for (var p in s) {
8 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
9 }
10
11 if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
12 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
13 }
14 return t;
15};
16
17import * as React from 'react';
18import RcCheckbox from 'rc-checkbox';
19import classNames from 'classnames';
20import { composeRef } from "rc-util/es/ref";
21import { ConfigContext } from '../config-provider';
22import RadioGroupContext from './context';
23import devWarning from '../_util/devWarning';
24
25var InternalRadio = function InternalRadio(props, ref) {
26 var _classNames;
27
28 var context = React.useContext(RadioGroupContext);
29
30 var _React$useContext = React.useContext(ConfigContext),
31 getPrefixCls = _React$useContext.getPrefixCls,
32 direction = _React$useContext.direction;
33
34 var innerRef = React.useRef();
35 var mergedRef = composeRef(ref, innerRef);
36 React.useEffect(function () {
37 devWarning(!('optionType' in props), 'Radio', '`optionType` is only support in Radio.Group.');
38 }, []);
39
40 var onChange = function onChange(e) {
41 var _a, _b;
42
43 (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, e);
44 (_b = context === null || context === void 0 ? void 0 : context.onChange) === null || _b === void 0 ? void 0 : _b.call(context, e);
45 };
46
47 var customizePrefixCls = props.prefixCls,
48 className = props.className,
49 children = props.children,
50 style = props.style,
51 restProps = __rest(props, ["prefixCls", "className", "children", "style"]);
52
53 var prefixCls = getPrefixCls('radio', customizePrefixCls);
54
55 var radioProps = _extends({}, restProps);
56
57 if (context) {
58 radioProps.name = context.name;
59 radioProps.onChange = onChange;
60 radioProps.checked = props.value === context.value;
61 radioProps.disabled = props.disabled || context.disabled;
62 }
63
64 var wrapperClassString = classNames("".concat(prefixCls, "-wrapper"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-checked"), radioProps.checked), _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-disabled"), radioProps.disabled), _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-rtl"), direction === 'rtl'), _classNames), className);
65 return (
66 /*#__PURE__*/
67 // eslint-disable-next-line jsx-a11y/label-has-associated-control
68 React.createElement("label", {
69 className: wrapperClassString,
70 style: style,
71 onMouseEnter: props.onMouseEnter,
72 onMouseLeave: props.onMouseLeave
73 }, /*#__PURE__*/React.createElement(RcCheckbox, _extends({}, radioProps, {
74 prefixCls: prefixCls,
75 ref: mergedRef
76 })), children !== undefined ? /*#__PURE__*/React.createElement("span", null, children) : null)
77 );
78};
79
80var Radio = /*#__PURE__*/React.forwardRef(InternalRadio);
81Radio.displayName = 'Radio';
82Radio.defaultProps = {
83 type: 'radio'
84};
85export default Radio;
\No newline at end of file