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';
|
2017-04-14 16:20:09 +08:00
|
|
|
import RadioGroup from './group';
|
|
|
|
import RadioButton from './radioButton';
|
2020-02-26 16:09:22 +08:00
|
|
|
import { RadioProps, RadioChangeEvent } from './interface';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-02-26 16:09:22 +08:00
|
|
|
import RadioGroupContext from './context';
|
2015-08-21 18:24:09 +08:00
|
|
|
|
2020-02-26 16:09:22 +08:00
|
|
|
export default class Radio extends React.PureComponent<RadioProps, {}> {
|
2017-04-14 16:20:09 +08:00
|
|
|
static Group: typeof RadioGroup;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2017-04-14 16:20:09 +08:00
|
|
|
static Button: typeof RadioButton;
|
2016-08-10 10:26:42 +08:00
|
|
|
|
2016-03-28 23:21:47 +08:00
|
|
|
static defaultProps = {
|
2017-05-18 14:22:47 +08:00
|
|
|
type: 'radio',
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2017-03-23 17:19:50 +08:00
|
|
|
|
2020-02-26 16:09:22 +08:00
|
|
|
static contextType = RadioGroupContext;
|
2018-11-09 17:38:19 +08:00
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
private rcCheckbox: any;
|
|
|
|
|
2017-11-21 15:12:03 +08:00
|
|
|
saveCheckbox = (node: any) => {
|
2017-11-19 01:41:40 +08:00
|
|
|
this.rcCheckbox = node;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2019-01-18 16:12:47 +08:00
|
|
|
onChange = (e: RadioChangeEvent) => {
|
|
|
|
if (this.props.onChange) {
|
|
|
|
this.props.onChange(e);
|
|
|
|
}
|
|
|
|
|
2020-02-26 16:09:22 +08:00
|
|
|
if (this.context?.onChange) {
|
|
|
|
this.context.onChange(e);
|
2019-01-18 16:12:47 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
focus() {
|
|
|
|
this.rcCheckbox.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.rcCheckbox.blur();
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderRadio = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2017-04-14 16:20:09 +08:00
|
|
|
const { props, context } = this;
|
2018-12-07 20:02:01 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, children, style, ...restProps } = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
2018-11-10 21:40:21 +08:00
|
|
|
const radioProps: RadioProps = { ...restProps };
|
2020-02-26 16:09:22 +08:00
|
|
|
if (context) {
|
|
|
|
radioProps.name = context.name;
|
2019-01-18 16:12:47 +08:00
|
|
|
radioProps.onChange = this.onChange;
|
2020-02-26 16:09:22 +08:00
|
|
|
radioProps.checked = props.value === context.value;
|
|
|
|
radioProps.disabled = props.disabled || context.disabled;
|
2017-03-23 17:19:50 +08:00
|
|
|
}
|
2017-04-14 16:20:09 +08:00
|
|
|
const wrapperClassString = classNames(className, {
|
2016-06-27 17:22:03 +08:00
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2017-03-23 17:19:50 +08:00
|
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-wrapper-rtl`]: direction === 'rtl',
|
2017-04-14 16:20:09 +08:00
|
|
|
});
|
2016-11-14 11:52:18 +08:00
|
|
|
|
2015-08-21 18:24:09 +08:00
|
|
|
return (
|
2019-08-06 15:02:05 +08:00
|
|
|
// eslint-disable-next-line jsx-a11y/label-has-associated-control
|
2016-11-14 11:52:18 +08:00
|
|
|
<label
|
|
|
|
className={wrapperClassString}
|
|
|
|
style={style}
|
2017-04-14 16:20:09 +08:00
|
|
|
onMouseEnter={props.onMouseEnter}
|
|
|
|
onMouseLeave={props.onMouseLeave}
|
2016-11-14 11:52:18 +08:00
|
|
|
>
|
2018-12-07 20:02:01 +08:00
|
|
|
<RcCheckbox {...radioProps} prefixCls={prefixCls} ref={this.saveCheckbox} />
|
2017-02-16 10:00:00 +08:00
|
|
|
{children !== undefined ? <span>{children}</span> : null}
|
2015-08-21 18:24:09 +08:00
|
|
|
</label>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderRadio}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|