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

Add 'SubmitEvent' to re-exported events from web_sys crate #668

Merged
merged 1 commit into from
Apr 3, 2024

Conversation

dyanechi
Copy link
Contributor

@dyanechi dyanechi commented Apr 3, 2024

Allows to use SubmitEvent type explicitly in closures to fix error when submitting form with on:submit.

Currently the following code:

let submit_fn = move |e: Event| {
    e.prevent_default();
};

view! {
    main(class="container") {
        form(on:submit=submit_fn) {
            button(type="submit") {
                "Submit"
            }
        }
    }
}

Will cause error:

the trait bound `{closure@src/app.rs:41:21: 41:36}: EventHandler<_, submit, _>` is not satisfied
the trait `EventHandler<_, submit, _>` is not implemented for closure `{closure@src/app.rs:41:21: 41:36}`rustc[...](...)
app.rs(45, 5): required by a bound introduced by this call
generic_node.rs(128, 12): required by a bound in `event`

Defining function with empty closure let submit_fn = move|_| {...} infers to impl Fn(SubmitEvent) but you can't call e.prevent_default() that prevents page refresh on submitting forms.

Unless there's better way to do it (like implementing, this simple re-export of SubmitEvent fixes the problem by allowing explicit type definition. This refactored function now compiles and works as expected:

let submit_fn = move |e: SubmitEvent| {
    e.prevent_default();
};

Allows to use SubmitEvent in closures to avoid errors when submitting form with `on:submit`.
Copy link

codecov bot commented Apr 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.44%. Comparing base (5d46d5e) to head (0127771).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #668   +/-   ##
=======================================
  Coverage   60.44%   60.44%           
=======================================
  Files          54       54           
  Lines        8543     8543           
=======================================
  Hits         5164     5164           
  Misses       3379     3379           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@lukechu10 lukechu10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this was a typo from my part. I'm putting the blame on Copilot 😓

@lukechu10 lukechu10 merged commit 86f0b05 into sycamore-rs:master Apr 3, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants