2023-09-10 12:27:10 +08:00
|
|
|
import * as React from 'react';
|
2016-02-28 00:16:32 +08:00
|
|
|
import classNames from 'classnames';
|
2020-07-09 15:36:40 +08:00
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2023-05-28 21:32:45 +08:00
|
|
|
import pickAttrs from 'rc-util/lib/pickAttrs';
|
2023-09-10 12:27:10 +08:00
|
|
|
|
2023-05-12 14:53:47 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2024-04-08 14:04:08 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2023-05-12 14:53:47 +08:00
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { RadioGroupContextProvider } from './context';
|
2024-09-06 12:25:41 +08:00
|
|
|
import type {
|
|
|
|
RadioChangeEvent,
|
|
|
|
RadioGroupButtonStyle,
|
|
|
|
RadioGroupContextProps,
|
|
|
|
RadioGroupProps,
|
|
|
|
} from './interface';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Radio from './radio';
|
2022-04-14 19:14:54 +08:00
|
|
|
import useStyle from './style';
|
2015-07-17 16:06:46 +08:00
|
|
|
|
2020-09-03 15:31:08 +08:00
|
|
|
const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>((props, ref) => {
|
2020-05-27 10:21:17 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-03-16 12:47:24 +08:00
|
|
|
|
2022-06-14 14:00:33 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2023-01-20 11:03:50 +08:00
|
|
|
className,
|
|
|
|
rootClassName,
|
2022-06-14 14:00:33 +08:00
|
|
|
options,
|
|
|
|
buttonStyle = 'outline' as RadioGroupButtonStyle,
|
|
|
|
disabled,
|
|
|
|
children,
|
|
|
|
size: customizeSize,
|
|
|
|
style,
|
|
|
|
id,
|
2024-09-06 12:25:41 +08:00
|
|
|
optionType,
|
|
|
|
name,
|
|
|
|
defaultValue,
|
|
|
|
value: customizedValue,
|
|
|
|
onChange,
|
2022-06-14 14:00:33 +08:00
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
|
|
|
onFocus,
|
|
|
|
onBlur,
|
|
|
|
} = props;
|
2024-09-06 12:25:41 +08:00
|
|
|
|
|
|
|
const [value, setValue] = useMergedState(defaultValue, {
|
|
|
|
value: customizedValue,
|
|
|
|
});
|
|
|
|
|
|
|
|
const onRadioChange = React.useCallback(
|
|
|
|
(event: RadioChangeEvent) => {
|
|
|
|
const lastValue = value;
|
|
|
|
const val = event.target.value;
|
|
|
|
if (!('value' in props)) {
|
|
|
|
setValue(val);
|
|
|
|
}
|
|
|
|
if (val !== lastValue) {
|
|
|
|
onChange?.(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[value, setValue, onChange],
|
|
|
|
);
|
|
|
|
|
2022-06-14 14:00:33 +08:00
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
2022-07-19 17:51:35 +08:00
|
|
|
|
|
|
|
// Style
|
2023-11-16 21:24:26 +08:00
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
2022-07-19 17:51:35 +08:00
|
|
|
|
2022-06-14 14:00:33 +08:00
|
|
|
let childrenToRender = children;
|
|
|
|
// 如果存在 options, 优先使用
|
|
|
|
if (options && options.length > 0) {
|
2022-11-19 13:47:33 +08:00
|
|
|
childrenToRender = options.map((option) => {
|
2022-06-14 14:00:33 +08:00
|
|
|
if (typeof option === 'string' || typeof option === 'number') {
|
|
|
|
// 此处类型自动推导为 string
|
2019-08-05 18:38:10 +08:00
|
|
|
return (
|
|
|
|
<Radio
|
2022-06-14 14:00:33 +08:00
|
|
|
key={option.toString()}
|
2022-04-08 15:23:52 +08:00
|
|
|
prefixCls={prefixCls}
|
2022-06-14 14:00:33 +08:00
|
|
|
disabled={disabled}
|
|
|
|
value={option}
|
|
|
|
checked={value === option}
|
2019-08-05 18:38:10 +08:00
|
|
|
>
|
2022-06-14 14:00:33 +08:00
|
|
|
{option}
|
2019-08-05 18:38:10 +08:00
|
|
|
</Radio>
|
|
|
|
);
|
2022-06-14 14:00:33 +08:00
|
|
|
}
|
|
|
|
// 此处类型自动推导为 { label: string value: string }
|
|
|
|
return (
|
|
|
|
<Radio
|
|
|
|
key={`radio-group-value-options-${option.value}`}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
disabled={option.disabled || disabled}
|
|
|
|
value={option.value}
|
|
|
|
checked={value === option.value}
|
2023-06-15 09:46:05 +08:00
|
|
|
title={option.title}
|
2022-06-14 14:00:33 +08:00
|
|
|
style={option.style}
|
2023-10-12 09:43:35 +08:00
|
|
|
id={option.id}
|
2023-11-23 20:27:22 +08:00
|
|
|
required={option.required}
|
2022-06-14 14:00:33 +08:00
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Radio>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-03-28 11:56:38 +08:00
|
|
|
|
2023-05-12 14:53:47 +08:00
|
|
|
const mergedSize = useSize(customizeSize);
|
|
|
|
|
2022-06-14 14:00:33 +08:00
|
|
|
const classString = classNames(
|
|
|
|
groupPrefixCls,
|
|
|
|
`${groupPrefixCls}-${buttonStyle}`,
|
|
|
|
{
|
|
|
|
[`${groupPrefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-07-19 17:51:35 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-11-16 21:24:26 +08:00
|
|
|
rootCls,
|
2022-06-14 14:00:33 +08:00
|
|
|
);
|
2024-09-06 12:25:41 +08:00
|
|
|
|
|
|
|
const memoizedValue = React.useMemo<RadioGroupContextProps>(
|
|
|
|
() => ({ onChange: onRadioChange, value, disabled, name, optionType }),
|
|
|
|
[onRadioChange, value, disabled, name, optionType],
|
|
|
|
);
|
|
|
|
|
2023-11-16 21:24:26 +08:00
|
|
|
return wrapCSSVar(
|
2022-06-14 14:00:33 +08:00
|
|
|
<div
|
2023-09-10 12:27:10 +08:00
|
|
|
{...pickAttrs(props, { aria: true, data: true })}
|
2022-06-14 14:00:33 +08:00
|
|
|
className={classString}
|
|
|
|
style={style}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
|
|
|
id={id}
|
|
|
|
ref={ref}
|
2020-05-27 10:21:17 +08:00
|
|
|
>
|
2024-09-06 12:25:41 +08:00
|
|
|
<RadioGroupContextProvider value={memoizedValue}>
|
2020-05-27 10:21:17 +08:00
|
|
|
{childrenToRender}
|
2022-06-14 14:00:33 +08:00
|
|
|
</RadioGroupContextProvider>
|
2022-07-19 17:51:35 +08:00
|
|
|
</div>,
|
2020-05-27 10:21:17 +08:00
|
|
|
);
|
2020-09-03 15:31:08 +08:00
|
|
|
});
|
2020-05-27 10:21:17 +08:00
|
|
|
|
|
|
|
export default React.memo(RadioGroup);
|