From 9f83b654d7813383884ae38b4c5e7b16c813884b Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Mon, 3 Feb 2025 16:31:26 -0800 Subject: [PATCH] [ENG-4570] Iterate over ObjectVar.entries --- reflex/.templates/jinja/web/pages/utils.js.jinja2 | 2 +- reflex/components/core/foreach.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/reflex/.templates/jinja/web/pages/utils.js.jinja2 b/reflex/.templates/jinja/web/pages/utils.js.jinja2 index 08aeb0d38..c883dadcb 100644 --- a/reflex/.templates/jinja/web/pages/utils.js.jinja2 +++ b/reflex/.templates/jinja/web/pages/utils.js.jinja2 @@ -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 %} diff --git a/reflex/components/core/foreach.py b/reflex/components/core/foreach.py index 927b01333..e9222b200 100644 --- a/reflex/components/core/foreach.py +++ b/reflex/components/core/foreach.py @@ -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,