Skip to content

Commit f9b0d4d

Browse files
authored
allow rendering vars in foreach (#5271)
* allow rendering vars in foreach * add test for it
1 parent 69eae3f commit f9b0d4d

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

reflex/components/tags/iter_tag.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def render_component(self) -> Component:
105105
The rendered component.
106106
"""
107107
# Import here to avoid circular imports.
108+
from reflex.compiler.compiler import _into_component_once
108109
from reflex.components.base.fragment import Fragment
109-
from reflex.components.component import Component
110110
from reflex.components.core.cond import Cond
111111
from reflex.components.core.foreach import Foreach
112112

@@ -128,11 +128,9 @@ def render_component(self) -> Component:
128128
if isinstance(component, (Foreach, Cond)):
129129
component = Fragment.create(component)
130130

131-
# If the component is a tuple, unpack and wrap it in a fragment.
132-
if isinstance(component, tuple):
133-
component = Fragment.create(*component)
131+
component = _into_component_once(component)
134132

135-
if not isinstance(component, Component):
133+
if component is None:
136134
raise ValueError("The render function must return a component.")
137135

138136
# Set the component key.

tests/integration/test_var_operations.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,13 @@ def index():
599599
),
600600
id="foreach_list_nested",
601601
),
602+
rx.box(
603+
rx.foreach(
604+
ArrayVar.range(0, 2),
605+
lambda x: VarOperationState.list1[x],
606+
),
607+
id="foreach_list_arg2",
608+
),
602609
memo_comp(
603610
list1=VarOperationState.list1,
604611
int_var1=VarOperationState.int_var1,
@@ -950,6 +957,7 @@ def test_var_operations(driver, var_operations: AppHarness):
950957
("foreach_list_arg", "1\n2"),
951958
("foreach_list_ix", "1\n2"),
952959
("foreach_list_nested", "1\n1\n2"),
960+
("foreach_list_arg2", "12"),
953961
# rx.memo component with state
954962
("memo_comp", "1210"),
955963
("memo_comp_nested", "345"),
@@ -992,8 +1000,10 @@ def test_var_operations(driver, var_operations: AppHarness):
9921000
]
9931001

9941002
for tag, expected in tests:
995-
print(tag)
996-
assert driver.find_element(By.ID, tag).text == expected
1003+
existing = driver.find_element(By.ID, tag).text
1004+
assert existing == expected, (
1005+
f"Failed on {tag}, expected {expected} but got {existing}"
1006+
)
9971007

9981008
# Highlight component with var query (does not plumb ID)
9991009
assert driver.find_element(By.TAG_NAME, "mark").text == "second"

0 commit comments

Comments
 (0)