2015-07-17 16:06:46 +08:00
|
|
|
var React = require('react');
|
2015-07-17 16:56:40 +08:00
|
|
|
var Radio = require('./index');
|
2015-07-17 16:06:46 +08:00
|
|
|
|
2015-07-20 22:36:49 +08:00
|
|
|
function getCheckedValue(children) {
|
|
|
|
var checkedValue = null;
|
|
|
|
children.forEach(function (radio) {
|
|
|
|
if (radio.props && radio.props.checked) {
|
|
|
|
checkedValue = radio.props.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return checkedValue;
|
|
|
|
}
|
|
|
|
|
2015-07-17 16:06:46 +08:00
|
|
|
var AntRadioGroup = React.createClass({
|
|
|
|
getDefaultProps: function () {
|
|
|
|
return {
|
2015-07-20 22:36:49 +08:00
|
|
|
prefixCls: 'ant-radio-group',
|
|
|
|
onChange: function () {
|
|
|
|
}
|
2015-07-17 16:06:46 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
getInitialState: function () {
|
2015-07-20 22:36:49 +08:00
|
|
|
var props = this.props;
|
2015-07-17 16:06:46 +08:00
|
|
|
return {
|
2015-07-20 22:36:49 +08:00
|
|
|
value: props.value || props.defaultValue || getCheckedValue(props.children)
|
2015-07-17 16:06:46 +08:00
|
|
|
};
|
|
|
|
},
|
2015-07-20 22:36:49 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if ('value' in nextProps || getCheckedValue(nextProps.children)) {
|
|
|
|
this.setState({
|
|
|
|
value: nextProps.value || getCheckedValue(nextProps.children)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2015-07-17 16:06:46 +08:00
|
|
|
render: function () {
|
2015-07-20 22:36:49 +08:00
|
|
|
var props = this.props;
|
|
|
|
var children = props.children.map((radio) => {
|
2015-07-17 16:06:46 +08:00
|
|
|
if (radio.props) {
|
2015-07-20 22:36:49 +08:00
|
|
|
return <Radio
|
|
|
|
key={radio.props.value}
|
|
|
|
{...radio.props}
|
|
|
|
onChange={this.onRadioChange}
|
|
|
|
checked={this.state.value === radio.props.value}
|
2015-07-17 16:56:40 +08:00
|
|
|
/>;
|
2015-07-17 16:06:46 +08:00
|
|
|
}
|
|
|
|
return radio;
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<div className={props.prefixCls}>
|
2015-07-17 16:43:20 +08:00
|
|
|
{children}
|
2015-07-17 16:06:46 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onRadioChange: function (ev) {
|
|
|
|
this.setState({
|
2015-07-20 22:36:49 +08:00
|
|
|
value: ev.target.value
|
2015-07-17 16:06:46 +08:00
|
|
|
});
|
2015-07-20 22:36:49 +08:00
|
|
|
this.props.onChange(ev);
|
2015-07-17 16:06:46 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = AntRadioGroup;
|