2019-01-12 14:09:17 +01:00
|
|
|
import React from "react";
|
|
|
|
|
import { optionchangewrapper } from "utils/optionchangewrapper";
|
|
|
|
|
|
|
|
|
|
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}>
|
|
|
|
|
<input
|
|
|
|
|
name={type === "radio" ? `${componentName}-radio` : ""}
|
|
|
|
|
type={type}
|
|
|
|
|
value={value}
|
|
|
|
|
checked={this.isChecked(type, value, optionName)}
|
|
|
|
|
onChange={option => this.optionChange(option, optionName, type)}
|
|
|
|
|
/>
|
|
|
|
|
{name}
|
|
|
|
|
</label>
|
|
|
|
|
));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { elements, componentName } = this.props;
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<span>{componentName}</span>
|
|
|
|
|
{this.renderElements(elements, componentName)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default optionchangewrapper(CheckboxAndRadioWrapper);
|