add impl fn for FormData to get input types
This commit is contained in:
1 parent
1a9d1eda19
commit
2cbbdc23c6
7 files changed
+38
-2
No files matched your search
@@ -20,6 +20,8 @@ pub struct FormData {
|
||||
|
||||
pub values: HashMap<String, Vec<String>>,
|
||||
|
||||
pub value_types: HashMap<String, String>,
|
||||
|
||||
#[cfg_attr(
|
||||
feature = "serialize",
|
||||
serde(
|
||||
@@ -53,6 +55,17 @@ impl FormData {
|
||||
let mut parsed_values: HashMap<String, ValueType> = HashMap::new();
|
||||
|
||||
for (fieldname, values) in raw_values.into_iter() {
|
||||
// check if the fieldname can hold multiple values based on its types
|
||||
let field_type = self
|
||||
.value_types
|
||||
.get(&fieldname)
|
||||
.expect("Provided invalid field");
|
||||
|
||||
let is_multi_valued_input = match field_type.as_str() {
|
||||
"select" | "checkbox" => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
/*
|
||||
case 1 - multiple values, example { driving_types: ["manual", "automatic"] }
|
||||
In this case we want to return the values as it is, NO point in making
|
||||
@@ -64,7 +77,7 @@ impl FormData {
|
||||
*/
|
||||
parsed_values.insert(
|
||||
fieldname,
|
||||
if values.len() > 1 {
|
||||
if is_multi_valued_input {
|
||||
// handling multiple values - case 1
|
||||
ValueType::VecText(values)
|
||||
} else {
|
||||
|
||||
@@ -63,6 +63,8 @@ pub struct FormData {
|
||||
|
||||
pub values: HashMap<String, Vec<String>>,
|
||||
|
||||
pub value_types: HashMap<String, String>,
|
||||
|
||||
pub files: Option<Files>,
|
||||
}
|
||||
|
||||
@@ -71,6 +73,7 @@ impl FormData {
|
||||
dioxus_html::FormData {
|
||||
value: self.value,
|
||||
values: self.values,
|
||||
value_types: self.value_types,
|
||||
files: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +93,7 @@ impl Button {
|
||||
let data = FormData {
|
||||
value: self.value.to_string(),
|
||||
values: HashMap::new(),
|
||||
value_types: HashMap::new(),
|
||||
files: None,
|
||||
};
|
||||
ctx.send(crate::Event {
|
||||
|
||||
@@ -129,6 +129,7 @@ impl CheckBox {
|
||||
.then(|| self.value.to_string())
|
||||
.unwrap_or_default(),
|
||||
values: HashMap::new(),
|
||||
value_types: HashMap::new(),
|
||||
files: None,
|
||||
};
|
||||
{
|
||||
|
||||
@@ -198,6 +198,7 @@ impl Slider {
|
||||
let data = FormData {
|
||||
value: self.value.to_string(),
|
||||
values: HashMap::new(),
|
||||
value_types: HashMap::new(),
|
||||
files: None,
|
||||
};
|
||||
ctx.send(Event {
|
||||
|
||||
@@ -171,6 +171,7 @@ impl<C: TextLikeController> TextLike<C> {
|
||||
let data: FormData = FormData {
|
||||
value: self.text.clone(),
|
||||
values: HashMap::new(),
|
||||
value_types: HashMap::new(),
|
||||
files: None,
|
||||
};
|
||||
let ctx: UniqueView<WidgetContext> = world.borrow().expect("expected widget context");
|
||||
|
||||
+17
-1
@@ -15,7 +15,7 @@ use dioxus_interpreter_js::{get_node, minimal_bindings, save_template, Channel};
|
||||
use futures_channel::mpsc;
|
||||
use js_sys::Array;
|
||||
use rustc_hash::FxHashMap;
|
||||
use std::{any::Any, rc::Rc};
|
||||
use std::{any::Any, collections::HashMap, rc::Rc};
|
||||
use wasm_bindgen::{closure::Closure, prelude::wasm_bindgen, JsCast, JsValue};
|
||||
use web_sys::{Document, Element, Event};
|
||||
|
||||
@@ -365,6 +365,21 @@ fn read_input_to_data(target: Element) -> Rc<FormData> {
|
||||
})
|
||||
.expect("only an InputElement or TextAreaElement or an element with contenteditable=true can have an oninput event listener");
|
||||
|
||||
let mut value_types = HashMap::new();
|
||||
|
||||
// to get the input_type for the corresponding input
|
||||
for input_el in target.dyn_ref::<web_sys::HtmlFormElement>().into_iter() {
|
||||
for index in 0..input_el.length() {
|
||||
if let Some(element) = input_el.get_with_index(index as u32) {
|
||||
if let Some(input) = element.dyn_into::<web_sys::HtmlInputElement>().ok() {
|
||||
let name = input.name();
|
||||
let input_type = input.type_();
|
||||
value_types.insert(name, input_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut values = std::collections::HashMap::new();
|
||||
|
||||
// try to fill in form values
|
||||
@@ -400,6 +415,7 @@ fn read_input_to_data(target: Element) -> Rc<FormData> {
|
||||
Rc::new(FormData {
|
||||
value,
|
||||
values,
|
||||
value_types,
|
||||
files,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user