You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.4 KiB

import React from 'react';
import Radio from './radio';
9 years ago
function getCheckedValue(children) {
var checkedValue = null;
9 years ago
React.Children.forEach(children, function (radio) {
if (radio.props && radio.props.checked) {
checkedValue = radio.props.value;
}
});
return checkedValue;
}
export default React.createClass({
9 years ago
getDefaultProps: function () {
return {
prefixCls: 'ant-radio-group',
onChange: function () {
}
9 years ago
};
},
getInitialState: function () {
var props = this.props;
9 years ago
return {
value: props.value || props.defaultValue || getCheckedValue(props.children)
9 years ago
};
},
componentWillReceiveProps(nextProps) {
if ('value' in nextProps || getCheckedValue(nextProps.children)) {
this.setState({
value: nextProps.value || getCheckedValue(nextProps.children)
});
}
},
9 years ago
render: function () {
var props = this.props;
9 years ago
var children = React.Children.map(props.children, (radio) => {
9 years ago
if (radio.props) {
return <Radio
key={radio.props.value}
{...radio.props}
onChange={this.onRadioChange}
checked={this.state.value === radio.props.value}
/>;
9 years ago
}
return radio;
});
return (
<div className={props.prefixCls}>
9 years ago
{children}
9 years ago
</div>
);
},
onRadioChange: function (ev) {
this.setState({
value: ev.target.value
9 years ago
});
this.props.onChange(ev);
9 years ago
}
});