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-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-05-12 14:53:47 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { RadioGroupContextProvider } from './context';
|
|
|
|
import type { RadioChangeEvent, RadioGroupButtonStyle, RadioGroupProps } from './interface';
|
|
|
|
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
|
|
|
|
2020-07-09 15:36:40 +08:00
|
|
|
const [value, setValue] = useMergedState(props.defaultValue, {
|
|
|
|
value: props.value,
|
|
|
|
});
|
2017-03-23 17:19:50 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const onRadioChange = (ev: RadioChangeEvent) => {
|
|
|
|
const lastValue = value;
|
|
|
|
const val = ev.target.value;
|
|
|
|
if (!('value' in props)) {
|
|
|
|
setValue(val);
|
2016-02-20 16:39:49 +08:00
|
|
|
}
|
2020-05-27 10:21:17 +08:00
|
|
|
const { onChange } = props;
|
|
|
|
if (onChange && val !== lastValue) {
|
2016-10-24 16:30:38 +08:00
|
|
|
onChange(ev);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2019-01-18 18:12:56 +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,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
|
|
|
onFocus,
|
|
|
|
onBlur,
|
|
|
|
} = props;
|
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
2022-07-19 17:51:35 +08:00
|
|
|
|
|
|
|
// Style
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
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}
|
|
|
|
>
|
|
|
|
{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,
|
2022-06-14 14:00:33 +08:00
|
|
|
);
|
2022-07-19 17:51:35 +08:00
|
|
|
return wrapSSR(
|
2022-06-14 14:00:33 +08:00
|
|
|
<div
|
2023-05-28 21:32:45 +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
|
|
|
>
|
2022-06-14 14:00:33 +08:00
|
|
|
<RadioGroupContextProvider
|
|
|
|
value={{
|
|
|
|
onChange: onRadioChange,
|
|
|
|
value,
|
|
|
|
disabled: props.disabled,
|
|
|
|
name: props.name,
|
|
|
|
optionType: props.optionType,
|
|
|
|
}}
|
2020-02-26 16:09:22 +08:00
|
|
|
>
|
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);
|