2019-07-25 14:29:50 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import RadioButton from './radio-button';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
|
|
|
|
const StyledDiv = styled.div`
|
|
|
|
display: flex;
|
|
|
|
`;
|
|
|
|
|
|
|
|
class RadioButtonGroup extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
selectedOption: this.props.selected
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
handleOptionChange = changeEvent => {
|
|
|
|
this.setState({
|
|
|
|
selectedOption: changeEvent.target.value
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-26 09:36:53 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.selected !== prevProps.selected) {
|
|
|
|
this.setState({ selectedOption: this.props.selected });
|
|
|
|
}
|
|
|
|
};
|
2019-07-25 14:29:50 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const options = this.props.options;
|
|
|
|
return (
|
|
|
|
<StyledDiv>
|
|
|
|
{options.map(option => {
|
|
|
|
return (
|
|
|
|
<RadioButton
|
2019-07-25 15:48:27 +00:00
|
|
|
key={option.value}
|
2019-07-25 14:29:50 +00:00
|
|
|
name={this.props.name}
|
|
|
|
value={option.value}
|
|
|
|
checked={this.state.selectedOption === option.value}
|
2019-07-26 09:33:34 +00:00
|
|
|
onChange={(e) => {
|
|
|
|
this.handleOptionChange(e);
|
|
|
|
this.props.onClick && this.props.onClick(e);
|
|
|
|
}}
|
2019-07-26 09:37:56 +00:00
|
|
|
|
2019-07-26 07:42:21 +00:00
|
|
|
disabled={this.props.isDisabledGroup || option.disabled}
|
2019-07-25 14:29:50 +00:00
|
|
|
label={option.label}
|
2019-07-26 07:53:02 +00:00
|
|
|
radiobuttonSpacing={this.props.radiobuttonSpacing}
|
2019-07-25 14:29:50 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2019-07-26 09:37:56 +00:00
|
|
|
)
|
|
|
|
}
|
2019-07-25 14:29:50 +00:00
|
|
|
</StyledDiv>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
RadioButtonGroup.propTypes = {
|
2019-07-26 07:42:21 +00:00
|
|
|
isDisabledGroup: PropTypes.bool,
|
2019-07-25 14:29:50 +00:00
|
|
|
name: PropTypes.string.isRequired,
|
2019-07-26 09:33:34 +00:00
|
|
|
onClick: PropTypes.func,
|
2019-07-25 14:29:50 +00:00
|
|
|
options: PropTypes.arrayOf(PropTypes.shape({
|
2019-07-26 09:37:56 +00:00
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
label: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool
|
|
|
|
})).isRequired,
|
2019-07-25 15:39:25 +00:00
|
|
|
selected: PropTypes.string.isRequired,
|
2019-07-26 07:53:02 +00:00
|
|
|
radiobuttonSpacing: PropTypes.number
|
2019-07-25 14:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RadioButtonGroup.defaultProps = {
|
2019-07-26 07:42:21 +00:00
|
|
|
isDisabledGroup: false,
|
2019-07-25 15:39:25 +00:00
|
|
|
selected: undefined,
|
2019-07-26 07:53:02 +00:00
|
|
|
radiobuttonSpacing: 33
|
2019-07-25 14:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default RadioButtonGroup;
|