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';
|
2022-06-21 10:24:52 +08:00
|
|
|
import * as React from 'react';
|
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';
|
2022-05-10 15:43:29 +08:00
|
|
|
import warning from '../_util/warning';
|
2022-06-21 10:24:52 +08:00
|
|
|
import RadioGroupContext, { RadioOptionTypeContext } from './context';
|
|
|
|
import type { RadioChangeEvent, RadioProps } from './interface';
|
2015-08-21 18:24:09 +08:00
|
|
|
|
2022-04-14 19:14:54 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
2020-10-09 18:01:04 +08:00
|
|
|
const InternalRadio: React.ForwardRefRenderFunction<HTMLElement, RadioProps> = (props, ref) => {
|
2022-04-08 15:23:52 +08:00
|
|
|
const groupContext = React.useContext(RadioGroupContext);
|
|
|
|
const radioOptionTypeContext = React.useContext(RadioOptionTypeContext);
|
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const innerRef = React.useRef<HTMLElement>();
|
|
|
|
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
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(!('optionType' in props), 'Radio', '`optionType` is only support in Radio.Group.');
|
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,
|
|
|
|
children,
|
|
|
|
style,
|
|
|
|
disabled: customDisabled,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
2022-04-08 15:23:52 +08:00
|
|
|
const radioPrefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
const prefixCls =
|
|
|
|
(groupContext?.optionType || radioOptionTypeContext) === 'button'
|
|
|
|
? `${radioPrefixCls}-button`
|
|
|
|
: radioPrefixCls;
|
|
|
|
|
2022-04-14 19:14:54 +08:00
|
|
|
// Style
|
2022-06-01 10:14:57 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(radioPrefixCls);
|
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);
|
|
|
|
radioProps.disabled = customDisabled || disabled;
|
|
|
|
|
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;
|
2022-04-29 20:48:10 +08:00
|
|
|
radioProps.disabled = radioProps.disabled || groupContext.disabled;
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
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
|
|
|
},
|
|
|
|
className,
|
2022-04-14 19:14:54 +08:00
|
|
|
hashId,
|
2020-09-06 13:07:39 +08:00
|
|
|
);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2022-04-14 19:14:54 +08:00
|
|
|
return wrapSSR(
|
2020-05-27 10:21:17 +08:00
|
|
|
// eslint-disable-next-line jsx-a11y/label-has-associated-control
|
|
|
|
<label
|
|
|
|
className={wrapperClassString}
|
|
|
|
style={style}
|
|
|
|
onMouseEnter={props.onMouseEnter}
|
|
|
|
onMouseLeave={props.onMouseLeave}
|
|
|
|
>
|
2021-10-13 17:13:07 +08:00
|
|
|
<RcCheckbox {...radioProps} type="radio" prefixCls={prefixCls} ref={mergedRef} />
|
2020-05-27 10:21:17 +08:00
|
|
|
{children !== undefined ? <span>{children}</span> : null}
|
2022-04-14 19:14:54 +08:00
|
|
|
</label>,
|
2020-05-27 10:21:17 +08:00
|
|
|
);
|
|
|
|
};
|
2016-11-14 11:52:18 +08:00
|
|
|
|
2020-06-02 14:12:04 +08:00
|
|
|
const Radio = React.forwardRef<unknown, 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;
|