diff --git a/examples/shorthand.rs b/examples/shorthand.rs index 4a3e9b416..38b965f3d 100644 --- a/examples/shorthand.rs +++ b/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 } } } diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 28ccbc76a..932f9cbfb 100644 --- a/packages/core/src/virtual_dom.rs +++ b/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} } /// }) /// } /// ``` diff --git a/packages/rsx/src/component.rs b/packages/rsx/src/component.rs index c7f4c1828..b878d2821 100644 --- a/packages/rsx/src/component.rs +++ b/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::().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()?); diff --git a/packages/rsx/src/element.rs b/packages/rsx/src/element.rs index 45a11125c..ff398f7d3 100644 --- a/packages/rsx/src/element.rs +++ b/packages/rsx/src/element.rs @@ -137,6 +137,12 @@ impl Parse for Element { { let name = content.parse::()?; 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(),