leptos icon indicating copy to clipboard operation
leptos copied to clipboard

Spreading a dynamic list of attributes on a component

Open bram209 opened this issue 1 year ago • 2 comments

Describe the bug Not entirely sure if this is a bug or a feature request, but I would like to be able to spread a dynamic list of attributes to a component.

Leptos Dependencies

leptos = { version = "0.6", features = ["nightly"] }
leptos_axum = { version = "0.6", optional = true }
leptos_meta = { version = "0.6", features = ["nightly"] }
leptos_router = { version = "0.6", features = ["nightly"] }

To Reproduce

#[component]
fn Comp1() -> impl IntoView {
    let attrs = vec![("id", Attribute::String("1".into()))];
    view! {
        <>
            // this works (id is set to 1)
            <div {..attrs}>Hello</div>

            // this doesn't work (no id is set)
            <Comp2 {..attrs}/>

            // this does work
            <Comp2 attr:test=5/>
        </>
    }
}

#[component]
fn Comp2(#[prop(attrs)] attrs: Vec<(&'static str, Attribute)>) -> impl IntoView {
    let attrs_len = attrs.len();
    view! { <div {..attrs}>{attrs_len}</div> }
}

Expected behavior I would expect that Comp2 renders into <div id="1">1</div>, but instead it renders to <div>0</div>.

bram209 avatar Mar 04 '24 18:03 bram209

i've got a working solution for this, but my fork also includes a way to spread things other than attributes which https://github.com/leptos-rs/leptos/pull/2432 seems to have beaten me to

so if/when it gets merged i'll adapt it to that

Upbolt avatar Mar 21 '24 12:03 Upbolt

i've got a working solution for this, but my fork also includes a way to spread things other than attributes which #2432 seems to have beaten me to

so if/when it gets merged i'll adapt it to that

Very cool, thanks for your contributions : )

bram209 avatar Mar 22 '24 15:03 bram209