2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-05-18 14:22:47 +08:00
|
|
|
import RcCheckbox from 'rc-checkbox';
|
2016-01-21 22:45:21 +08:00
|
|
|
import classNames from 'classnames';
|
2020-02-26 16:09:22 +08:00
|
|
|
import { RadioProps, RadioChangeEvent } from './interface';
|
2020-05-27 10:21:17 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-02-26 16:09:22 +08:00
|
|
|
import RadioGroupContext from './context';
|
2020-05-27 10:21:17 +08:00
|
|
|
import { composeRef } from '../_util/ref';
|
2020-06-28 22:41:59 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2015-08-21 18:24:09 +08:00
|
|
|
|
2020-10-09 18:01:04 +08:00
|
|
|
const InternalRadio: React.ForwardRefRenderFunction<HTMLElement, RadioProps> = (props, ref) => {
|
2020-05-27 10:21:17 +08:00
|
|
|
const context = React.useContext(RadioGroupContext);
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const innerRef = React.useRef<HTMLElement>();
|
|
|
|
const mergedRef = composeRef(ref, innerRef);
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2020-06-28 22:41:59 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
devWarning(!('optionType' in props), 'Radio', '`optionType` is only support in Radio.Group.');
|
|
|
|
}, []);
|
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const onChange = (e: RadioChangeEvent) => {
|
|
|
|
if (props.onChange) {
|
|
|
|
props.onChange(e);
|
2019-01-18 16:12:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
if (context?.onChange) {
|
|
|
|
context.onChange(e);
|
2019-01-18 16:12:47 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, children, style, ...restProps } = props;
|
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
const radioProps: RadioProps = { ...restProps };
|
|
|
|
if (context) {
|
|
|
|
radioProps.name = context.name;
|
|
|
|
radioProps.onChange = onChange;
|
|
|
|
radioProps.checked = props.value === context.value;
|
|
|
|
radioProps.disabled = props.disabled || context.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',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
return (
|
|
|
|
// eslint-disable-next-line jsx-a11y/label-has-associated-control
|
|
|
|
<label
|
|
|
|
className={wrapperClassString}
|
|
|
|
style={style}
|
|
|
|
onMouseEnter={props.onMouseEnter}
|
|
|
|
onMouseLeave={props.onMouseLeave}
|
|
|
|
>
|
2020-10-09 18:01:04 +08:00
|
|
|
<RcCheckbox {...radioProps} prefixCls={prefixCls} ref={mergedRef} />
|
2020-05-27 10:21:17 +08:00
|
|
|
{children !== undefined ? <span>{children}</span> : null}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
};
|
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
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
Radio.displayName = 'Radio';
|
2020-06-02 14:12:04 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
Radio.defaultProps = {
|
|
|
|
type: 'radio',
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-27 10:21:17 +08:00
|
|
|
export default Radio;
|