leptos-rs / leptos

Build fast web applications with Rust.
https://leptos.dev
MIT License
16.43k stars 659 forks source link

[0.7] `MaybeProp` can't be passed as attribute #3240

Open DanielleHuisman opened 2 weeks ago

DanielleHuisman commented 2 weeks ago

Describe the bug

Passing a MaybeProp as attribute gives an error:

#[component]
fn TestButton(#[prop(into, optional)] id: MaybeProp<String>, children: Children) -> impl IntoView {
    view! {
        <button id=id>
            {children()}
        </button>
    }
}
error[E0277]: the trait bound `leptos::prelude::MaybeProp<std::string::String>: leptos::prelude::IntoAttributeValue` is not satisfied
   --> packages/leptos/button/src/default.rs:115:20
    |
114 | /     view! {
115 | |         <button id=id>
    | |                    ^^ the trait `FnMut()` is not implemented for `leptos::prelude::MaybeProp<std::string::String>`, which is required by `leptos::prelude::MaybeProp<std::string::String>: leptos::prelude::IntoAttributeValue`
116 | |             {children()}
117 | |         </button>
118 | |     }
    | |_____- required by a bound introduced by this call
    |
    = note: required for `leptos::prelude::MaybeProp<std::string::String>` to implement `leptos::tachys::reactive_graph::ReactiveFunction`
    = note: required for `leptos::prelude::MaybeProp<std::string::String>` to implement `leptos::attr::AttributeValue`
    = note: required for `leptos::prelude::MaybeProp<std::string::String>` to implement `leptos::prelude::IntoAttributeValue`

However, manually calling .get() works fine:

#[component]
fn TestButton(#[prop(into, optional)] id: MaybeProp<String>, children: Children) -> impl IntoView {
    view! {
        <button id=move || id.get()>
            {children()}
        </button>
    }
}

Leptos Dependencies

Please copy and paste the Leptos dependencies and features from your Cargo.toml.

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

Expected behavior

I would expect MaybeProp to be accepted as is, without manually calling .get().

gbj commented 2 weeks ago

PR welcome to add it.