2020-10-16 13:16:01 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import RadioButton from "../radio-button";
|
2021-02-09 11:41:50 +00:00
|
|
|
import StyledDiv from "./styled-radio-button-group";
|
2019-07-25 14:29:50 +00:00
|
|
|
|
|
|
|
class RadioButtonGroup extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2020-10-16 13:16:01 +00:00
|
|
|
selectedOption: this.props.selected,
|
2019-07-25 14:29:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
handleOptionChange = (changeEvent) => {
|
2019-07-25 14:29:50 +00:00
|
|
|
this.setState({
|
2020-10-16 13:16:01 +00:00
|
|
|
selectedOption: changeEvent.target.value,
|
2019-07-25 14:29:50 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-26 09:36:53 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.selected !== prevProps.selected) {
|
|
|
|
this.setState({ selectedOption: this.props.selected });
|
|
|
|
}
|
2019-09-10 07:03:36 +00:00
|
|
|
}
|
2019-07-25 14:29:50 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const options = this.props.options;
|
|
|
|
return (
|
2019-12-03 12:45:13 +00:00
|
|
|
<StyledDiv
|
|
|
|
id={this.props.id}
|
|
|
|
className={this.props.className}
|
|
|
|
style={this.props.style}
|
2019-12-10 06:28:45 +00:00
|
|
|
orientation={this.props.orientation}
|
|
|
|
width={this.props.width}
|
2019-12-03 12:45:13 +00:00
|
|
|
>
|
2020-10-16 13:16:01 +00:00
|
|
|
{options.map((option) => {
|
2019-07-25 14:29:50 +00:00
|
|
|
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}
|
2019-07-26 12:43:13 +00:00
|
|
|
isChecked={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 12:43:13 +00:00
|
|
|
isDisabled={this.props.isDisabled || option.disabled}
|
2019-07-25 14:29:50 +00:00
|
|
|
label={option.label}
|
2019-12-27 12:08:23 +00:00
|
|
|
fontSize={this.props.fontSize}
|
|
|
|
fontWeight={this.props.fontWeight}
|
2019-07-26 09:42:30 +00:00
|
|
|
spacing={this.props.spacing}
|
2019-12-10 06:28:45 +00:00
|
|
|
orientation={this.props.orientation}
|
2019-07-25 14:29:50 +00:00
|
|
|
/>
|
2020-10-16 13:16:01 +00:00
|
|
|
);
|
|
|
|
})}
|
2019-07-25 14:29:50 +00:00
|
|
|
</StyledDiv>
|
|
|
|
);
|
2019-09-10 07:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-25 14:29:50 +00:00
|
|
|
|
|
|
|
RadioButtonGroup.propTypes = {
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Disabling all radiobutton in group */
|
2019-07-26 09:42:30 +00:00
|
|
|
isDisabled: PropTypes.bool,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Used as HTML `name` property for `<input>` tag. Used for identification RadioButtonGroup */
|
2019-07-25 14:29:50 +00:00
|
|
|
name: PropTypes.string.isRequired,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Allow you to handle clicking events on `<RadioButton />` component */
|
2019-07-26 09:33:34 +00:00
|
|
|
onClick: PropTypes.func,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Array of objects, contains props for each `<RadioButton />` component */
|
2020-10-16 13:16:01 +00:00
|
|
|
options: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
value: PropTypes.string.isRequired,
|
2021-04-16 13:04:50 +00:00
|
|
|
label: PropTypes.oneOfType([PropTypes.any, PropTypes.string]),
|
2020-10-16 13:16:01 +00:00
|
|
|
disabled: PropTypes.bool,
|
|
|
|
})
|
|
|
|
).isRequired,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Value of the selected radiobutton */
|
2019-07-25 15:39:25 +00:00
|
|
|
selected: PropTypes.string.isRequired,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Margin between radiobutton. If orientation `horizontal`, it is `margin-left`(apply for all radiobuttons,
|
|
|
|
* except first), if orientation `vertical`, it is `margin-bottom`(apply for all radiobuttons, except last) */
|
2019-12-10 06:28:45 +00:00
|
|
|
spacing: PropTypes.string,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Accepts class */
|
2019-12-03 12:45:13 +00:00
|
|
|
className: PropTypes.string,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Accepts id */
|
2019-12-03 12:45:13 +00:00
|
|
|
id: PropTypes.string,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Accepts css style */
|
2019-12-10 06:28:45 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Position of radiobuttons */
|
2020-10-16 13:16:01 +00:00
|
|
|
orientation: PropTypes.oneOf(["horizontal", "vertical"]),
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Width of RadioButtonGroup container */
|
2019-12-10 06:28:45 +00:00
|
|
|
width: PropTypes.string,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Font size of link */
|
2019-12-27 12:08:23 +00:00
|
|
|
fontSize: PropTypes.string,
|
2021-03-10 07:59:03 +00:00
|
|
|
/** Font weight of link */
|
2019-12-27 12:08:23 +00:00
|
|
|
fontWeight: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
2020-10-16 13:16:01 +00:00
|
|
|
};
|
2019-07-25 14:29:50 +00:00
|
|
|
|
|
|
|
RadioButtonGroup.defaultProps = {
|
2019-07-26 09:42:30 +00:00
|
|
|
isDisabled: false,
|
2019-07-25 15:39:25 +00:00
|
|
|
selected: undefined,
|
2020-10-16 13:16:01 +00:00
|
|
|
spacing: "15px",
|
|
|
|
orientation: "horizontal",
|
|
|
|
width: "100%",
|
|
|
|
};
|
2019-07-25 14:29:50 +00:00
|
|
|
|
|
|
|
export default RadioButtonGroup;
|