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.

62 lines
1.5 KiB

import React from 'react';
9 years ago
function getCheckedValue(children) {
let checkedValue = null;
React.Children.forEach(children, (radio) => {
if (radio.props && radio.props.checked) {
checkedValue = radio.props.value;
}
});
return checkedValue;
}
export default React.createClass({
getDefaultProps() {
9 years ago
return {
prefixCls: 'ant-radio-group',
disabled: false,
onChange() {
}
9 years ago
};
},
getInitialState() {
let 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)
});
}
},
onRadioChange(ev) {
this.setState({
value: ev.target.value
});
this.props.onChange(ev);
},
render() {
const props = this.props;
const children = React.Children.map(props.children, (radio) => {
9 years ago
if (radio.props) {
return React.cloneElement(radio, {
key: radio.props.value,
...radio.props,
onChange: this.onRadioChange,
checked: this.state.value === radio.props.value,
disabled: radio.props.disabled || this.props.disabled,
});
9 years ago
}
return radio;
});
return (
<div className={`${props.prefixCls} ${props.prefixCls}-${props.size}`}>
9 years ago
{children}
9 years ago
</div>
);
},
});