ant-design/components/radio/radio.tsx

92 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
import RcCheckbox from 'rc-checkbox';
2016-01-21 22:45:21 +08:00
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
import RadioGroup from './group';
import RadioButton from './radioButton';
import { RadioProps, RadioChangeEvent, RadioGroupContext } from './interface';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
2015-08-21 18:24:09 +08:00
2017-11-21 15:12:03 +08:00
export default class Radio extends React.Component<RadioProps, {}> {
static Group: typeof RadioGroup;
static Button: typeof RadioButton;
2016-08-10 10:26:42 +08:00
static defaultProps = {
type: 'radio',
2016-07-13 11:14:24 +08:00
};
static contextTypes = {
radioGroup: PropTypes.any,
};
2018-11-09 17:38:19 +08:00
context: any;
private rcCheckbox: any;
2017-11-21 15:12:03 +08:00
shouldComponentUpdate(nextProps: RadioProps, nextState: {}, nextContext: RadioGroupContext) {
2018-12-07 20:02:01 +08:00
return (
!shallowEqual(this.props, nextProps) ||
!shallowEqual(this.state, nextState) ||
!shallowEqual(this.context.radioGroup, nextContext.radioGroup)
);
2016-06-12 16:15:11 +08:00
}
focus() {
this.rcCheckbox.focus();
}
blur() {
this.rcCheckbox.blur();
}
2017-11-21 15:12:03 +08:00
saveCheckbox = (node: any) => {
this.rcCheckbox = node;
2018-12-07 20:02:01 +08:00
};
onChange = (e: RadioChangeEvent) => {
if (this.props.onChange) {
this.props.onChange(e);
}
if (this.context.radioGroup && this.context.radioGroup.onChange) {
this.context.radioGroup.onChange(e);
}
};
renderRadio = ({ getPrefixCls }: ConfigConsumerProps) => {
const { props, context } = this;
2018-12-07 20:02:01 +08:00
const { prefixCls: customizePrefixCls, className, children, style, ...restProps } = props;
const { radioGroup } = context;
const prefixCls = getPrefixCls('radio', customizePrefixCls);
2018-11-10 21:40:21 +08:00
const radioProps: RadioProps = { ...restProps };
if (radioGroup) {
radioProps.name = radioGroup.name;
radioProps.onChange = this.onChange;
radioProps.checked = props.value === radioGroup.value;
radioProps.disabled = props.disabled || radioGroup.disabled;
}
const wrapperClassString = classNames(className, {
2016-06-27 17:22:03 +08:00
[`${prefixCls}-wrapper`]: true,
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
});
2015-08-21 18:24:09 +08:00
return (
<label
className={wrapperClassString}
style={style}
onMouseEnter={props.onMouseEnter}
onMouseLeave={props.onMouseLeave}
>
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
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderRadio}</ConfigConsumer>;
}
}