ant-design/components/radio/group.tsx

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2016-02-28 00:16:32 +08:00
import classNames from 'classnames';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import pickAttrs from 'rc-util/lib/pickAttrs';
import * as React from 'react';
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';
import useStyle from './style';
2015-07-17 16:06:46 +08:00
const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>((props, ref) => {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const [value, setValue] = useMergedState(props.defaultValue, {
value: 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 {
prefixCls: customizePrefixCls,
className,
rootClassName,
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);
let childrenToRender = children;
// 如果存在 options, 优先使用
if (options && options.length > 0) {
childrenToRender = options.map((option) => {
if (typeof option === 'string' || typeof option === 'number') {
// 此处类型自动推导为 string
2019-08-05 18:38:10 +08:00
return (
<Radio
key={option.toString()}
prefixCls={prefixCls}
disabled={disabled}
value={option}
checked={value === option}
2019-08-05 18:38:10 +08:00
>
{option}
2019-08-05 18:38:10 +08:00
</Radio>
);
}
// 此处类型自动推导为 { 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}
style={option.style}
>
{option.label}
</Radio>
);
});
}
2017-03-28 11:56:38 +08:00
const mergedSize = useSize(customizeSize);
const classString = classNames(
groupPrefixCls,
`${groupPrefixCls}-${buttonStyle}`,
{
[`${groupPrefixCls}-${mergedSize}`]: mergedSize,
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
},
className,
rootClassName,
2022-07-19 17:51:35 +08:00
hashId,
);
2022-07-19 17:51:35 +08:00
return wrapSSR(
<div
{...pickAttrs(props, {
aria: true,
data: true,
})}
className={classString}
style={style}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onFocus={onFocus}
onBlur={onBlur}
id={id}
ref={ref}
>
<RadioGroupContextProvider
value={{
onChange: onRadioChange,
value,
disabled: props.disabled,
name: props.name,
optionType: props.optionType,
}}
>
{childrenToRender}
</RadioGroupContextProvider>
2022-07-19 17:51:35 +08:00
</div>,
);
});
export default React.memo(RadioGroup);