2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-02-28 00:16:32 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-28 11:56:38 +08:00
|
|
|
import Radio from './radio';
|
2020-05-27 10:21:17 +08:00
|
|
|
import { RadioGroupProps, RadioChangeEvent, RadioGroupButtonStyle } from './interface';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-01-03 13:38:16 +08:00
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2020-02-26 16:09:22 +08:00
|
|
|
import { RadioGroupContextProvider } from './context';
|
2015-07-17 16:06:46 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const RadioGroup: React.FC<RadioGroupProps> = props => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const size = React.useContext(SizeContext);
|
2020-03-16 12:47:24 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
let initValue;
|
|
|
|
if (props.value !== undefined) {
|
|
|
|
initValue = props.value;
|
|
|
|
} else if (props.defaultValue !== undefined) {
|
|
|
|
initValue = props.defaultValue;
|
2018-12-17 15:01:55 +08:00
|
|
|
}
|
2020-05-27 10:21:17 +08:00
|
|
|
const [value, setValue] = React.useState(initValue);
|
|
|
|
const [prevPropValue, setPrevPropValue] = React.useState(props.value);
|
2018-12-17 15:01:55 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
setPrevPropValue(props.value);
|
|
|
|
if (props.value !== undefined || prevPropValue !== props.value) {
|
|
|
|
setValue(props.value);
|
2016-02-20 16:39:49 +08:00
|
|
|
}
|
2020-05-27 10:21:17 +08:00
|
|
|
}, [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-06-28 22:41:59 +08:00
|
|
|
optionType,
|
2020-01-03 13:38:16 +08:00
|
|
|
buttonStyle,
|
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-06-28 22:41:59 +08:00
|
|
|
const optionsPrefixCls = optionType === 'button' ? `${prefixCls}-button` : prefixCls;
|
2020-05-26 22:26:51 +08:00
|
|
|
childrenToRender = options.map(option => {
|
2018-12-07 20:02:01 +08:00
|
|
|
if (typeof option === 'string') {
|
|
|
|
// 此处类型自动推导为 string
|
2017-03-28 11:56:38 +08:00
|
|
|
return (
|
|
|
|
<Radio
|
2019-08-06 15:04:10 +08:00
|
|
|
key={option}
|
2020-06-28 22:41:59 +08:00
|
|
|
prefixCls={optionsPrefixCls}
|
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}`}
|
2020-06-28 22:41:59 +08:00
|
|
|
prefixCls={optionsPrefixCls}
|
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
|
|
|
|
className={classString}
|
|
|
|
style={style}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
id={id}
|
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,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{renderGroup()}
|
|
|
|
</RadioGroupContextProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
RadioGroup.defaultProps = {
|
|
|
|
buttonStyle: 'outline' as RadioGroupButtonStyle,
|
|
|
|
};
|
2018-12-17 15:01:55 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
export default React.memo(RadioGroup);
|