Commit 313dce37 by dongshufeng

refactor(iesplan): radio is not ok

parent 33cb78ef
5,car1,,
1000001,源电压,Checkbox,
1000002,A相有功634,Radio,
1000002,A相有功634,Radio,name1:1.0;name2:3;name3:4
1000003,A相无功634,TextField,
1000004,B相有功634,Switch,
1000005,B相无功634,Slider,-100;100;10
......
......@@ -46,9 +46,9 @@ impl QueryWithId {
pub enum ParaType {
// show expression, true expression, false expression
Checkbox,
Radio,
Switch,
Select(Vec<f64>),
Radio(Vec<(String, f64)>),
Select(Vec<(String, f64)>),
// min, max, step
Slider(f64, f64, f64),
TextField,
......@@ -81,7 +81,6 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
let para_type_s = csv_str(&row, 2).unwrap().to_uppercase();
let para_type = match para_type_s.as_str() {
"CHECKBOX" => ParaType::Checkbox,
"RADIO" => ParaType::Radio,
"SWITCH" => ParaType::Switch,
"TEXTFIELD" => ParaType::TextField,
"SLIDER" => {
......@@ -94,9 +93,13 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
},
"SELECT" => {
let v = csv_str(&row, 3).unwrap();
let floats = v.split(";")
.map(|s| s.parse::<f64>().unwrap()).collect();
ParaType::Select(floats)
let options = parse_options(v);
ParaType::Select(options)
}
"RADIO" => {
let v = csv_str(&row, 3).unwrap();
let options = parse_options(v);
ParaType::Radio(options)
}
_ => ParaType::TextField
};
......@@ -105,6 +108,19 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
Parameters { id, name, labels, points, para_types }
}
fn parse_options(v: &str) -> Vec<(String, f64)> {
let options = v.split(";")
.map(|s| {
let options: Vec<&str> = s.split(":").collect();
if options.len() == 2 {
(options[0].to_string(), options[1].parse::<f64>().unwrap())
} else {
("".to_string(), s.parse::<f64>().unwrap())
}
}).collect();
options
}
pub fn build_tiles(xlsx_bytes: Vec<u8>) -> Option<Tiles> {
let (m, n, merge_map, values) = get_first_sheet_merged_cells(xlsx_bytes)?;
let mut class_str = Vec::new();
......
......@@ -40,8 +40,7 @@ impl Component for ParaCard {
for index in 0..ctx.props().paras.points.len() {
let input_type = &ctx.props().paras.para_types[index];
if ParaType::Checkbox.eq(input_type)
|| ParaType::Switch.eq(input_type)
|| ParaType::Radio.eq(input_type) {
|| ParaType::Switch.eq(input_type) {
bools.insert(index, false);
} else {
floats.insert(index, 0.0);
......@@ -85,6 +84,7 @@ impl Component for ParaCard {
self.do_set_point(ctx, &value, point_id);
}
Msg::SetOption(i, value) => {
log::warn!("========================= {i} {value}");
if value == "None" {
return false;
}
......@@ -141,17 +141,6 @@ impl ParaCard {
</Field>
}
}
ParaType::Radio => {
let checked = self.bools.get(&i).cloned().unwrap_or(false);
html! {
<Field horizontal={true} label={label}>
<Radio update={link.callback(move |_| Msg::SetBool(i, !checked))}
checked_value={"selected"}
value={if checked {"selected"} else {"none"}}>
</Radio>
</Field>
}
}
ParaType::Switch => {
let checked = self.bools.get(&i).cloned().unwrap_or(false);
html! {
......@@ -179,22 +168,56 @@ impl ParaCard {
}
}
ParaType::Select(options) => {
let current_v = self.floats.get(&i).cloned().unwrap_or(0.0).to_string();
let current_f = self.floats.get(&i).cloned().unwrap_or(0.0);
let content = (0..options.len()).map(|i| {
let (name, f) = &options[i];
let to_show = if name.is_empty() {
f.to_string()
} else {
name.clone()
};
html! {
<option value={f.to_string()} selected={current_f == *f}>
{to_show}
</option>
}
}).collect::<Html>();
html! {
<Field horizontal={true} label={label}>
<Select update={link.callback(move |s| Msg::SetOption(i, s))} >
{for options.iter().map(|f| {
html! {
<option value={f.to_string()} selected={current_v == f.to_string()}>
{f.to_string()}
</option>
}
})}
{content}
<option value={"None"}>{"no_selection"}</option>
</Select>
</Field>
}
}
ParaType::Radio(options) => {
let current_f = self.floats.get(&i).cloned().unwrap_or(0.0);
let content = (0..options.len()).map(|i| {
let (name, f) = &options[i];
let to_show = if name.is_empty() {
f.to_string()
} else {
name.clone()
};
let checked_value = if current_f == *f {
f.to_string()
} else {
current_f.to_string()
};
html! {
<Radio update={link.callback(move |s| Msg::SetOption(i, s))}
checked_value={checked_value} value={f.to_string()}>
<span>{to_show}</span>
</Radio>
}
}).collect::<Html>();
html! {
<Field horizontal={true} label={label}>
<div class="radios">{content}</div>
</Field>
}
}
ParaType::TextField => {
let name = format!("tf_{}", point_id);
let f = self.floats.get(&i).cloned().unwrap_or(0.0).to_string();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论