2015-10-11 20:07:09 +03:00
|
|
|
import React from 'react';
|
2015-10-20 18:36:08 +03:00
|
|
|
import RadioButton from './radio_button';
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2015-10-22 02:31:17 +03:00
|
|
|
class RadioGroup extends React.Component {
|
2015-10-20 08:40:51 +03:00
|
|
|
static propTypes = {
|
2015-10-06 22:18:39 +03:00
|
|
|
className: React.PropTypes.string,
|
|
|
|
disabled: React.PropTypes.bool,
|
|
|
|
name: React.PropTypes.string,
|
|
|
|
onChange: React.PropTypes.func,
|
|
|
|
value: React.PropTypes.any
|
2015-10-20 08:40:51 +03:00
|
|
|
};
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2015-10-20 08:40:51 +03:00
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
|
|
|
disabled: false
|
|
|
|
};
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2015-10-20 08:40:51 +03:00
|
|
|
state = {
|
|
|
|
value: this.props.value
|
|
|
|
};
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2015-10-22 02:31:17 +03:00
|
|
|
handleChange = (value, event) => {
|
2015-10-06 22:18:39 +03:00
|
|
|
this.setState({value: value}, () => {
|
|
|
|
if (this.props.onChange) this.props.onChange(event, this);
|
|
|
|
});
|
2015-10-22 02:31:17 +03:00
|
|
|
};
|
2015-10-06 22:18:39 +03:00
|
|
|
|
|
|
|
renderRadioButtons () {
|
|
|
|
return React.Children.map(this.props.children, (radio, idx) => {
|
|
|
|
return (
|
|
|
|
<RadioButton
|
|
|
|
{...radio.props}
|
|
|
|
checked={radio.props.value === this.state.value}
|
|
|
|
disabled={this.props.disabled || radio.props.disabled}
|
|
|
|
key={idx}
|
|
|
|
label={radio.props.label}
|
|
|
|
onChange={this.handleChange.bind(this, radio.props.value)}
|
|
|
|
value={radio.props.value}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-10-06 22:18:39 +03:00
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<div className={this.props.className}>
|
|
|
|
{this.renderRadioButtons()}
|
|
|
|
</div>
|
|
|
|
);
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-10-06 22:18:39 +03:00
|
|
|
|
|
|
|
getValue () {
|
|
|
|
return this.state.value;
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-10-06 22:18:39 +03:00
|
|
|
|
|
|
|
setValue (value) {
|
|
|
|
this.setState({value: value});
|
|
|
|
}
|
2015-10-21 13:25:07 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
|
|
|
export default RadioGroup;
|