acud
Version:
acg react 组件库
81 lines • 3.89 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import React, { useCallback, useContext, useMemo, useState } from 'react';
import classNames from 'classnames';
import RadioButton from './RadioButton';
import RadioGroup from './RadioGroup';
import RadioGroupContext from './context';
import { ConfigContext } from '../config-provider';
var InternalRadio = function InternalRadio(props, ref) {
var customizePrefixCls = props.prefixCls,
children = props.children,
className = props.className,
onChange = props.onChange,
label = props.label,
restProps = __rest(props, ["prefixCls", "children", "className", "onChange", "label"]);
var radioRef = /*#__PURE__*/React.createRef();
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
initChecked = _useState2[0],
setInitChecked = _useState2[1];
React.useImperativeHandle(ref, function () {
return radioRef.current;
});
var context = useContext(RadioGroupContext);
var radioProps = _extends({}, restProps);
if (context) {
radioProps.checked = props.value === (context === null || context === void 0 ? void 0 : context.value);
radioProps.disabled = props.disabled || (context === null || context === void 0 ? void 0 : context.disabled);
}
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls;
var prefixCls = getPrefixCls('radio', customizePrefixCls);
var classes = useMemo(function () {
// 兼容与antd交互保持一致,引入initChecked判断单独使用时是否选中
var disabled = radioProps.disabled,
checked = radioProps.checked;
var isChecked = checked === !!checked ? checked : initChecked;
return classNames(prefixCls, _defineProperty(_defineProperty(_defineProperty({}, "".concat(prefixCls, "-wrapper"), true), "".concat(prefixCls, "-disabled"), disabled), "".concat(prefixCls, "-checked"), isChecked), className);
}, [radioProps, initChecked]);
var handleClick = useCallback(function (e) {
var _a;
var value = radioProps.value,
disabled = radioProps.disabled;
var wrapClassReg = new RegExp("".concat(prefixCls, "-wrapper"));
if (disabled || wrapClassReg.test(e.target.className)) {
return;
}
e.target.value = value || true;
onChange === null || onChange === void 0 ? void 0 : onChange(e);
e.target.value = value;
(_a = context === null || context === void 0 ? void 0 : context.onChange) === null || _a === void 0 ? void 0 : _a.call(context, e);
setInitChecked(true);
}, [radioProps, setInitChecked]);
return /*#__PURE__*/React.createElement("span", _extends({
className: classes,
ref: radioRef,
onClick: handleClick
}, radioProps), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-content")
}, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-outline")
}, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-inner")
})), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-label")
}, children || label)));
};
var Radio = /*#__PURE__*/React.forwardRef(InternalRadio);
Radio.displayName = 'Radio';
Radio.Group = RadioGroup;
Radio.Button = RadioButton;
export default Radio;