sycamore-rs / sycamore

A library for creating reactive web apps in Rust and WebAssembly
https://sycamore-rs.netlify.app
MIT License
2.79k stars 148 forks source link

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

Closed dyanechi closed 6 months ago

dyanechi commented 6 months ago

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();
};
codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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

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

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.