2016-03-21 21:16:38 +08:00
|
|
|
import RcRadio from 'rc-radio';
|
2017-03-23 17:19:50 +08:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-01-21 22:45:21 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-23 17:19:50 +08:00
|
|
|
import shallowEqual from 'shallowequal';
|
2015-08-21 18:24:09 +08:00
|
|
|
|
2016-08-10 10:26:42 +08:00
|
|
|
export interface RadioProps {
|
|
|
|
/** 指定当前是否选中*/
|
|
|
|
checked?: boolean;
|
|
|
|
/** 初始是否选中*/
|
|
|
|
defaultChecked?: boolean;
|
|
|
|
/** 根据 value 进行比较,判断是否选中 */
|
2016-12-15 16:25:15 +08:00
|
|
|
value?: any;
|
2016-08-10 10:26:42 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
prefixCls?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
className?: string;
|
2016-08-11 15:38:03 +08:00
|
|
|
onChange?: (e: any) => any;
|
2016-11-14 11:52:18 +08:00
|
|
|
onMouseEnter?: React.FormEventHandler<any>;
|
|
|
|
onMouseLeave?: React.FormEventHandler<any>;
|
2016-08-10 10:26:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Radio extends React.Component<RadioProps, any> {
|
|
|
|
static Group: any;
|
|
|
|
static Button: any;
|
|
|
|
|
2016-03-28 23:21:47 +08:00
|
|
|
static defaultProps = {
|
2016-05-11 09:32:33 +08:00
|
|
|
prefixCls: 'ant-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-03-15 11:01:06 +08:00
|
|
|
const { prefixCls, className, children, style, ...restProps } = this.props;
|
2017-03-23 17:19:50 +08:00
|
|
|
let radioProps: RadioProps = { ...restProps };
|
|
|
|
if (this.context.radioGroup) {
|
|
|
|
radioProps.onChange = this.context.radioGroup.onChange;
|
|
|
|
radioProps.checked = this.props.value === this.context.radioGroup.value;
|
|
|
|
radioProps.disabled = this.props.disabled || this.context.radioGroup.disabled;
|
|
|
|
}
|
2016-06-27 17:22:03 +08:00
|
|
|
const wrapperClassString = classNames({
|
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2017-03-23 17:19:50 +08:00
|
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
|
2016-11-30 10:20:23 +08:00
|
|
|
}, className);
|
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}
|
|
|
|
onMouseEnter={this.props.onMouseEnter}
|
|
|
|
onMouseLeave={this.props.onMouseLeave}
|
|
|
|
>
|
2017-03-23 17:19:50 +08:00
|
|
|
<RcRadio
|
|
|
|
{...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
|
|
|
}
|