Skip to content

Commit

Permalink
Merge pull request #320 from naturalethic/form-input-event-radio-value
Browse files Browse the repository at this point in the history
Capture correct radio button value in a form input event
  • Loading branch information
jkelleyrtp committed Mar 21, 2022
2 parents d3ac3db + ae2e8a8 commit 41b7de3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions examples/form.rs
Expand Up @@ -20,6 +20,8 @@ fn app(cx: Scope) -> Element {
input { r#type: "text", name: "username" }
input { r#type: "text", name: "full-name" }
input { r#type: "password", name: "password" }
input { r#type: "radio", name: "color", value: "red" }
input { r#type: "radio", name: "color", value: "blue" }
button { r#type: "submit", value: "Submit", "Submit the form" }
}
}
Expand Down
26 changes: 17 additions & 9 deletions packages/web/src/dom.rs
Expand Up @@ -241,25 +241,33 @@ fn virtual_event_from_websys_event(
for x in 0..elements.length() {
let element = elements.item(x).unwrap();
if let Some(name) = element.get_attribute("name") {
let value: String = (&element)
let value: Option<String> = (&element)
.dyn_ref()
.map(|input: &web_sys::HtmlInputElement| {
log::info!("Input type: {}", input.type_());
match input.type_().as_str() {
"checkbox" => {
match input.checked() {
true => "true".to_string(),
false => "false".to_string(),
true => Some("true".to_string()),
false => Some("false".to_string()),
}
},
_ => input.value()
"radio" => {
match input.checked() {
true => Some(input.value()),
false => None,
}
}
_ => Some(input.value())
}
})
.or_else(|| target.dyn_ref().map(|input: &web_sys::HtmlTextAreaElement| input.value()))
.or_else(|| target.dyn_ref().map(|input: &web_sys::HtmlSelectElement| input.value()))
.or_else(|| target.dyn_ref::<web_sys::HtmlElement>().unwrap().text_content())
.or_else(|| target.dyn_ref().map(|input: &web_sys::HtmlTextAreaElement| Some(input.value())))
.or_else(|| target.dyn_ref().map(|input: &web_sys::HtmlSelectElement| Some(input.value())))
.or_else(|| Some(target.dyn_ref::<web_sys::HtmlElement>().unwrap().text_content()))
.expect("only an InputElement or TextAreaElement or an element with contenteditable=true can have an oninput event listener");

values.insert(name, value);
if let Some(value) = value {
values.insert(name, value);
}
}
}
}
Expand Down

0 comments on commit 41b7de3

Please sign in to comment.