Commit ac576b5
committed
Let a component declare children: Vec<Element> as an alternative to Element
Closes the gap in #1177 ("Iterable Children"): today the
only way to get a caller's children as independent, individually-wrappable
values is a hand-built Vec<Element> field populated with explicit vec![]/
rsx!{} ceremony at every call site (the issue's own documented workaround).
Natural rsx! composition (bare children, for loops, if chains) only ever
produces one opaque merged Element.
This makes both declared shapes accept the exact same call-site syntax, with
zero change to how a caller writes children:
ListItem {
label: "Phone",
"555-1234"
"555-5678"
}
works identically whether ListItem declares `children: Element` (merged, as
today) or `children: Vec<Element>` (one entry per top-level child, for/if
flattened to however many elements they actually produce).
Mechanism: `rsx!`'s component-children codegen (dioxus-rsx's component.rs)
now always compiles a component's children into a Vec<Element> - each
non-control-flow root becomes its own standalone single-root template
(TemplateBody::to_vec_tokens, reusing the existing single-Element codegen
unchanged), a `for` root recurses into its body and flat_maps per iteration,
and an `if`/`else` chain unifies its branches to Vec<Element> directly
(IfChain::to_vec_tokens) instead of merging through IntoDynNode. The macro
has no visibility into the callee's declared field type (macros expand
before type-checking), so it can't choose per-callee - instead the callee
side resolves it: dioxus-core-macro now forces the `children` field to
always go through SuperInto (like #[props(into)]) regardless of whether the
struct author wrote #[props(into)], and dioxus_core::properties gains
SuperFrom<Vec<Element>, _> impls for both `Element` and `Option<Element>`
that merge the vec back into one node through the same dynamic-node-slot
machinery a `for` loop's own output already uses (unwrapping directly for
the 1-element case, so the common single-child path produces the exact same
VNode shape as before).
The two hand-rolled Properties implementations in the whole workspace that
predate the derive macro's current shape (Portal, SuspenseBoundary) had
exact-type `children: Element` setters that would otherwise be the one place
still broken by this - both updated to accept `impl SuperInto<Element, M>`
too, mirroring what their own `fallback`-style setters already did.
Verified via packages/core/tests/vec_children.rs (single/multiple/nested
for+if/empty-default, both Element and Option<Element> targets), the full
existing dioxus-core/dioxus-rsx/dioxus-core-macro/dioxus-html/dioxus-hooks/
dioxus-ssr/dioxus test suites (all green, including doctests with
--all-features), cargo fmt --all --check, and cargo clippy --all-features
--all-targets -- -D warnings on every crate this touches - no regressions.1 parent 24f6a82 commit ac576b5
8 files changed
Lines changed: 465 additions & 17 deletions
File tree
- packages
- core-macro/src/props
- core
- src
- suspense
- tests
- rsx/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
176 | 179 | | |
177 | 180 | | |
178 | 181 | | |
| |||
204 | 207 | | |
205 | 208 | | |
206 | 209 | | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
211 | 228 | | |
212 | 229 | | |
213 | 230 | | |
| |||
497 | 514 | | |
498 | 515 | | |
499 | 516 | | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
500 | 542 | | |
501 | 543 | | |
502 | 544 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
96 | 101 | | |
97 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
98 | 106 | | |
99 | 107 | | |
100 | | - | |
| 108 | + | |
101 | 109 | | |
102 | 110 | | |
103 | 111 | | |
| |||
147 | 155 | | |
148 | 156 | | |
149 | 157 | | |
150 | | - | |
| 158 | + | |
151 | 159 | | |
152 | | - | |
| 160 | + | |
153 | 161 | | |
154 | 162 | | |
155 | 163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
356 | 356 | | |
357 | 357 | | |
358 | 358 | | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
359 | 401 | | |
360 | 402 | | |
361 | 403 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
| 108 | + | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
178 | | - | |
| 178 | + | |
179 | 179 | | |
180 | | - | |
| 180 | + | |
181 | 181 | | |
182 | | - | |
| 182 | + | |
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
| |||
0 commit comments