From 2d2de36eed3d909028d4910c2b974020268a0ad5 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sun, 13 Mar 2022 18:53:20 -0400 Subject: [PATCH] fix: filtering for buttons --- examples/form.rs | 20 +++++++++++--------- packages/interpreter/src/interpreter.js | 14 +++++++++++--- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/examples/form.rs b/examples/form.rs index 5f0c992f5..3637826da 100644 --- a/examples/form.rs +++ b/examples/form.rs @@ -11,15 +11,17 @@ fn main() { fn app(cx: Scope) -> Element { cx.render(rsx! { - div { - h1 { "Form" } - form { - onsubmit: move |ev| println!("Submitted {:?}", ev.values), - oninput: move |ev| println!("Input {:?}", ev.values), - input { r#type: "text", name: "username" } - input { r#type: "text", name: "full-name" } - input { r#type: "password", name: "password" } - button { "Submit the form" } + Router { + div { + h1 { "Form" } + form { + onsubmit: move |ev| println!("Submitted {:?}", ev.values), + oninput: move |ev| println!("Input {:?}", ev.values), + input { r#type: "text", name: "username" } + input { r#type: "text", name: "full-name" } + input { r#type: "password", name: "password" } + button { r#type: "submit", value: "Submit", "Submit the form" } + } } } }) diff --git a/packages/interpreter/src/interpreter.js b/packages/interpreter/src/interpreter.js index f6b4fe200..86a39c2a1 100644 --- a/packages/interpreter/src/interpreter.js +++ b/packages/interpreter/src/interpreter.js @@ -77,7 +77,7 @@ export class Interpreter { const element = this.nodes[root]; element.setAttribute("data-dioxus-id", `${root}`); if (this.listeners[event_name] === undefined) { - this.listeners[event_name] = 0; + this.listeners[event_name] = 1; this.handlers[event_name] = handler; this.root.addEventListener(event_name, handler); } else { @@ -190,9 +190,13 @@ export class Interpreter { this.RemoveEventListener(edit.root, edit.event_name); break; case "NewEventListener": + console.log(this.listeners); + // this handler is only provided on desktop implementations since this // method is not used by the web implementation let handler = (event) => { + console.log(event); + let target = event.target; if (target != null) { let realId = target.getAttribute(`data-dioxus-id`); @@ -215,7 +219,7 @@ export class Interpreter { } // also prevent buttons from submitting - if (target.tagName === "BUTTON") { + if (target.tagName === "BUTTON" && event.type == "submit") { event.preventDefault(); } } @@ -239,11 +243,15 @@ export class Interpreter { if (shouldPreventDefault === `on${event.type}`) { event.preventDefault(); } + if (event.type === "submit") { event.preventDefault(); } - if (target.tagName === "FORM") { + if ( + target.tagName === "FORM" && + (event.type === "submit" || event.type === "input") + ) { for (let x = 0; x < target.elements.length; x++) { let element = target.elements[x]; let name = element.getAttribute("name");