Allow children shorthand in components

This commit is contained in:
Jonathan Kelley committed 2024-01-10 23:52:38 -08:00
1 parent 8a77d2560e
commit aff5259654
2 files changed
+11 -19

No files matched your search

+5 -4
View File
@@ -15,17 +15,18 @@ fn app(cx: Scope) -> Element {
let children = render! { "Child" }; let children = render! { "Child" };
render! { render! {
div { class, id, {children} } div { class, id, {&children} }
Component { a, b, c } Component { a, b, c, children }
Component { a, ..ComponentProps { a: 1, b: 2, c: 3 } } Component { a, ..ComponentProps { a: 1, b: 2, c: 3, children: None } }
} }
} }
#[component] #[component]
fn Component(cx: Scope, a: i32, b: i32, c: i32) -> Element { fn Component<'a>(cx: Scope<'a>, a: i32, b: i32, c: i32, children: Element<'a>) -> Element {
render! { render! {
div { "{a}" } div { "{a}" }
div { "{b}" } div { "{b}" }
div { "{c}" } div { "{c}" }
div { {children} }
} }
} }
+6 -15
View File
@@ -55,23 +55,14 @@ impl Parse for Component {
manual_props = Some(content.parse()?); manual_props = Some(content.parse()?);
} else if } else if
// Named fields // Named fields
content.peek(Ident) && content.peek2(Token![:]) && !content.peek3(Token![:]) { (content.peek(Ident) && content.peek2(Token![:]) && !content.peek3(Token![:]))
fields.push(content.parse()?);
} else if
// shorthand struct initialization // shorthand struct initialization
// Not a div {}, mod::Component {}, or web-component {} // Not a div {}, mod::Component {}, or web-component {}
content.peek(Ident) || (content.peek(Ident)
&& !content.peek2(Brace) && !content.peek2(Brace)
&& !content.peek2(Token![:]) && !content.peek2(Token![:])
&& !content.peek2(Token![-]) && !content.peek2(Token![-]))
{ {
let name = content.fork().parse::<Ident>().unwrap().to_string();
// If the shorthand field is children, these are actually children!
if name == "children" {
panic!("children must be wrapped in braces");
};
fields.push(content.parse()?); fields.push(content.parse()?);
} else { } else {
children.push(content.parse()?); children.push(content.parse()?);