2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-03-21 21:16:38 +08:00
|
|
|
import Radio from './radio';
|
2018-03-08 20:34:40 +08:00
|
|
|
import { RadioChangeEvent } from './interface';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { AbstractCheckboxProps } from '../checkbox/Checkbox';
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-02-26 16:09:22 +08:00
|
|
|
import RadioGroupContext from './context';
|
2015-11-06 22:03:09 +08:00
|
|
|
|
2018-03-08 20:34:40 +08:00
|
|
|
export type RadioButtonProps = AbstractCheckboxProps<RadioChangeEvent>;
|
2016-08-10 10:26:42 +08:00
|
|
|
|
2020-03-05 16:25:47 +08:00
|
|
|
const RadioButton = (props: RadioButtonProps, ref: React.Ref<any>) => {
|
2020-02-26 16:09:22 +08:00
|
|
|
const radioGroupContext = React.useContext(RadioGroupContext);
|
2017-03-23 17:19:50 +08:00
|
|
|
|
2020-02-26 16:09:22 +08:00
|
|
|
return (
|
|
|
|
<ConfigConsumer>
|
|
|
|
{({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const { prefixCls: customizePrefixCls, ...radioProps }: RadioButtonProps = props;
|
|
|
|
const prefixCls = getPrefixCls('radio-button', customizePrefixCls);
|
|
|
|
if (radioGroupContext) {
|
|
|
|
radioProps.checked = props.value === radioGroupContext.value;
|
|
|
|
radioProps.disabled = props.disabled || radioGroupContext.disabled;
|
|
|
|
}
|
2020-03-05 16:25:47 +08:00
|
|
|
return <Radio prefixCls={prefixCls} {...radioProps} type="radio" ref={ref} />;
|
2020-02-26 16:09:22 +08:00
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
};
|
2018-11-09 17:38:19 +08:00
|
|
|
|
2020-03-05 16:25:47 +08:00
|
|
|
export default React.forwardRef(RadioButton);
|