react-toolbox/components/radio/RadioGroup.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-05-29 23:09:02 +03:00
import React, { Component, PropTypes } from 'react';
import { themr } from 'react-css-themr';
import { RADIO } from '../identifiers.js';
import InjectRadioButton from './RadioButton.js';
2016-09-03 17:08:52 +03:00
import { isComponentOfType } from '../utils/react.js';
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,
name: PropTypes.string,
onChange: PropTypes.func,
value: PropTypes.any
};
2016-05-29 23:09:02 +03:00
static defaultProps = {
className: '',
disabled: false
};
2016-05-29 23:09:02 +03:00
handleChange = (value) => {
if (this.props.onChange) this.props.onChange(value);
};
2016-05-29 23:09:02 +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, {
checked: child.props.value === this.props.value,
disabled: this.props.disabled || child.props.disabled,
onChange: this.handleChange.bind(this, child.props.value)
})
));
2016-05-29 23:09:02 +03:00
}
render () {
return (
2016-05-29 23:09:02 +03:00
<div data-react-toolbox='radio-group' className={this.props.className}>
{this.renderRadioButtons()}
</div>
);
2016-05-29 23:09:02 +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 };