Add switch and line

Signed-off-by: Egor Savkin <es@m-labs.hk>
This commit is contained in:
Egor Savkin 2023-08-18 15:51:00 +08:00
parent f93cf142f0
commit ff8a6d54bc
3 changed files with 104 additions and 23 deletions

View File

@ -719,8 +719,6 @@ class ProductCartItem extends React.PureComponent {
</div> </div>
) : null } ) : null }
{warning && model.showWarning && ( {warning && model.showWarning && (
<div className={`k-popup-warning ${shouldTooltipWarningClassInverted ? 'inverted': ''}`}> <div className={`k-popup-warning ${shouldTooltipWarningClassInverted ? 'inverted': ''}`}>
<p className="rule warning"> <p className="rule warning">

View File

@ -11,32 +11,27 @@ class Radio extends Component {
super(props); super(props);
// Initialize the state object with the initial values from the props // Initialize the state object with the initial values from the props
this.state = { this.state = {
target: props.target,
outvar: props.outvar,
variants: props.variants,
variant: props.outvar in props.data ? props.data[props.outvar] : props.variants[0], variant: props.outvar in props.data ? props.data[props.outvar] : props.variants[0],
id: props.id
}; };
// Bind the event handler to this // Bind the event handler to this
this.handleClick = this.handleClick.bind(this); this.handleClick = this.handleClick.bind(this);
this.state.target(this.state.outvar, this.state.variant); this.props.target(this.props.outvar, this.state.variant);
} }
handleClick(variant) { handleClick(variant) {
console.log(variant);
// Update the state object with the new value for outvar // Update the state object with the new value for outvar
this.setState({ this.setState({
...this.state, ...this.state,
variant: variant variant: variant
}); });
this.state.target(this.state.outvar, variant); this.props.target(this.props.outvar, variant);
} }
render() { render() {
let key = this.state.id + this.state.outvar; let key = this.props.id + this.props.outvar;
return ( return (
<div className="shop-radio" key={this.state.id}> <div className="shop-radio" key={this.props.id}>
{this.props.title} {this.props.title}
{this.props.variants.map((variant, _) => ( {this.props.variants.map((variant, _) => (
<div className="form-check" key={key + variant}> <div className="form-check" key={key + variant}>
@ -60,18 +55,105 @@ class Radio extends Component {
} }
function RadioWrapper(target, id, data, {title, variants, outvar}) { function RadioWrapper(target, id, data, {title, variants, outvar}) {
return <Radio target={target} title={title} variants={variants} outvar={outvar} key={id} id={id} data={data} />; return <Radio target={target} title={title} variants={variants} outvar={outvar} key={id} id={id} data={data}/>;
}
class Switch extends Component {
constructor(props) {
super(props);
// Initialize the state object with the initial values from the props
this.state = {
checked: props.outvar in props.data ? props.data[props.outvar] : !!(props.fallback)
};
// Bind the event handler to this
this.handleClick = this.handleClick.bind(this);
this.props.target(this.props.outvar, this.state.checked);
}
handleClick() {
// Update the state object with the new value for outvar
let new_checked = !this.state.checked;
this.setState({
checked: new_checked
});
this.props.target(this.props.outvar, new_checked);
}
render() {
let key = this.props.id + this.props.outvar;
return (
<div className="shop-switch" key={this.props.id}>
<div className="form-check form-switch" key={key}>
<input
className="form-check-input"
type="checkbox"
role="switch"
id={key}
checked={this.state.checked}
onClick={this.handleClick}
onChange={this.handleClick}
/>
<label className="form-check-label" htmlFor={key}>
{this.props.title}
</label>
</div>
</div>
);
}
}
function SwitchWrapper(target, id, data, {title, fallback, outvar}) {
return <Switch target={target} title={title} fallback={fallback} outvar={outvar} key={id} id={id} data={data}/>;
} }
function MissingComponent(type, id) { class Line 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] : (props.fallback ? props.fallback : "")
};
// Bind the event handler to this
this.handleClick = this.handleClick.bind(this);
this.props.target(this.props.outvar, this.state.text);
}
handleClick(element) {
let text = element.target.value;
this.setState({
text: text
});
this.props.target(this.props.outvar, text);
}
render() {
let key = this.props.id + this.props.outvar;
return (
<div className="shop-switch" key={this.props.id}>
<label htmlFor={key} className="form-label">{this.props.title}: </label>
<input type="email" className="form-control" id={key} onChange={this.handleClick} value={this.state.text} />
</div>
);
}
}
function LineWrapper(target, id, data, {title, fallback, outvar}) {
return <Line target={target} title={title} fallback={fallback} outvar={outvar} key={id} id={id} data={data}/>;
}
function UnimplementedComponent(type, id) {
//console.error("Missing component with type:", type) //console.error("Missing component with type:", type)
return <div key={type + id} style={{background: "red"}}>MISSING</div> return <div key={type + id} style={{background: "red"}}>UNIMPLEMENTED</div>
} }
const componentsList = { const componentsList = {
"Radio": RadioWrapper, "Radio": RadioWrapper,
"Default": MissingComponent, "Switch": SwitchWrapper,
"Line": LineWrapper,
"Default": UnimplementedComponent,
}; };

View File

@ -76,21 +76,22 @@ const shop_data = {
] ]
}, },
[ [
{type: "LineIPv4", args: {title: "IPv4", outvar: "ipv4", default: "192.168.1.75/24"}}, {type: "Line", args: {title: "IPv4", outvar: "ipv4", fallback: "192.168.1.75/24"}},
{type: "SwitchLine", args: {title: "IPv6", outvar: "ipv6"}}, {type: "SwitchLine", args: {title: "IPv6", outvar: "ipv6"}},
{type: "SwitchLine", args: {title: "MAC", outvar: "mac"}}, {type: "SwitchLine", args: {title: "MAC", outvar: "mac"}},
{type: "Switch", args: {title: "Ext CLK", outvar: "ext_clk"}}, {type: "Switch", args: {title: "Ext CLK", outvar: "ext_clk"}},
{
"if": [
{"===": [{"var": "ext_clk"}, true]},
{type: "Line", args: {title: "Frequency", outvar: "ext_clk_freq", fallback: "125 MHz"}},
null
]
}
], ],
null null
] ]
}, },
{ null
"if": [
{"===": [{"var": "ext_clk"}, true]},
{type: "line", args: {title: "Frequency", outvar: "ext_clk"}},
null
]
}
], ],
rules: { rules: {
maxSlot: { maxSlot: {