117 lines
3.9 KiB
JavaScript
117 lines
3.9 KiB
JavaScript
|
"use strict";
|
||
|
"use client";
|
||
|
|
||
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
||
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = void 0;
|
||
|
var React = _interopRequireWildcard(require("react"));
|
||
|
var _classnames = _interopRequireDefault(require("classnames"));
|
||
|
var _useMergedState = _interopRequireDefault(require("rc-util/lib/hooks/useMergedState"));
|
||
|
var _pickAttrs = _interopRequireDefault(require("rc-util/lib/pickAttrs"));
|
||
|
var _configProvider = require("../config-provider");
|
||
|
var _useCSSVarCls = _interopRequireDefault(require("../config-provider/hooks/useCSSVarCls"));
|
||
|
var _useSize = _interopRequireDefault(require("../config-provider/hooks/useSize"));
|
||
|
var _context = require("./context");
|
||
|
var _radio = _interopRequireDefault(require("./radio"));
|
||
|
var _style = _interopRequireDefault(require("./style"));
|
||
|
const RadioGroup = /*#__PURE__*/React.forwardRef((props, ref) => {
|
||
|
const {
|
||
|
getPrefixCls,
|
||
|
direction
|
||
|
} = React.useContext(_configProvider.ConfigContext);
|
||
|
const [value, setValue] = (0, _useMergedState.default)(props.defaultValue, {
|
||
|
value: props.value
|
||
|
});
|
||
|
const onRadioChange = ev => {
|
||
|
const lastValue = value;
|
||
|
const val = ev.target.value;
|
||
|
if (!('value' in props)) {
|
||
|
setValue(val);
|
||
|
}
|
||
|
const {
|
||
|
onChange
|
||
|
} = props;
|
||
|
if (onChange && val !== lastValue) {
|
||
|
onChange(ev);
|
||
|
}
|
||
|
};
|
||
|
const {
|
||
|
prefixCls: customizePrefixCls,
|
||
|
className,
|
||
|
rootClassName,
|
||
|
options,
|
||
|
buttonStyle = 'outline',
|
||
|
disabled,
|
||
|
children,
|
||
|
size: customizeSize,
|
||
|
style,
|
||
|
id,
|
||
|
onMouseEnter,
|
||
|
onMouseLeave,
|
||
|
onFocus,
|
||
|
onBlur
|
||
|
} = props;
|
||
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
||
|
const groupPrefixCls = `${prefixCls}-group`;
|
||
|
// Style
|
||
|
const rootCls = (0, _useCSSVarCls.default)(prefixCls);
|
||
|
const [wrapCSSVar, hashId, cssVarCls] = (0, _style.default)(prefixCls, rootCls);
|
||
|
let childrenToRender = children;
|
||
|
// 如果存在 options, 优先使用
|
||
|
if (options && options.length > 0) {
|
||
|
childrenToRender = options.map(option => {
|
||
|
if (typeof option === 'string' || typeof option === 'number') {
|
||
|
// 此处类型自动推导为 string
|
||
|
return /*#__PURE__*/React.createElement(_radio.default, {
|
||
|
key: option.toString(),
|
||
|
prefixCls: prefixCls,
|
||
|
disabled: disabled,
|
||
|
value: option,
|
||
|
checked: value === option
|
||
|
}, option);
|
||
|
}
|
||
|
// 此处类型自动推导为 { label: string value: string }
|
||
|
return /*#__PURE__*/React.createElement(_radio.default, {
|
||
|
key: `radio-group-value-options-${option.value}`,
|
||
|
prefixCls: prefixCls,
|
||
|
disabled: option.disabled || disabled,
|
||
|
value: option.value,
|
||
|
checked: value === option.value,
|
||
|
title: option.title,
|
||
|
style: option.style,
|
||
|
id: option.id,
|
||
|
required: option.required
|
||
|
}, option.label);
|
||
|
});
|
||
|
}
|
||
|
const mergedSize = (0, _useSize.default)(customizeSize);
|
||
|
const classString = (0, _classnames.default)(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, {
|
||
|
[`${groupPrefixCls}-${mergedSize}`]: mergedSize,
|
||
|
[`${groupPrefixCls}-rtl`]: direction === 'rtl'
|
||
|
}, className, rootClassName, hashId, cssVarCls, rootCls);
|
||
|
return wrapCSSVar( /*#__PURE__*/React.createElement("div", Object.assign({}, (0, _pickAttrs.default)(props, {
|
||
|
aria: true,
|
||
|
data: true
|
||
|
}), {
|
||
|
className: classString,
|
||
|
style: style,
|
||
|
onMouseEnter: onMouseEnter,
|
||
|
onMouseLeave: onMouseLeave,
|
||
|
onFocus: onFocus,
|
||
|
onBlur: onBlur,
|
||
|
id: id,
|
||
|
ref: ref
|
||
|
}), /*#__PURE__*/React.createElement(_context.RadioGroupContextProvider, {
|
||
|
value: {
|
||
|
onChange: onRadioChange,
|
||
|
value,
|
||
|
disabled: props.disabled,
|
||
|
name: props.name,
|
||
|
optionType: props.optionType
|
||
|
}
|
||
|
}, childrenToRender)));
|
||
|
});
|
||
|
var _default = exports.default = /*#__PURE__*/React.memo(RadioGroup);
|