ant-design/components/radio/group.jsx

63 lines
1.5 KiB
React
Raw Normal View History

import React from 'react';
2015-08-21 18:24:09 +08:00
import Radio from './radio';
2015-07-17 16:06:46 +08:00
2015-07-20 22:36:49 +08:00
function getCheckedValue(children) {
2015-09-01 16:18:46 +08:00
let checkedValue = null;
2015-08-27 13:34:38 +08:00
React.Children.forEach(children, function (radio) {
2015-07-20 22:36:49 +08:00
if (radio.props && radio.props.checked) {
checkedValue = radio.props.value;
}
});
return checkedValue;
}
export default React.createClass({
2015-07-17 16:06:46 +08:00
getDefaultProps: function () {
return {
2015-07-20 22:36:49 +08:00
prefixCls: 'ant-radio-group',
disabled: false,
2015-07-20 22:36:49 +08:00
onChange: function () {
}
2015-07-17 16:06:46 +08:00
};
},
getInitialState: function () {
2015-09-01 16:18:46 +08:00
let 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-09-01 16:18:46 +08:00
let props = this.props;
let children = React.Children.map(props.children, (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}
disabled={this.props.disabled}
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
}
});