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";
|
2024-03-27 16:14:05 +08:00
|
|
|
import {Validation} from "../validation";
|
2023-11-30 17:26:17 +08:00
|
|
|
|
|
|
|
class SwitchLine extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
// Initialize the state object with the initial values from the props
|
|
|
|
this.state = {
|
|
|
|
text: props.outvar in props.data ? props.data[props.outvar].text : (props.fallback ? props.fallback.text : ""),
|
2024-03-27 16:14:05 +08:00
|
|
|
checked: props.outvar in props.data ? props.data[props.outvar].checked : (props.fallback ? props.fallback.checked : false),
|
|
|
|
valid: true
|
2023-11-30 17:26:17 +08:00
|
|
|
};
|
|
|
|
// Bind the event handler to this
|
|
|
|
this.handleText = this.handleText.bind(this);
|
|
|
|
this.handleCheck = this.handleCheck.bind(this);
|
|
|
|
this.props.target.construct(this.props.outvar, this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleText(element) {
|
|
|
|
let new_state = {
|
|
|
|
...this.state,
|
2024-03-27 16:14:05 +08:00
|
|
|
text: element.target.value,
|
|
|
|
valid: this.props.validator ? this.props.validator(element.target.value) : true
|
2023-11-30 17:26:17 +08:00
|
|
|
}
|
|
|
|
this.setState(new_state);
|
|
|
|
this.props.target.update(this.props.outvar, new_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCheck() {
|
|
|
|
// Update the state object with the new value for outvar
|
|
|
|
let new_state = {
|
|
|
|
...this.state,
|
|
|
|
checked: !this.state.checked
|
|
|
|
}
|
|
|
|
this.setState(new_state);
|
|
|
|
this.props.target.update(this.props.outvar, new_state);
|
|
|
|
}
|
|
|
|
|
2024-01-30 17:20:29 +08:00
|
|
|
static getDerivedStateFromProps(props, current_state) {
|
|
|
|
if (current_state.checked !== props.data[props.outvar].checked || current_state.text !== props.data[props.outvar].text) {
|
|
|
|
return {
|
|
|
|
checked: props.data[props.outvar].checked,
|
|
|
|
text: props.data[props.outvar].text,
|
2024-03-27 16:14:05 +08:00
|
|
|
valid: this.props.validator ? this.props.validator(props.data[props.outvar].text) : true
|
2024-01-30 17:20:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-11-30 17:26:17 +08:00
|
|
|
render() {
|
|
|
|
let key = this.props.id + this.props.outvar;
|
|
|
|
return (
|
|
|
|
<div className="shop-switch-line" key={this.props.id}>
|
|
|
|
<div className="form-check form-switch" key={key}>
|
|
|
|
<input
|
|
|
|
className="form-check-input"
|
|
|
|
type="checkbox"
|
|
|
|
role="switch"
|
|
|
|
id={key + "switch"}
|
|
|
|
checked={this.state.checked}
|
|
|
|
onClick={this.handleCheck}
|
|
|
|
onChange={this.handleCheck}
|
|
|
|
/>
|
|
|
|
<label className="form-check-label" htmlFor={key + "switch"}>
|
|
|
|
{this.props.icon && <img src={`/images${this.props.icon}`} className="options-icon"/>}
|
|
|
|
{this.props.title}
|
|
|
|
</label>
|
|
|
|
{this.props.tip && <Tip id={this.props.id + "tooltip"} tip={this.props.tip}/>}
|
|
|
|
</div>
|
2024-03-27 16:14:05 +08:00
|
|
|
<input type="text" className={`form-control form-control-sm ${this.state.valid ? "" : "options-invalid"}`} id={key + "line"} onChange={this.handleText}
|
2023-11-30 17:26:17 +08:00
|
|
|
value={this.state.text} disabled={!this.state.checked}/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 16:14:05 +08:00
|
|
|
export function SwitchLineWrapper(target, id, data, {title, fallback, outvar, icon, tip, classes, validator}) {
|
2023-11-30 17:26:17 +08:00
|
|
|
return <SwitchLine target={target} title={title} fallback={fallback} outvar={outvar} icon={icon} tip={tip} key={id}
|
2024-03-27 16:14:05 +08:00
|
|
|
id={id} data={data} classes={classes} validator={validator && Validation[validator.name](validator.params)}/>;
|
2023-11-30 17:26:17 +08:00
|
|
|
}
|