antd
Version:
An enterprise-class UI design language and React components implementation
113 lines (112 loc) • 3.6 kB
JavaScript
"use client";
import * as React from 'react';
import classNames from 'classnames';
import useMergedState from "rc-util/es/hooks/useMergedState";
import pickAttrs from "rc-util/es/pickAttrs";
import { ConfigContext } from '../config-provider';
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
import useSize from '../config-provider/hooks/useSize';
import { RadioGroupContextProvider } from './context';
import Radio from './radio';
import useStyle from './style';
const RadioGroup = /*#__PURE__*/React.forwardRef((props, ref) => {
const {
getPrefixCls,
direction
} = React.useContext(ConfigContext);
const {
prefixCls: customizePrefixCls,
className,
rootClassName,
options,
buttonStyle = 'outline',
disabled,
children,
size: customizeSize,
style,
id,
optionType,
name,
defaultValue,
value: customizedValue,
onChange,
onMouseEnter,
onMouseLeave,
onFocus,
onBlur
} = props;
const [value, setValue] = useMergedState(defaultValue, {
value: customizedValue
});
const onRadioChange = React.useCallback(event => {
const lastValue = value;
const val = event.target.value;
if (!('value' in props)) {
setValue(val);
}
if (val !== lastValue) {
onChange === null || onChange === void 0 ? void 0 : onChange(event);
}
}, [value, setValue, onChange]);
const prefixCls = getPrefixCls('radio', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`;
// Style
const rootCls = useCSSVarCls(prefixCls);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(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, {
key: option.toString(),
prefixCls: prefixCls,
disabled: disabled,
value: option,
checked: value === option
}, option);
}
// 此处类型自动推导为 { label: string value: string }
return /*#__PURE__*/React.createElement(Radio, {
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 = useSize(customizeSize);
const classString = classNames(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, {
[`${groupPrefixCls}-${mergedSize}`]: mergedSize,
[`${groupPrefixCls}-rtl`]: direction === 'rtl'
}, className, rootClassName, hashId, cssVarCls, rootCls);
const memoizedValue = React.useMemo(() => ({
onChange: onRadioChange,
value,
disabled,
name,
optionType
}), [onRadioChange, value, disabled, name, optionType]);
return wrapCSSVar(/*#__PURE__*/React.createElement("div", Object.assign({}, pickAttrs(props, {
aria: true,
data: true
}), {
className: classString,
style: style,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave,
onFocus: onFocus,
onBlur: onBlur,
id: id,
ref: ref
}), /*#__PURE__*/React.createElement(RadioGroupContextProvider, {
value: memoizedValue
}, childrenToRender)));
});
export default /*#__PURE__*/React.memo(RadioGroup);