react-toolbox/components/radio/RadioGroup.js

54 lines
1.4 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';
2017-01-26 20:05:32 +03:00
import { RADIO } from '../identifiers';
import InjectRadioButton from './RadioButton';
import { isComponentOfType } from '../utils/react';
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
};
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
};
2016-05-29 23:09:02 +03:00
handleChange = (value) => {
if (this.props.onChange) this.props.onChange(value);
};
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-05-29 23:09:02 +03:00
}
2017-01-26 20:05:32 +03:00
render() {
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>
);
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 };