acud
Version:
acg react 组件库
58 lines • 2.98 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _extends from "@babel/runtime/helpers/esm/extends";
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 } from 'react';
import classNames from 'classnames';
import RadioGroupContext from './context';
import { ConfigContext } from '../config-provider';
import { lengthLimit } from '../_util/lengthLimit';
export default (function (props) {
var customizePrefixCls = props.prefixCls,
children = props.children,
className = props.className,
onChange = props.onChange,
label = props.label,
_props$limit = props.limit,
limit = _props$limit === void 0 ? 8 : _props$limit,
restProps = __rest(props, ["prefixCls", "children", "className", "onChange", "label", "limit"]);
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-button', customizePrefixCls);
var classes = classNames(prefixCls, _defineProperty(_defineProperty({}, "".concat(prefixCls, "-disabled"), radioProps.disabled), "".concat(prefixCls, "-checked"), radioProps.checked), className);
var handleClick = useCallback(function (e) {
var _a;
var value = radioProps.value,
disabled = radioProps.disabled;
if (disabled) {
return;
}
e.target.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);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [radioProps]);
// 1.值为false或0时,不做处理,自适应宽度; 2.非零整数,超过指定个数省略展示; 3.其他情况,默认限制8字符。
var labelInfo = children;
if (limit) {
var limitLength = typeof limit === 'number' && limit > 0 ? limit : 8;
labelInfo = typeof (children || label) === 'string' ? lengthLimit(children || label, limitLength) : children || lengthLimit(label, limitLength);
}
return /*#__PURE__*/React.createElement("span", _extends({
className: classes,
onClick: handleClick
}, radioProps), labelInfo);
});