浏览代码

Allow children shorthand in components

Jonathan Kelley 1 年之前
父节点
当前提交
aff5259654
共有 2 个文件被更改,包括 11 次插入19 次删除
  1. 5 4
      examples/shorthand.rs
  2. 6 15
      packages/rsx/src/component.rs

+ 5 - 4
examples/shorthand.rs

@@ -15,17 +15,18 @@ fn app(cx: Scope) -> Element {
     let children = render! { "Child" };
 
     render! {
-        div { class, id, {children} }
-        Component { a, b, c }
-        Component { a, ..ComponentProps { a: 1, b: 2, c: 3 } }
+        div { class, id, {&children} }
+        Component { a, b, c, children }
+        Component { a, ..ComponentProps { a: 1, b: 2, c: 3, children: None } }
     }
 }
 
 #[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! {
         div { "{a}" }
         div { "{b}" }
         div { "{c}" }
+        div { {children} }
     }
 }

+ 6 - 15
packages/rsx/src/component.rs

@@ -55,23 +55,14 @@ impl Parse for Component {
                 manual_props = Some(content.parse()?);
             } else if
             // Named fields
-            content.peek(Ident) && content.peek2(Token![:]) && !content.peek3(Token![:]) {
-                fields.push(content.parse()?);
-            } else if
+            (content.peek(Ident) && content.peek2(Token![:]) && !content.peek3(Token![:]))
             // shorthand struct initialization
-            // Not a div {}, mod::Component {}, or web-component {}
-            content.peek(Ident)
-                && !content.peek2(Brace)
-                && !content.peek2(Token![:])
-                && !content.peek2(Token![-])
+                // Not a div {}, mod::Component {}, or web-component {}
+                || (content.peek(Ident)
+                    && !content.peek2(Brace)
+                    && !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()?);
             } else {
                 children.push(content.parse()?);