2017-04-17 17:14:17 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-05-29 23:09:02 +03:00
|
|
|
import { themr } from 'react-css-themr';
|
2017-01-26 20:05:32 +03:00
|
|
|
import { RADIO } from '../identifiers';
|
|
|
|
import InjectRadioButton from './RadioButton';
|
2017-03-29 01:13:52 +03:00
|
|
|
import isComponentOfType from '../utils/is-component-of-type';
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2016-05-29 23:09:02 +03:00
|
|
|
const factory = (RadioButton) => {
|
|
|
|
class RadioGroup extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.node,
|
|
|
|
className: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func,
|
2017-01-26 20:05:32 +03:00
|
|
|
value: PropTypes.string,
|
2016-05-29 23:09:02 +03:00
|
|
|
};
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2016-05-29 23:09:02 +03:00
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
2017-01-26 20:05:32 +03:00
|
|
|
disabled: false,
|
2016-05-29 23:09:02 +03:00
|
|
|
};
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2016-05-29 23:09:02 +03:00
|
|
|
handleChange = (value) => {
|
|
|
|
if (this.props.onChange) this.props.onChange(value);
|
|
|
|
};
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
renderRadioButtons() {
|
2016-09-03 17:08:52 +03:00
|
|
|
return React.Children.map(this.props.children, child => (
|
|
|
|
!isComponentOfType(RadioButton, child)
|
|
|
|
? child
|
|
|
|
: React.cloneElement(child, {
|
2017-01-26 20:05:32 +03:00
|
|
|
checked: child.props.value === this.props.value,
|
|
|
|
disabled: this.props.disabled || child.props.disabled,
|
|
|
|
onChange: this.handleChange.bind(this, child.props.value),
|
|
|
|
})
|
2016-09-03 17:02:04 +03:00
|
|
|
));
|
2016-05-29 23:09:02 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
render() {
|
2015-10-06 22:18:39 +03:00
|
|
|
return (
|
2017-01-26 20:05:32 +03:00
|
|
|
<div data-react-toolbox="radio-group" className={this.props.className}>
|
2016-05-29 23:09:02 +03:00
|
|
|
{this.renderRadioButtons()}
|
|
|
|
</div>
|
2015-10-06 22:18:39 +03:00
|
|
|
);
|
2016-05-29 23:09:02 +03:00
|
|
|
}
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-10-06 22:18:39 +03:00
|
|
|
|
2016-05-29 23:09:02 +03:00
|
|
|
return RadioGroup;
|
|
|
|
};
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-29 23:09:02 +03:00
|
|
|
const RadioGroup = factory(InjectRadioButton);
|
|
|
|
export default themr(RADIO)(RadioGroup);
|
|
|
|
export { factory as radioGroupFactory };
|
|
|
|
export { RadioGroup };
|