From 1578cbeda4679bc85cdb727bdd5568ba0d127649 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sat, 1 Apr 2023 19:29:17 -0500 Subject: [PATCH 1/3] prevent default redirect on web forms --- packages/web/src/dom.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/web/src/dom.rs b/packages/web/src/dom.rs index 4d17749d8..e9e801588 100644 --- a/packages/web/src/dom.rs +++ b/packages/web/src/dom.rs @@ -54,6 +54,10 @@ impl WebsysDom { let element = walk_event_for_id(event); let bubbles = dioxus_html::event_bubbles(name.as_str()); if let Some((element, target)) = element { + // Prevent forms from submitting and redirecting + if name == "submit" { + event.prevent_default(); + } if target .get_attribute("dioxus-prevent-default") .as_deref() From bf2d561be4300f5ba9a60919d4c93070db337a35 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 4 Apr 2023 09:32:09 -0500 Subject: [PATCH 2/3] invert prevent default on web forms --- packages/web/src/dom.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/web/src/dom.rs b/packages/web/src/dom.rs index e9e801588..35ccabe33 100644 --- a/packages/web/src/dom.rs +++ b/packages/web/src/dom.rs @@ -54,16 +54,17 @@ impl WebsysDom { let element = walk_event_for_id(event); let bubbles = dioxus_html::event_bubbles(name.as_str()); if let Some((element, target)) = element { + let prevent_default_attribute = target.get_attribute("dioxus-prevent-default"); + let prevented_event = prevent_default_attribute + .as_deref() + .map(|f| f.trim_start_matches("on")); // Prevent forms from submitting and redirecting if name == "submit" { - event.prevent_default(); - } - if target - .get_attribute("dioxus-prevent-default") - .as_deref() - .map(|f| f.trim_start_matches("on")) - == Some(&name) - { + // On forms the default behavior is not to submit, if prevent default is set then we submit the form + if prevented_event != Some("submit") { + event.prevent_default(); + } + } else if prevented_event == Some(&name) { event.prevent_default(); } From 14867931a45654773cdbd2329c6eede10876e988 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 20 Jul 2023 13:51:26 -0700 Subject: [PATCH 3/3] fix clippy --- packages/web/src/dom.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/web/src/dom.rs b/packages/web/src/dom.rs index c492c6d42..754bb1605 100644 --- a/packages/web/src/dom.rs +++ b/packages/web/src/dom.rs @@ -75,10 +75,8 @@ impl WebsysDom { if !prevent_event { event.prevent_default(); } - } else { - if prevent_event { - event.prevent_default(); - } + } else if prevent_event { + event.prevent_default(); } let data = virtual_event_from_websys_event(event.clone(), target);