Browse Source

fix tests around children elements

Jonathan Kelley 1 năm trước cách đây
mục cha
commit
8a77d2560e

+ 4 - 1
examples/shorthand.rs

@@ -11,8 +11,11 @@ fn app(cx: Scope) -> Element {
     let class = "class";
     let id = "id";
 
+    // todo: i'd like it for children to be inferred
+    let children = render! { "Child" };
+
     render! {
-        div { class, id }
+        div { class, id, {children} }
         Component { a, b, c }
         Component { a, ..ComponentProps { a: 1, b: 2, c: 3 } }
     }

+ 1 - 1
packages/core/src/virtual_dom.rs

@@ -78,7 +78,7 @@ use std::{
 /// #[component]
 /// fn Title<'a>(cx: Scope<'a>, children: Element<'a>) -> Element {
 ///     cx.render(rsx! {
-///         div { id: "title", children }
+///         div { id: "title", {children} }
 ///     })
 /// }
 /// ```

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

@@ -55,14 +55,23 @@ impl Parse for Component {
                 manual_props = Some(content.parse()?);
             } else if
             // 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
-                // 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()?);

+ 6 - 0
packages/rsx/src/element.rs

@@ -137,6 +137,12 @@ impl Parse for Element {
             {
                 let name = content.parse::<Ident>()?;
                 let name_ = name.clone();
+
+                // If the shorthand field is children, these are actually children!
+                if name == "children" {
+                    panic!("children must be wrapped in braces");
+                };
+
                 let value = ElementAttrValue::Shorthand(name.clone());
                 attributes.push(attribute::AttributeType::Named(ElementAttrNamed {
                     el_name: el_name.clone(),