[ENG-4570] Iterate over ObjectVar.entries

This commit is contained in:
Masen Furer 2025-02-03 16:31:26 -08:00
parent 27a745b38d
commit 9f83b654d7
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95
2 changed files with 6 additions and 1 deletions

View File

@ -60,7 +60,7 @@
{# Args: #}
{# component: component dictionary #}
{% macro render_iterable_tag(component) %}
<>{ {%- if component.iterable_type == 'dict' -%}Object.entries({{- component.iterable_state }}){%- else -%}{{- component.iterable_state }}{%- endif -%}.map(({{ component.arg_name }}, {{ component.arg_index }}) => (
<>{ {{ component.iterable_state }}.map(({{ component.arg_name }}, {{ component.arg_index }}) => (
{% for child in component.children %}
{{ render(child) }}
{% endfor %}

View File

@ -54,6 +54,8 @@ class Foreach(Component):
TypeError: If the render function is a ComponentState.
UntypedVarError: If the iterable is of type Any without a type annotation.
"""
from reflex.vars.object import ObjectVar
iterable = LiteralVar.create(iterable)
if iterable._var_type == Any:
raise ForeachVarError(
@ -70,6 +72,9 @@ class Foreach(Component):
"Using a ComponentState as `render_fn` inside `rx.foreach` is not supported yet."
)
if isinstance(iterable, ObjectVar):
iterable = iterable.entries()
component = cls(
iterable=iterable,
render_fn=render_fn,