ant-design/components/radio/group.tsx

133 lines
3.5 KiB
TypeScript
Raw Normal View History

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';
import { RadioGroupProps, RadioChangeEvent, RadioGroupButtonStyle } from './interface';
import { ConfigContext } from '../config-provider';
import SizeContext from '../config-provider/SizeContext';
import { RadioGroupContextProvider } from './context';
2015-07-17 16:06:46 +08:00
const RadioGroup: React.FC<RadioGroupProps> = props => {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const size = React.useContext(SizeContext);
let initValue;
if (props.value !== undefined) {
initValue = props.value;
} else if (props.defaultValue !== undefined) {
initValue = props.defaultValue;
}
const [value, setValue] = React.useState(initValue);
const [prevPropValue, setPrevPropValue] = React.useState(props.value);
React.useEffect(() => {
setPrevPropValue(props.value);
if (props.value !== undefined || prevPropValue !== props.value) {
setValue(props.value);
2016-02-20 16:39:49 +08:00
}
}, [props.value]);
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
}
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
const renderGroup = () => {
const {
prefixCls: customizePrefixCls,
className = '',
options,
buttonStyle,
disabled,
children,
size: customizeSize,
style,
id,
onMouseEnter,
onMouseLeave,
} = props;
const prefixCls = getPrefixCls('radio', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`;
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) {
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}
prefixCls={prefixCls}
disabled={disabled}
2017-03-28 11:56:38 +08:00
value={option}
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}`}
2019-08-05 18:38:10 +08:00
prefixCls={prefixCls}
disabled={option.disabled || disabled}
2019-08-05 18:38:10 +08:00
value={option.value}
checked={value === option.value}
style={option.style}
2019-08-05 18:38:10 +08:00
>
{option.label}
</Radio>
);
2017-03-28 11:56:38 +08:00
});
}
const mergedSize = customizeSize || size;
const classString = classNames(
groupPrefixCls,
`${groupPrefixCls}-${buttonStyle}`,
{
[`${groupPrefixCls}-${mergedSize}`]: mergedSize,
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
},
className,
);
return (
<div
className={classString}
style={style}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
id={id}
>
{childrenToRender}
</div>
);
};
return (
<RadioGroupContextProvider
value={{
onChange: onRadioChange,
value,
disabled: props.disabled,
name: props.name,
}}
>
{renderGroup()}
</RadioGroupContextProvider>
);
};
RadioGroup.defaultProps = {
buttonStyle: 'outline' as RadioGroupButtonStyle,
};
export default React.memo(RadioGroup);