Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion reflex/compiler/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def render_iterable_tag(component: Any) -> str:
children_rendered = "".join(
[_RenderUtils.render(child) for child in component.get("children", [])]
)
return f"{component['iterable_state']}.map(({component['arg_name']},{component['arg_index']})=>({children_rendered}))"
return f"Array.prototype.map.call({component['iterable_state']} ?? [],(({component['arg_name']},{component['arg_index']})=>({children_rendered})))"

@staticmethod
def render_match_tag(component: Any) -> str:
Expand Down
27 changes: 6 additions & 21 deletions reflex/vars/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,9 @@ def object_keys_operation(value: ObjectVar):
Returns:
The keys of the object.
"""
if not types.is_optional(value._var_type):
return var_operation_return(
js_expression=f"Object.keys({value})",
var_type=list[str],
)
return var_operation_return(
js_expression=f"((value) => value ?? undefined === undefined ? undefined : Object.keys(value))({value})",
var_type=(list[str] | None),
js_expression=f"Object.keys({value} ?? {{}})",
var_type=list[str],
)


Expand All @@ -500,14 +495,9 @@ def object_values_operation(value: ObjectVar):
Returns:
The values of the object.
"""
if not types.is_optional(value._var_type):
return var_operation_return(
js_expression=f"Object.values({value})",
var_type=list[value._value_type()],
)
return var_operation_return(
js_expression=f"((value) => value ?? undefined === undefined ? undefined : Object.values(value))({value})",
var_type=(list[value._value_type()] | None),
js_expression=f"Object.values({value} ?? {{}})",
var_type=list[value._value_type()],
)


Expand All @@ -521,14 +511,9 @@ def object_entries_operation(value: ObjectVar):
Returns:
The entries of the object.
"""
if not types.is_optional(value._var_type):
return var_operation_return(
js_expression=f"Object.entries({value})",
var_type=list[tuple[str, value._value_type()]],
)
return var_operation_return(
js_expression=f"((value) => value ?? undefined === undefined ? undefined : Object.entries(value))({value})",
var_type=(list[tuple[str, value._value_type()]] | None),
js_expression=f"Object.entries({value} ?? {{}})",
var_type=list[tuple[str, value._value_type()]],
)


Expand Down
23 changes: 23 additions & 0 deletions tests/integration/test_var_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,29 @@ def index():
rx.text(ArrayVar.range(2, 10, 2).join(","), id="list_join_range2"),
rx.text(ArrayVar.range(5, 0, -1).join(","), id="list_join_range3"),
rx.text(ArrayVar.range(0, 3).join(","), id="list_join_range4"),
rx.box(
# Test that foreach works with various non-array inputs without throwing
rx.foreach(
rx.Var("undefined").to(list),
rx.text.span,
),
rx.foreach(
rx.Var("null").to(list),
rx.text.span,
),
rx.foreach(
rx.Var("({})").to(list),
rx.text.span,
),
rx.foreach(
rx.Var("2").to(list),
rx.text.span,
),
rx.foreach(
rx.Var("false").to(list),
rx.text.span,
),
),
rx.box(
rx.foreach(
ArrayVar.range(0, 2),
Expand Down
8 changes: 4 additions & 4 deletions tests/units/components/core/test_foreach.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,28 @@ def display_color_index_tuple(color):
ForEachState.primary_color,
display_primary_colors,
{
"iterable_state": f"Object.entries({ForEachState.get_full_name()}.primary_color{FIELD_MARKER})",
"iterable_state": f"Object.entries({ForEachState.get_full_name()}.primary_color{FIELD_MARKER} ?? {{}})",
},
),
(
ForEachState.color_with_shades,
display_color_with_shades,
{
"iterable_state": f"Object.entries({ForEachState.get_full_name()}.color_with_shades{FIELD_MARKER})",
"iterable_state": f"Object.entries({ForEachState.get_full_name()}.color_with_shades{FIELD_MARKER} ?? {{}})",
},
),
(
ForEachState.nested_colors_with_shades,
display_nested_color_with_shades,
{
"iterable_state": f"Object.entries({ForEachState.get_full_name()}.nested_colors_with_shades{FIELD_MARKER})",
"iterable_state": f"Object.entries({ForEachState.get_full_name()}.nested_colors_with_shades{FIELD_MARKER} ?? {{}})",
},
),
(
ForEachState.nested_colors_with_shades,
display_nested_color_with_shades_v2,
{
"iterable_state": f"Object.entries({ForEachState.get_full_name()}.nested_colors_with_shades{FIELD_MARKER})",
"iterable_state": f"Object.entries({ForEachState.get_full_name()}.nested_colors_with_shades{FIELD_MARKER} ?? {{}})",
},
),
(
Expand Down
11 changes: 6 additions & 5 deletions tests/units/test_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,15 +1079,16 @@ def test_object_operations():
object_var = LiteralObjectVar.create({"a": 1, "b": 2, "c": 3})

assert (
str(object_var.keys()) == 'Object.keys(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }))'
str(object_var.keys())
== 'Object.keys(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }) ?? {})'
)
assert (
str(object_var.values())
== 'Object.values(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }))'
== 'Object.values(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }) ?? {})'
)
assert (
str(object_var.entries())
== 'Object.entries(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }))'
== 'Object.entries(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }) ?? {})'
)
assert str(object_var.a) == '({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })["a"]'
assert str(object_var["a"]) == '({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })["a"]'
Expand Down Expand Up @@ -1129,11 +1130,11 @@ def test_type_chains():
)
assert (
str(object_var.keys()[0].upper())
== 'Object.keys(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })).at(0).toUpperCase()'
== 'Object.keys(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }) ?? {}).at(0).toUpperCase()'
)
assert (
str(object_var.entries()[1][1] - 1)
== '(Object.entries(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })).at(1).at(1) - 1)'
== '(Object.entries(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }) ?? {}).at(1).at(1) - 1)'
)
assert (
str(object_var["c"] + object_var["b"]) # pyright: ignore [reportCallIssue, reportOperatorIssue]
Expand Down
Loading