2023-11-30 17:26:17 +08:00
|
|
|
import React, {Component} from "react";
|
2023-12-12 18:21:09 +08:00
|
|
|
import {Tip} from "./Tip";
|
2023-11-30 17:26:17 +08:00
|
|
|
|
|
|
|
class Radio extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
// Initialize the state object with the initial values from the props
|
|
|
|
this.state = {
|
|
|
|
variant: props.outvar in props.data ? props.data[props.outvar] : props.variants[props.fallback ? props.fallback : 0],
|
|
|
|
};
|
|
|
|
|
|
|
|
// Bind the event handler to this
|
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.props.target.construct(this.props.outvar, this.state.variant);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(variant) {
|
|
|
|
// Update the state object with the new value for outvar
|
|
|
|
this.setState({
|
|
|
|
...this.state,
|
|
|
|
variant: variant
|
|
|
|
});
|
|
|
|
this.props.target.update(this.props.outvar, variant);
|
|
|
|
}
|
|
|
|
|
2024-01-30 17:20:29 +08:00
|
|
|
static getDerivedStateFromProps(props, current_state) {
|
|
|
|
if (current_state.variant !== props.data[props.outvar]) {
|
|
|
|
return {
|
|
|
|
variant: props.data[props.outvar]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-11-30 17:26:17 +08:00
|
|
|
render() {
|
|
|
|
let key = this.props.id + this.props.outvar;
|
|
|
|
return (
|
|
|
|
<div className="shop-radio" key={this.props.id}>
|
2024-01-31 11:40:33 +08:00
|
|
|
<div style={{"display": "inline"}} className="shop-radio-label">
|
2023-11-30 17:26:17 +08:00
|
|
|
{this.props.icon && <img src={`/images${this.props.icon}`} className="options-icon"/>}
|
|
|
|
{this.props.title}
|
|
|
|
</div>
|
|
|
|
{this.props.tip && <Tip id={this.props.id + "tooltip"} tip={this.props.tip}/>}
|
|
|
|
{this.props.variants.map((variant, _) => (
|
2024-02-02 17:24:01 +08:00
|
|
|
<div className={`form-check shop-radio-variant ${this.props.classes}`} key={key + variant}>
|
2023-11-30 17:26:17 +08:00
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 17:24:01 +08:00
|
|
|
export function RadioWrapper(target, id, data, {title, variants, outvar, fallback, icon, tip, classes}) {
|
2023-11-30 17:26:17 +08:00
|
|
|
return <Radio target={target} title={title} variants={variants} outvar={outvar} icon={icon} tip={tip} key={id}
|
2024-02-02 17:24:01 +08:00
|
|
|
fallback={fallback} classes={classes}
|
2023-11-30 17:26:17 +08:00
|
|
|
id={id} data={data}/>;
|
|
|
|
}
|