123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- "use client";
- import * as React from 'react';
- import classNames from 'classnames';
- import useId from "rc-util/es/hooks/useId";
- 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';
- import { FormItemInputContext } from '../form/context';
- import { toNamePathStr } from '../form/hooks/useForm';
- const RadioGroup = /*#__PURE__*/React.forwardRef((props, ref) => {
- const {
- getPrefixCls,
- direction
- } = React.useContext(ConfigContext);
- const {
- name: formItemName
- } = React.useContext(FormItemInputContext);
- const defaultName = useId(toNamePathStr(formItemName));
- const {
- prefixCls: customizePrefixCls,
- className,
- rootClassName,
- options,
- buttonStyle = 'outline',
- disabled,
- children,
- size: customizeSize,
- style,
- id,
- optionType,
- name = defaultName,
- defaultValue,
- value: customizedValue,
- block = false,
- 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,
- className: option.className,
- 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',
- [`${groupPrefixCls}-block`]: block
- }, className, rootClassName, hashId, cssVarCls, rootCls);
- const memoizedValue = React.useMemo(() => ({
- onChange: onRadioChange,
- value,
- disabled,
- name,
- optionType,
- block
- }), [onRadioChange, value, disabled, name, optionType, block]);
- 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);
|