2019-01-12 14:09:17 +01:00
|
|
|
import React from "react";
|
2019-03-14 05:41:06 +01:00
|
|
|
import { optionchangewrapper } from "../../utils/optionchangewrapper";
|
2019-01-29 20:30:43 +01:00
|
|
|
import Checkbox from "@material-ui/core/Checkbox";
|
|
|
|
|
import Radio from "@material-ui/core/Radio";
|
2019-03-14 05:41:06 +01:00
|
|
|
import "../../assets/checkboxAndRadioStyle.css";
|
2019-01-12 14:09:17 +01:00
|
|
|
|
|
|
|
|
class CheckboxAndRadioWrapper extends React.Component {
|
|
|
|
|
optionChange = (option, optionName, type) => {
|
|
|
|
|
const optionTypePicker = {
|
|
|
|
|
radio: option.target.value,
|
2019-01-14 22:41:53 +01:00
|
|
|
checkbox: option.target.checked ? option.target.value : false
|
2019-01-12 14:09:17 +01:00
|
|
|
};
|
|
|
|
|
const { onOptionChanged } = this.props;
|
|
|
|
|
onOptionChanged({
|
|
|
|
|
optionName,
|
|
|
|
|
optionValue: optionTypePicker[type]
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
isChecked = (type, value, optionName) => {
|
|
|
|
|
const { options } = this.props;
|
2019-01-14 22:41:53 +01:00
|
|
|
return options.hasOwnProperty(optionName) && type === "checkbox"
|
|
|
|
|
? options[optionName]
|
|
|
|
|
: options[optionName] === String(value);
|
2019-01-12 14:09:17 +01:00
|
|
|
};
|
|
|
|
|
renderElements = (elements, componentName) => {
|
|
|
|
|
return elements.map(({ type, value, name, optionName } = {}) => (
|
|
|
|
|
<label key={name + type}>
|
2019-01-29 20:30:43 +01:00
|
|
|
{type === "radio" ? (
|
|
|
|
|
<Radio
|
|
|
|
|
className="radio-style"
|
|
|
|
|
name={type === "radio" ? `${componentName}-radio` : ""}
|
|
|
|
|
type={type}
|
|
|
|
|
value={value}
|
|
|
|
|
checked={this.isChecked(type, value, optionName)}
|
|
|
|
|
onChange={option => this.optionChange(option, optionName, type)}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<Checkbox
|
|
|
|
|
className="checkbox-style"
|
|
|
|
|
type={type}
|
|
|
|
|
value={value}
|
|
|
|
|
checked={this.isChecked(type, value, optionName)}
|
|
|
|
|
onChange={option => this.optionChange(option, optionName, type)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<span className="label-style">{name}</span>
|
|
|
|
|
<br />
|
2019-01-12 14:09:17 +01:00
|
|
|
</label>
|
|
|
|
|
));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { elements, componentName } = this.props;
|
2019-01-29 20:30:43 +01:00
|
|
|
return <div>{this.renderElements(elements, componentName)}</div>;
|
2019-01-12 14:09:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default optionchangewrapper(CheckboxAndRadioWrapper);
|