anp / moxie

lightweight platform-agnostic tools for declarative UI

Home Page:https://moxie.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support tags with dynamic/variable children

anp opened this issue · comments

In the dom-hacking example, we ensure we can add children in a loop (which also works for a conditional):

let mut root = div();
root = root.child(mox! { <div>{% "hello world from moxie! ({})", &count }</div> });
root = root.child(mox! {
<button type="button" onclick={move |_| set_count.update(|c| Some(c + 1))}>
"increment"
</button>
});
for t in &["first", "second", "third"] {
root = root.child(mox! { <div>{% "{}", t }</div> });
}
root.build()

It would be nice to be able to write the whole example as a single mox invocation, maybe something like:

mox! {
    <div>
        <div>{% "hello world from moxie! ({})", &count }</div>
        <button type="button" onclick={move |_| set_count.update(|c| Some(c + 1))}>
            "increment"
        </button>
        |mut root: Div| -> Div {
            for t in &["first", "second", "third"] {
                root = root.child(mox! { <div>{% "{}", t }</div> });
            }
            root
        }
    </div>
}

Maybe we could pass impl Iterator into .child(iter.into_child()). That would allow to implement fragments.

Passing closure would allow to transform for example Div into something completely else. While this is an interesting possibly, closing tag </div> would make confusion. Thus this kind of behaviour should be disallowed, while using mox! macro.

Example from above redone:

mox! {
    <div>
        <div>{% "hello world from moxie! ({})", &count }</div>
        <button type="button" onclick={move |_| set_count.update(|c| Some(c + 1))}>
            "increment"
        </button>
        { ["first", "second", "third"].iter().map(|t| mox! { <div>{ *}</div> }) }
    </div>
}

I hadn't thought of supporting iterators directly, I like that idea a lot.

Not sure if that should replace the ability to write your own loop, but maybe it's enough?

Not sure if that should replace the ability to write your own loop, but maybe it's enough?

If you would like to use loops, then use generators.