2023-08-17 14:42:47 +08:00
|
|
|
'use strict';
|
|
|
|
|
2023-08-17 16:41:50 +08:00
|
|
|
import React, {Component} from "react";
|
2023-08-17 14:42:47 +08:00
|
|
|
import jsonLogic from 'json-logic-js';
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/70511311
|
|
|
|
const trueTypeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
|
|
|
|
|
2023-08-17 16:41:50 +08:00
|
|
|
class Radio extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
// Initialize the state object with the initial values from the props
|
|
|
|
this.state = {
|
|
|
|
target: props.target,
|
|
|
|
outvar: props.outvar,
|
|
|
|
variants: props.variants,
|
2023-08-18 12:34:30 +08:00
|
|
|
variant: props.outvar in props.data ? props.data[props.outvar] : props.variants[0],
|
2023-08-17 16:41:50 +08:00
|
|
|
id: props.id
|
|
|
|
};
|
|
|
|
|
|
|
|
// Bind the event handler to this
|
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.state.target(this.state.outvar, this.state.variant);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(variant) {
|
|
|
|
console.log(variant);
|
|
|
|
// Update the state object with the new value for outvar
|
|
|
|
this.setState({
|
|
|
|
...this.state,
|
|
|
|
variant: variant
|
|
|
|
});
|
|
|
|
this.state.target(this.state.outvar, variant);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let key = this.state.id + this.state.outvar;
|
|
|
|
return (
|
|
|
|
<div className="shop-radio" key={this.state.id}>
|
|
|
|
{this.props.title}
|
|
|
|
{this.props.variants.map((variant, _) => (
|
|
|
|
<div className="form-check" key={key + variant}>
|
|
|
|
<input
|
|
|
|
className="form-check-input"
|
|
|
|
type="radio"
|
|
|
|
name={key}
|
|
|
|
id={key + variant}
|
|
|
|
checked={this.state.variant === variant}
|
|
|
|
onClick={() => this.handleClick(variant)}
|
|
|
|
onChange={() => this.handleClick(variant)}
|
|
|
|
/>
|
|
|
|
<label className="form-check-label" htmlFor={key + variant}>
|
|
|
|
{variant}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
2023-08-17 14:42:47 +08:00
|
|
|
}
|
2023-08-17 16:41:50 +08:00
|
|
|
}
|
2023-08-17 14:42:47 +08:00
|
|
|
|
2023-08-18 12:34:30 +08:00
|
|
|
function RadioWrapper(target, id, data, {title, variants, outvar}) {
|
|
|
|
return <Radio target={target} title={title} variants={variants} outvar={outvar} key={id} id={id} data={data} />;
|
2023-08-17 14:42:47 +08:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:41:50 +08:00
|
|
|
|
|
|
|
function MissingComponent(type, id) {
|
|
|
|
//console.error("Missing component with type:", type)
|
|
|
|
return <div key={type + id} style={{background: "red"}}>MISSING</div>
|
2023-08-17 14:42:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const componentsList = {
|
2023-08-17 16:41:50 +08:00
|
|
|
"Radio": RadioWrapper,
|
2023-08-17 14:42:47 +08:00
|
|
|
"Default": MissingComponent,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-17 16:41:50 +08:00
|
|
|
export default function ProcessOptions({options, data, target, id}) {
|
2023-08-17 14:42:47 +08:00
|
|
|
let options_t = trueTypeOf(options);
|
|
|
|
|
|
|
|
if (options_t === "array") {
|
|
|
|
return Array.from(
|
2023-08-17 16:41:50 +08:00
|
|
|
options.map((option_item, i) => ProcessOptions({
|
|
|
|
options: option_item,
|
|
|
|
data: data,
|
|
|
|
target: target,
|
|
|
|
id: id + i
|
|
|
|
}))
|
2023-08-17 14:42:47 +08:00
|
|
|
);
|
|
|
|
} else if (options_t === "object") {
|
|
|
|
if (
|
|
|
|
trueTypeOf(options.type) === "string" &&
|
|
|
|
trueTypeOf(options.args) === "object"
|
|
|
|
) {
|
|
|
|
if (options.type in componentsList) {
|
2023-08-18 12:34:30 +08:00
|
|
|
return componentsList[options.type](target, id + options.type, data, options.args);
|
2023-08-17 16:41:50 +08:00
|
|
|
} else {
|
|
|
|
return componentsList["Default"](options.type, id + "missing");
|
2023-08-17 14:42:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-17 16:41:50 +08:00
|
|
|
return ProcessOptions({options: jsonLogic.apply(options, data), data: data, target: target, id: id});
|
2023-08-17 14:42:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|