瀏覽代碼

add if statements to test and rsx usage example

Evan Almloff 1 年之前
父節點
當前提交
c0f345e775
共有 3 個文件被更改,包括 14 次插入2 次删除
  1. 5 0
      examples/rsx_usage.rs
  2. 3 1
      packages/core/tests/kitchen_sink.rs
  3. 6 1
      packages/rsx/src/attribute.rs

+ 5 - 0
examples/rsx_usage.rs

@@ -53,6 +53,7 @@ fn app(cx: Scope) -> Element {
     let formatting = "formatting!";
     let formatting_tuple = ("a", "b");
     let lazy_fmt = format_args!("lazily formatted text");
+    let asd = 123;
     cx.render(rsx! {
         div {
             // Elements
@@ -80,6 +81,10 @@ fn app(cx: Scope) -> Element {
                 // pass simple rust expressions in
                 class: lazy_fmt,
                 id: format_args!("attributes can be passed lazily with std::fmt::Arguments"),
+                class: "asd",
+                class: "{asd}",
+                // if statements can be used to conditionally render attributes
+                class: if formatting.contains("form") { "{asd}" },
                 div {
                     class: {
                         const WORD: &str = "expressions";

+ 3 - 1
packages/core/tests/kitchen_sink.rs

@@ -10,6 +10,8 @@ fn basic_syntax_is_a_template(cx: Scope) -> Element {
         div { key: "12345",
             class: "asd",
             class: "{asd}",
+            class: if true { "{asd}" },
+            class: if false { "{asd}" },
             onclick: move |_| {},
             div { "{var}" }
             div {
@@ -37,7 +39,7 @@ fn dual_stream() {
             LoadTemplate { name: "template", index: 0, id: ElementId(1) },
             SetAttribute {
                 name: "class",
-                value: (&*bump.alloc("ast 123".into_value(&bump))).into(),
+                value: (&*bump.alloc("asd 123 123".into_value(&bump))).into(),
                 id: ElementId(1),
                 ns: None,
             },

+ 6 - 1
packages/rsx/src/attribute.rs

@@ -121,7 +121,12 @@ impl Parse for ElementAttrValue {
             } else {
                 ElementAttrValue::AttrOptionalExpr {
                     condition: *if_expr.cond,
-                    value: Box::new(syn::parse2(if_expr.then_branch.into_token_stream())?),
+                    value: {
+                        let stmts = if_expr.then_branch.stmts;
+                        Box::new(syn::parse2(quote! {
+                            #(#stmts)*
+                        })?)
+                    },
                 }
             }
         } else if input.peek(LitStr) {