leptos
leptos copied to clipboard
[0.7] `Callback` can't be passed as listener
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>>
.