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

[0.7] Callback can't be passed as listener #3241

Open
DanielleHuisman opened this issue Nov 13, 2024 · 1 comment
Open

[0.7] Callback can't be passed as listener #3241

DanielleHuisman opened this issue Nov 13, 2024 · 1 comment

Comments

@DanielleHuisman
Copy link
Contributor

Describe the bug

Passing a Callback as listener gives an error:

#[component]
fn TestButton(#[prop(into)] on_click: Callback<MouseEvent>, children: Children) -> impl IntoView {
    view! {
        <button on:click=on_click>
            {children()}
        </button>
    }
}
error[E0277]: expected a `FnMut(leptos::ev::MouseEvent)` closure, found `leptos::prelude::Callback<leptos::ev::MouseEvent>`
   --> packages/leptos/button/src/default.rs:114:5
    |
114 | /     view! {
115 | |         <button on:click=on_click>
    | |                 -- required by a bound introduced by this call
116 | |             {children()}
117 | |         </button>
118 | |     }
    | |_____^ expected an `FnMut(leptos::ev::MouseEvent)` closure, found `leptos::prelude::Callback<leptos::ev::MouseEvent>`
    |
    = help: the trait `FnMut(leptos::ev::MouseEvent)` is not implemented for `leptos::prelude::Callback<leptos::ev::MouseEvent>`, which is required by `leptos::html::HtmlElement<leptos::html::Button, (), (leptos::prelude::AnyView,)>: leptos::prelude::OnAttribute<_, _>`
    = help: the trait `leptos::prelude::OnAttribute<E, F>` is implemented for `leptos::html::HtmlElement<El, At, Ch>`
    = note: required for `leptos::html::HtmlElement<leptos::html::Button, (), (leptos::prelude::AnyView,)>` to implement `leptos::prelude::OnAttribute<leptos::ev::click, leptos::prelude::Callback<leptos::ev::MouseEvent>>`

However, manually calling .run() works fine:

#[component]
fn TestButton(#[prop(into)] on_click: Callback<MouseEvent>, children: Children) -> impl IntoView {
    view! {
        <button on:click=move |event| on_click.run(event)>
            {children()}
        </button>
    }
}

Leptos Dependencies

# Latest commit on main branch
leptos = { git = "https://github.com/leptos-rs/leptos.git", rev = "c2b239d" }

Expected behavior

I would expect Callback to be accepted as is, without manually calling .run().

It might be nice for Option<Callback<T>> to be accepted as well, so you can create an
#[prop(into, optional)] on_click: Option<Callback<MouseEvent>>.

@gbj
Copy link
Collaborator

gbj commented Nov 13, 2024

Unfortunately this is working as designed. I have attempted to implement this in the past and not been successful. I'm open to a PR that successfully implements it directly.

I would recommend taking impl FnMut(MouseEvent) + 'static or a similar type for on_click in the examples above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants