import React, {Component} from "react"; import {Tip} from "./Tip"; 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); } static getDerivedStateFromProps(props, current_state) { if (current_state.variant !== props.data[props.outvar]) { return { variant: props.data[props.outvar] } } return null } render() { let key = this.props.id + this.props.outvar; return (
{this.props.icon && } {this.props.title}
{this.props.tip && } {this.props.variants.map((variant, _) => (
this.handleClick(variant)} onChange={() => this.handleClick(variant)} />
))}
); } } export function RadioWrapper(target, id, data, {title, variants, outvar, fallback, icon, tip}) { return ; }