Only sync regulator inputs on change
This lets us infer a regulator's role from whether it has a set point and what text specifies the set point.
This commit is contained in:
parent
b3e4e902f3
commit
fef4127f69
3 changed files with 30 additions and 57 deletions
|
|
@ -1,8 +1,6 @@
|
|||
use itertools::Itertools;
|
||||
use sycamore::prelude::*;
|
||||
use web_sys::{
|
||||
Event,
|
||||
HtmlInputElement,
|
||||
KeyboardEvent,
|
||||
MouseEvent,
|
||||
wasm_bindgen::JsCast
|
||||
|
|
@ -14,7 +12,6 @@ use crate::{
|
|||
assembly::{
|
||||
Regulator,
|
||||
RegulatorKey,
|
||||
RegulatorRole::*,
|
||||
ElementKey
|
||||
}
|
||||
};
|
||||
|
|
@ -22,25 +19,17 @@ use crate::{
|
|||
// an editable view of a regulator
|
||||
#[component(inline_props)]
|
||||
fn RegulatorInput(regulator: Regulator) -> View {
|
||||
let value = create_signal(regulator.set_point_spec.get_clone_untracked());
|
||||
create_effect(move || value.set(regulator.set_point_spec.get_clone()));
|
||||
view! {
|
||||
input(
|
||||
r#type="text",
|
||||
placeholder=regulator.measurement.with(|result| result.to_string()),
|
||||
bind:value=regulator.set_point_text,
|
||||
on:change=move |event: Event| {
|
||||
let target: HtmlInputElement = event.target().unwrap().unchecked_into();
|
||||
let value = target.value();
|
||||
if value.is_empty() {
|
||||
regulator.role.set(Measurement);
|
||||
} else {
|
||||
match target.value().parse::<f64>() {
|
||||
Ok(set_pt) => batch(|| {
|
||||
regulator.set_point.set(set_pt);
|
||||
regulator.role.set(Constraint(true));
|
||||
}),
|
||||
Err(_) => regulator.role.set(Constraint(false))
|
||||
};
|
||||
}
|
||||
bind:value=value,
|
||||
on:change=move |_| {
|
||||
let value_val = value.get_clone_untracked();
|
||||
regulator.set_point.set(value_val.parse::<f64>().ok());
|
||||
regulator.set_point_spec.set(value_val);
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
@ -51,20 +40,20 @@ fn RegulatorInput(regulator: Regulator) -> View {
|
|||
fn RegulatorOutlineItem(regulator_key: RegulatorKey, element_key: ElementKey) -> View {
|
||||
let state = use_context::<AppState>();
|
||||
let assembly = &state.assembly;
|
||||
let regulator = assembly.regulators.with(|regs| regs[regulator_key].clone());
|
||||
let regulator = assembly.regulators.with(|regs| regs[regulator_key]);
|
||||
let other_subject = if regulator.subjects.0 == element_key {
|
||||
regulator.subjects.1
|
||||
} else {
|
||||
regulator.subjects.0
|
||||
};
|
||||
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
|
||||
let class = regulator.role.map(
|
||||
|role| match role {
|
||||
Measurement => "regulator",
|
||||
Constraint(true) => "regulator valid-constraint",
|
||||
Constraint(false) => "regulator invalid-constraint"
|
||||
let class = create_memo(move || {
|
||||
match regulator.set_point.get() {
|
||||
None if regulator.has_no_set_point_spec() => "regulator",
|
||||
None => "regulator invalid-constraint",
|
||||
Some(_) => "regulator valid-constraint"
|
||||
}
|
||||
);
|
||||
});
|
||||
view! {
|
||||
li(class=class.get()) {
|
||||
div(class="regulator-label") { (other_subject_label) }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue