Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent default redirect on web forms #926

Merged
merged 4 commits into from
Jul 20, 2023

Conversation

ealmloff
Copy link
Member

@ealmloff ealmloff commented Apr 2, 2023

Prevent the default redirect behavior of forms in the web renderer to match the desktop renderer, and documentation (the guide and examples)

Fixes #924

@ealmloff ealmloff added bug Something isn't working web relating to the web renderer for dioxus labels Apr 2, 2023
@kylerlaird
Copy link

Does this allow the option of standard form submission or not (by returning false in JavaScript)?
https://www.tutorialspoint.com/How-to-stop-form-submission-using-JavaScript

@ealmloff
Copy link
Member Author

ealmloff commented Apr 4, 2023

Does this allow the option of standard form submission or not (by returning false in JavaScript)? https://www.tutorialspoint.com/How-to-stop-form-submission-using-JavaScript

Adding prevent default will now make the form submit. In the original example, if you wanted the form to redirect you could use:

use dioxus::prelude::*;

fn main() {
    dioxus_web::launch(app);
}

fn app(cx: Scope) -> Element {
    cx.render(rsx!(
        body {
            form {
                prevent_default: "onsubmit",
                onsubmit: move |event| {
                    println!("Submitted! {event:?}")
                },
                input { name: "name", },
                input { name: "age", },
                input { name: "date", },
                input { r#type: "submit", },
            }

        }
    ))
}

@kylerlaird
Copy link

kylerlaird commented Apr 4, 2023

Thank you for the update. I'm not much of a JavaScript or Rust programmer, so I'll appreciate remedial explanations.

Adding "prevent_default" will now make the form submit (which is the default behavior)? That seems backward to me.

Is the onsubmit handler able to specify whether or not the form submits normally after the handler completes (such as returning false in JavaScript)?

@ealmloff
Copy link
Member Author

ealmloff commented Apr 4, 2023

Adding "prevent_default" will now make the form submit (which is the default behavior)? That seems backward to me.

I agree making the default different from JavaScript could be unexpected, but I don't see a different solution while maintaining consistency between different platforms.

Dioxus supports two main renderers right now: Web and Desktop. For the web renderer, making redirecting the default behavior might make sense, but for the desktop renderer redirecting out of the application into the browser does not make sense. In other renderers, like the Blitz and TUI renderers, there is no concept of redirecting at all so the default form behavior makes even less sense.

Is the onsubmit handler able to specify whether or not the form submits normally after the handler completes (such as returning false in JavaScript)?

There is currently no way to conditionally prevent default on events. If we added this feature it would still leave issues with the default form behavior on web breaking the desktop renderer.

I'm open to other ideas, but I think a solution that is cross platform by default should be preferred.

@jkelleyrtp
Copy link
Member

I'd rather not override default behavior for web. I think it makes sense to do it for desktop/mobile since the experience would be completely jarring, but for web we want to match whatever the standard is. What would make more sense is to automatically prevent default if the event handler is present since... why would you want to handle the event if the page is redirected?

@ealmloff ealmloff force-pushed the prevent-default-form-web branch 2 times, most recently from 3720baa to bf2d561 Compare July 20, 2023 20:41
@jkelleyrtp jkelleyrtp enabled auto-merge July 20, 2023 23:07
@jkelleyrtp jkelleyrtp merged commit af25410 into DioxusLabs:master Jul 20, 2023
@ealmloff ealmloff deleted the prevent-default-form-web branch July 20, 2023 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working web relating to the web renderer for dioxus
Projects
None yet
Development

Successfully merging this pull request may close these issues.

onsubmit example doesn't behave as expected.
3 participants