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';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2020-05-27 10:21:17 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-01-03 13:38:16 +08:00
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2021-05-24 16:24:00 +08:00
|
|
|
import getDataOrAriaProps from '../_util/getDataOrAriaProps';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { RadioGroupContextProvider } from './context';
|
|
|
|
import type { RadioChangeEvent, RadioGroupButtonStyle, RadioGroupProps } from './interface';
|
|
|
|
import Radio from './radio';
|
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);
|
|
|
|
const size = React.useContext(SizeContext);
|
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
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const renderGroup = () => {
|
2020-01-03 13:38:16 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className = '',
|
|
|
|
options,
|
2020-11-17 11:01:05 +08:00
|
|
|
buttonStyle = 'outline' as RadioGroupButtonStyle,
|
2020-05-26 22:26:51 +08:00
|
|
|
disabled,
|
|
|
|
children,
|
2020-01-03 13:38:16 +08:00
|
|
|
size: customizeSize,
|
2020-05-26 22:26:51 +08:00
|
|
|
style,
|
|
|
|
id,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
2020-05-27 10:21:17 +08:00
|
|
|
} = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
2018-04-22 16:35:34 +08:00
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
2020-05-26 22:26:51 +08:00
|
|
|
let childrenToRender = children;
|
2017-03-28 11:56:38 +08:00
|
|
|
// 如果存在 options, 优先使用
|
2017-11-27 10:49:17 +08:00
|
|
|
if (options && options.length > 0) {
|
2020-05-26 22:26:51 +08:00
|
|
|
childrenToRender = options.map(option => {
|
2022-01-12 22:57:43 +08:00
|
|
|
if (typeof option === 'string' || typeof option === 'number') {
|
2018-12-07 20:02:01 +08:00
|
|
|
// 此处类型自动推导为 string
|
2017-03-28 11:56:38 +08:00
|
|
|
return (
|
|
|
|
<Radio
|
2022-01-12 22:57:43 +08:00
|
|
|
key={option.toString()}
|
2022-04-08 15:23:52 +08:00
|
|
|
prefixCls={prefixCls}
|
2020-05-26 22:26:51 +08:00
|
|
|
disabled={disabled}
|
2017-03-28 11:56:38 +08:00
|
|
|
value={option}
|
2020-05-26 22:26:51 +08:00
|
|
|
checked={value === option}
|
2017-03-28 11:56:38 +08:00
|
|
|
>
|
|
|
|
{option}
|
|
|
|
</Radio>
|
|
|
|
);
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
// 此处类型自动推导为 { label: string value: string }
|
|
|
|
return (
|
|
|
|
<Radio
|
2019-08-06 15:04:10 +08:00
|
|
|
key={`radio-group-value-options-${option.value}`}
|
2022-04-08 15:23:52 +08:00
|
|
|
prefixCls={prefixCls}
|
2020-05-26 22:26:51 +08:00
|
|
|
disabled={option.disabled || disabled}
|
2019-08-05 18:38:10 +08:00
|
|
|
value={option.value}
|
2020-05-26 22:26:51 +08:00
|
|
|
checked={value === option.value}
|
2020-02-04 17:34:02 +08:00
|
|
|
style={option.style}
|
2019-08-05 18:38:10 +08:00
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Radio>
|
|
|
|
);
|
2017-03-28 11:56:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const mergedSize = customizeSize || size;
|
|
|
|
const classString = classNames(
|
|
|
|
groupPrefixCls,
|
|
|
|
`${groupPrefixCls}-${buttonStyle}`,
|
|
|
|
{
|
|
|
|
[`${groupPrefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2016-11-14 11:52:18 +08:00
|
|
|
);
|
2020-02-26 16:09:22 +08:00
|
|
|
return (
|
2020-05-27 10:21:17 +08:00
|
|
|
<div
|
2021-05-24 16:24:00 +08:00
|
|
|
{...getDataOrAriaProps(props)}
|
2020-05-27 10:21:17 +08:00
|
|
|
className={classString}
|
|
|
|
style={style}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
id={id}
|
2020-09-03 15:31:08 +08:00
|
|
|
ref={ref}
|
2020-02-26 16:09:22 +08:00
|
|
|
>
|
2020-05-27 10:21:17 +08:00
|
|
|
{childrenToRender}
|
|
|
|
</div>
|
2020-02-26 16:09:22 +08:00
|
|
|
);
|
2020-05-27 10:21:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RadioGroupContextProvider
|
|
|
|
value={{
|
|
|
|
onChange: onRadioChange,
|
|
|
|
value,
|
|
|
|
disabled: props.disabled,
|
|
|
|
name: props.name,
|
2022-04-08 15:23:52 +08:00
|
|
|
optionType: props.optionType,
|
2020-05-27 10:21:17 +08:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{renderGroup()}
|
|
|
|
</RadioGroupContextProvider>
|
|
|
|
);
|
2020-09-03 15:31:08 +08:00
|
|
|
});
|
2020-05-27 10:21:17 +08:00
|
|
|
|
|
|
|
export default React.memo(RadioGroup);
|