2017-05-09 13:40:33 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
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-03-23 17:19:50 +08:00
|
|
|
import shallowEqual from 'shallowequal';
|
2017-04-14 16:20:09 +08:00
|
|
|
import { AbstractCheckboxProps } from '../checkbox/Checkbox';
|
|
|
|
import RadioGroup from './group';
|
|
|
|
import RadioButton from './radioButton';
|
2015-08-21 18:24:09 +08:00
|
|
|
|
2017-05-16 11:55:17 +08:00
|
|
|
export type RadioProps = AbstractCheckboxProps;
|
2016-08-10 10:26:42 +08:00
|
|
|
|
|
|
|
export default class Radio extends React.Component<RadioProps, any> {
|
2017-04-14 16:20:09 +08:00
|
|
|
static Group: typeof RadioGroup;
|
|
|
|
static Button: typeof RadioButton;
|
2016-08-10 10:26:42 +08:00
|
|
|
|
2016-03-28 23:21:47 +08:00
|
|
|
static defaultProps = {
|
2016-05-11 09:32:33 +08:00
|
|
|
prefixCls: 'ant-radio',
|
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
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
radioGroup: PropTypes.any,
|
|
|
|
};
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState, nextContext) {
|
|
|
|
return !shallowEqual(this.props, nextProps) ||
|
|
|
|
!shallowEqual(this.state, nextState) ||
|
|
|
|
!shallowEqual(this.context.radioGroup, nextContext.radioGroup);
|
2016-06-12 16:15:11 +08:00
|
|
|
}
|
2017-03-23 17:19:50 +08:00
|
|
|
|
2015-08-21 18:24:09 +08:00
|
|
|
render() {
|
2017-04-14 16:20:09 +08:00
|
|
|
const { props, context } = this;
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
className,
|
|
|
|
children,
|
|
|
|
style,
|
|
|
|
...restProps,
|
|
|
|
} = props;
|
|
|
|
const { radioGroup } = context;
|
2017-03-23 17:19:50 +08:00
|
|
|
let radioProps: RadioProps = { ...restProps };
|
2017-04-14 16:20:09 +08:00
|
|
|
if (radioGroup) {
|
2017-07-31 11:33:47 +08:00
|
|
|
radioProps.name = radioGroup.name;
|
2017-04-14 16:20:09 +08:00
|
|
|
radioProps.onChange = radioGroup.onChange;
|
|
|
|
radioProps.checked = props.value === radioGroup.value;
|
|
|
|
radioProps.disabled = props.disabled || radioGroup.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,
|
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 (
|
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
|
|
|
>
|
2017-05-18 14:22:47 +08:00
|
|
|
<RcCheckbox
|
2017-03-23 17:19:50 +08:00
|
|
|
{...radioProps}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
/>
|
2017-02-16 10:00:00 +08:00
|
|
|
{children !== undefined ? <span>{children}</span> : null}
|
2015-08-21 18:24:09 +08:00
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|