2023-09-11 17:28:04 +08:00
|
|
|
import * as React from 'react';
|
2016-01-21 22:45:21 +08:00
|
|
|
import classNames from 'classnames';
|
2022-06-21 10:24:52 +08:00
|
|
|
import RcCheckbox from 'rc-checkbox';
|
2020-11-21 15:40:06 +08:00
|
|
|
import { composeRef } from 'rc-util/lib/ref';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
import { devUseWarning } from '../_util/warning';
|
|
|
|
import Wave from '../_util/wave';
|
|
|
|
import { TARGET_CLS } from '../_util/wave/interface';
|
2020-05-27 10:21:17 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-04-29 20:48:10 +08:00
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2022-06-21 10:24:52 +08:00
|
|
|
import { FormItemInputContext } from '../form/context';
|
|
|
|
import RadioGroupContext, { RadioOptionTypeContext } from './context';
|
2023-09-12 11:40:53 +08:00
|
|
|
import type { RadioChangeEvent, RadioProps, RadioRef } from './interface';
|
2022-04-14 19:14:54 +08:00
|
|
|
import useStyle from './style';
|
2023-11-16 21:24:26 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2022-04-14 19:14:54 +08:00
|
|
|
|
2023-09-12 11:40:53 +08:00
|
|
|
const InternalRadio: React.ForwardRefRenderFunction<RadioRef, RadioProps> = (props, ref) => {
|
2022-04-08 15:23:52 +08:00
|
|
|
const groupContext = React.useContext(RadioGroupContext);
|
|
|
|
const radioOptionTypeContext = React.useContext(RadioOptionTypeContext);
|
|
|
|
|
2023-06-26 19:07:57 +08:00
|
|
|
const { getPrefixCls, direction, radio } = React.useContext(ConfigContext);
|
2023-09-12 11:40:53 +08:00
|
|
|
const innerRef = React.useRef<RadioRef>(null);
|
2020-05-27 10:21:17 +08:00
|
|
|
const mergedRef = composeRef(ref, innerRef);
|
2022-12-06 21:09:59 +08:00
|
|
|
const { isFormItemInput } = React.useContext(FormItemInputContext);
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Radio');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
warning(!('optionType' in props), 'usage', '`optionType` is only support in Radio.Group.');
|
2023-09-11 17:28:04 +08:00
|
|
|
}
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const onChange = (e: RadioChangeEvent) => {
|
2021-02-19 18:26:53 +08:00
|
|
|
props.onChange?.(e);
|
2022-04-08 15:23:52 +08:00
|
|
|
groupContext?.onChange?.(e);
|
2019-01-18 16:12:47 +08:00
|
|
|
};
|
|
|
|
|
2022-04-29 20:48:10 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-04-29 20:48:10 +08:00
|
|
|
children,
|
|
|
|
style,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
2022-04-08 15:23:52 +08:00
|
|
|
const radioPrefixCls = getPrefixCls('radio', customizePrefixCls);
|
2023-08-04 11:22:33 +08:00
|
|
|
|
|
|
|
const isButtonType = (groupContext?.optionType || radioOptionTypeContext) === 'button';
|
|
|
|
const prefixCls = isButtonType ? `${radioPrefixCls}-button` : radioPrefixCls;
|
2022-04-08 15:23:52 +08:00
|
|
|
|
2022-04-14 19:14:54 +08:00
|
|
|
// Style
|
2023-11-16 21:24:26 +08:00
|
|
|
const rootCls = useCSSVarCls(radioPrefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(radioPrefixCls, rootCls);
|
2022-04-14 19:14:54 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const radioProps: RadioProps = { ...restProps };
|
2022-04-29 20:48:10 +08:00
|
|
|
|
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
|
|
|
|
2022-04-08 15:23:52 +08:00
|
|
|
if (groupContext) {
|
|
|
|
radioProps.name = groupContext.name;
|
2020-05-27 10:21:17 +08:00
|
|
|
radioProps.onChange = onChange;
|
2022-04-08 15:23:52 +08:00
|
|
|
radioProps.checked = props.value === groupContext.value;
|
2023-02-17 10:23:34 +08:00
|
|
|
radioProps.disabled = radioProps.disabled ?? groupContext.disabled;
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
2023-02-17 10:23:34 +08:00
|
|
|
|
|
|
|
radioProps.disabled = radioProps.disabled ?? disabled;
|
2020-09-06 13:07:39 +08:00
|
|
|
const wrapperClassString = classNames(
|
2020-10-09 18:01:04 +08:00
|
|
|
`${prefixCls}-wrapper`,
|
2020-09-06 13:07:39 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
|
|
|
|
[`${prefixCls}-wrapper-rtl`]: direction === 'rtl',
|
2022-03-24 21:54:20 +08:00
|
|
|
[`${prefixCls}-wrapper-in-form-item`]: isFormItemInput,
|
2020-09-06 13:07:39 +08:00
|
|
|
},
|
2023-06-26 19:07:57 +08:00
|
|
|
radio?.className,
|
2020-09-06 13:07:39 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-04-14 19:14:54 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-11-16 21:24:26 +08:00
|
|
|
rootCls,
|
2020-09-06 13:07:39 +08:00
|
|
|
);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2023-11-16 21:24:26 +08:00
|
|
|
return wrapCSSVar(
|
2023-08-04 11:22:33 +08:00
|
|
|
<Wave component="Radio" disabled={radioProps.disabled}>
|
|
|
|
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
|
|
|
|
<label
|
|
|
|
className={wrapperClassString}
|
|
|
|
style={{ ...radio?.style, ...style }}
|
|
|
|
onMouseEnter={props.onMouseEnter}
|
|
|
|
onMouseLeave={props.onMouseLeave}
|
|
|
|
>
|
|
|
|
<RcCheckbox
|
|
|
|
{...radioProps}
|
|
|
|
className={classNames(radioProps.className, !isButtonType && TARGET_CLS)}
|
|
|
|
type="radio"
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
ref={mergedRef}
|
|
|
|
/>
|
|
|
|
{children !== undefined ? <span>{children}</span> : null}
|
|
|
|
</label>
|
|
|
|
</Wave>,
|
2020-05-27 10:21:17 +08:00
|
|
|
);
|
|
|
|
};
|
2016-11-14 11:52:18 +08:00
|
|
|
|
2023-09-12 11:40:53 +08:00
|
|
|
const Radio = React.forwardRef<RadioRef, RadioProps>(InternalRadio);
|
2020-10-09 18:01:04 +08:00
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Radio.displayName = 'Radio';
|
|
|
|
}
|
2020-06-02 14:12:04 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
export default Radio;
|