2015-07-23 22:23:11 +08:00
|
|
|
import React from 'react';
|
2015-07-17 16:06:46 +08:00
|
|
|
|
2015-07-20 22:36:49 +08:00
|
|
|
function getCheckedValue(children) {
|
2016-02-20 16:39:49 +08:00
|
|
|
let value = null;
|
2016-02-21 14:04:08 +08:00
|
|
|
let matched = false;
|
2016-01-23 15:22:16 +08:00
|
|
|
React.Children.forEach(children, (radio) => {
|
2015-07-20 22:36:49 +08:00
|
|
|
if (radio.props && radio.props.checked) {
|
2016-02-20 16:39:49 +08:00
|
|
|
value = radio.props.value;
|
2016-02-21 14:04:08 +08:00
|
|
|
matched = true;
|
2015-07-20 22:36:49 +08:00
|
|
|
}
|
|
|
|
});
|
2016-02-21 14:04:08 +08:00
|
|
|
return matched ? { value } : undefined;
|
2015-07-20 22:36:49 +08:00
|
|
|
}
|
|
|
|
|
2015-07-23 22:23:11 +08:00
|
|
|
export default React.createClass({
|
2016-01-07 17:46:46 +08:00
|
|
|
getDefaultProps() {
|
2015-07-17 16:06:46 +08:00
|
|
|
return {
|
2015-07-20 22:36:49 +08:00
|
|
|
prefixCls: 'ant-radio-group',
|
2015-11-06 22:03:09 +08:00
|
|
|
disabled: false,
|
2016-02-26 17:15:46 +08:00
|
|
|
size: 'default',
|
2016-01-07 17:46:46 +08:00
|
|
|
onChange() {
|
2016-02-21 14:04:08 +08:00
|
|
|
},
|
2015-07-17 16:06:46 +08:00
|
|
|
};
|
|
|
|
},
|
2016-01-07 17:46:46 +08:00
|
|
|
getInitialState() {
|
2015-09-01 16:18:46 +08:00
|
|
|
let props = this.props;
|
2016-02-20 16:39:49 +08:00
|
|
|
let value;
|
|
|
|
if ('value' in props) {
|
|
|
|
value = props.value;
|
|
|
|
} else if ('defaultValue' in props) {
|
|
|
|
value = props.defaultValue;
|
|
|
|
} else {
|
|
|
|
const checkedValue = getCheckedValue(props.children);
|
|
|
|
value = checkedValue && checkedValue.value;
|
|
|
|
}
|
2015-07-17 16:06:46 +08:00
|
|
|
return {
|
2016-02-20 16:39:49 +08:00
|
|
|
value,
|
2015-07-17 16:06:46 +08:00
|
|
|
};
|
|
|
|
},
|
2015-07-20 22:36:49 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-02-20 16:39:49 +08:00
|
|
|
if ('value' in nextProps) {
|
2015-07-20 22:36:49 +08:00
|
|
|
this.setState({
|
2016-02-21 14:04:08 +08:00
|
|
|
value: nextProps.value,
|
2015-07-20 22:36:49 +08:00
|
|
|
});
|
2016-02-20 16:39:49 +08:00
|
|
|
} else {
|
|
|
|
const checkedValue = getCheckedValue(nextProps.children);
|
|
|
|
if (checkedValue) {
|
|
|
|
this.setState({
|
2016-02-21 14:04:08 +08:00
|
|
|
value: checkedValue.value,
|
2016-02-20 16:39:49 +08:00
|
|
|
});
|
|
|
|
}
|
2015-07-20 22:36:49 +08:00
|
|
|
}
|
|
|
|
},
|
2016-01-21 22:45:21 +08:00
|
|
|
onRadioChange(ev) {
|
2016-02-20 16:39:49 +08:00
|
|
|
if (!('value' in this.props)) {
|
|
|
|
this.setState({
|
2016-02-21 14:04:08 +08:00
|
|
|
value: ev.target.value,
|
2016-02-20 16:39:49 +08:00
|
|
|
});
|
|
|
|
}
|
2016-01-21 22:45:21 +08:00
|
|
|
this.props.onChange(ev);
|
|
|
|
},
|
2016-01-07 17:46:46 +08:00
|
|
|
render() {
|
2016-01-21 22:45:21 +08:00
|
|
|
const props = this.props;
|
|
|
|
const children = React.Children.map(props.children, (radio) => {
|
2015-07-17 16:06:46 +08:00
|
|
|
if (radio.props) {
|
2016-02-20 16:39:49 +08:00
|
|
|
const keyProps = {};
|
|
|
|
if (!('key' in radio) && typeof radio.props.value === 'string') {
|
|
|
|
keyProps.key = radio.props.value;
|
|
|
|
}
|
2016-01-21 22:45:21 +08:00
|
|
|
return React.cloneElement(radio, {
|
2016-02-20 16:39:49 +08:00
|
|
|
...keyProps,
|
2016-01-21 22:45:21 +08:00
|
|
|
...radio.props,
|
|
|
|
onChange: this.onRadioChange,
|
|
|
|
checked: this.state.value === radio.props.value,
|
|
|
|
disabled: radio.props.disabled || this.props.disabled,
|
|
|
|
});
|
2015-07-17 16:06:46 +08:00
|
|
|
}
|
|
|
|
return radio;
|
|
|
|
});
|
|
|
|
return (
|
2016-01-21 22:45:21 +08:00
|
|
|
<div className={`${props.prefixCls} ${props.prefixCls}-${props.size}`}>
|
2015-07-17 16:43:20 +08:00
|
|
|
{children}
|
2015-07-17 16:06:46 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|