ant-design/components/radio/group.tsx

147 lines
3.8 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
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 { ConfigContext } from '../config-provider';
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
import useSize from '../config-provider/hooks/useSize';
2022-06-22 14:57:09 +08:00
import { RadioGroupContextProvider } from './context';
import type {
RadioChangeEvent,
RadioGroupButtonStyle,
RadioGroupContextProps,
RadioGroupProps,
} from './interface';
2022-06-22 14:57:09 +08:00
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 {
prefixCls: customizePrefixCls,
className,
rootClassName,
options,
buttonStyle = 'outline' as RadioGroupButtonStyle,
disabled,
children,
size: customizeSize,
style,
id,
optionType,
name,
defaultValue,
value: customizedValue,
onChange,
onMouseEnter,
onMouseLeave,
onFocus,
onBlur,
} = props;
const [value, setValue] = useMergedState(defaultValue, {
value: customizedValue,
});
const onRadioChange = React.useCallback(
(event: RadioChangeEvent) => {
const lastValue = value;
const val = event.target.value;
if (!('value' in props)) {
setValue(val);
}
if (val !== lastValue) {
onChange?.(event);
}
},
[value, setValue, onChange],
);
const prefixCls = getPrefixCls('radio', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`;
2022-07-19 17:51:35 +08:00
// Style
const rootCls = useCSSVarCls(prefixCls);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
2022-07-19 17:51:35 +08:00
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}
2023-06-15 09:46:05 +08:00
title={option.title}
style={option.style}
2023-10-12 09:43:35 +08:00
id={option.id}
required={option.required}
>
{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,
cssVarCls,
rootCls,
);
const memoizedValue = React.useMemo<RadioGroupContextProps>(
() => ({ onChange: onRadioChange, value, disabled, name, optionType }),
[onRadioChange, value, disabled, name, optionType],
);
return wrapCSSVar(
<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={memoizedValue}>
{childrenToRender}
</RadioGroupContextProvider>
2022-07-19 17:51:35 +08:00
</div>,
);
});
export default React.memo(RadioGroup);