remove even more cond and match
This commit is contained in:
parent
db89a712e9
commit
b10f6e836d
@ -8,8 +8,6 @@
|
|||||||
{{- component }}
|
{{- component }}
|
||||||
{%- elif "iterable" in component %}
|
{%- elif "iterable" in component %}
|
||||||
{{- render_iterable_tag(component) }}
|
{{- render_iterable_tag(component) }}
|
||||||
{%- elif "cond" in component %}
|
|
||||||
{{- render_condition_tag(component) }}
|
|
||||||
{%- elif component.children|length %}
|
{%- elif component.children|length %}
|
||||||
{{- render_tag(component) }}
|
{{- render_tag(component) }}
|
||||||
{%- else %}
|
{%- else %}
|
||||||
|
@ -67,7 +67,6 @@ from reflex.vars.base import (
|
|||||||
cached_property_no_lock,
|
cached_property_no_lock,
|
||||||
)
|
)
|
||||||
from reflex.vars.function import ArgsFunctionOperation, FunctionStringVar
|
from reflex.vars.function import ArgsFunctionOperation, FunctionStringVar
|
||||||
from reflex.vars.number import ternary_operation
|
|
||||||
from reflex.vars.object import ObjectVar
|
from reflex.vars.object import ObjectVar
|
||||||
from reflex.vars.sequence import LiteralArrayVar
|
from reflex.vars.sequence import LiteralArrayVar
|
||||||
|
|
||||||
@ -2430,33 +2429,6 @@ def render_dict_to_var(tag: dict | Component | str, imported_names: set[str]) ->
|
|||||||
func,
|
func,
|
||||||
)
|
)
|
||||||
|
|
||||||
if tag["name"] == "match":
|
|
||||||
element = tag["cond"]
|
|
||||||
|
|
||||||
conditionals = tag["default"]
|
|
||||||
|
|
||||||
for case in tag["match_cases"][::-1]:
|
|
||||||
condition = case[0].to_string() == element.to_string()
|
|
||||||
for pattern in case[1:-1]:
|
|
||||||
condition = condition | (pattern.to_string() == element.to_string())
|
|
||||||
|
|
||||||
conditionals = ternary_operation(
|
|
||||||
condition,
|
|
||||||
case[-1],
|
|
||||||
conditionals,
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditionals
|
|
||||||
|
|
||||||
if "cond" in tag:
|
|
||||||
return ternary_operation(
|
|
||||||
tag["cond"],
|
|
||||||
render_dict_to_var(tag["true_value"], imported_names),
|
|
||||||
render_dict_to_var(tag["false_value"], imported_names)
|
|
||||||
if tag["false_value"] is not None
|
|
||||||
else Var.create(None),
|
|
||||||
)
|
|
||||||
|
|
||||||
props = {}
|
props = {}
|
||||||
|
|
||||||
special_props = []
|
special_props = []
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
"""Representations for React tags."""
|
"""Representations for React tags."""
|
||||||
|
|
||||||
from .cond_tag import CondTag
|
|
||||||
from .iter_tag import IterTag
|
from .iter_tag import IterTag
|
||||||
from .match_tag import MatchTag
|
|
||||||
from .tag import Tag
|
from .tag import Tag
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
"""Tag to conditionally render components."""
|
|
||||||
|
|
||||||
import dataclasses
|
|
||||||
from typing import Any, Dict, Optional
|
|
||||||
|
|
||||||
from reflex.components.tags.tag import Tag
|
|
||||||
from reflex.vars.base import Var
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass()
|
|
||||||
class CondTag(Tag):
|
|
||||||
"""A conditional tag."""
|
|
||||||
|
|
||||||
# The condition to determine which component to render.
|
|
||||||
cond: Var[Any] = dataclasses.field(default_factory=lambda: Var.create(True))
|
|
||||||
|
|
||||||
# The code to render if the condition is true.
|
|
||||||
true_value: Dict = dataclasses.field(default_factory=dict)
|
|
||||||
|
|
||||||
# The code to render if the condition is false.
|
|
||||||
false_value: Optional[Dict] = None
|
|
@ -1,21 +0,0 @@
|
|||||||
"""Tag to conditionally match cases."""
|
|
||||||
|
|
||||||
import dataclasses
|
|
||||||
from typing import Any, List
|
|
||||||
|
|
||||||
from reflex.components.tags.tag import Tag
|
|
||||||
from reflex.vars.base import Var
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass()
|
|
||||||
class MatchTag(Tag):
|
|
||||||
"""A match tag."""
|
|
||||||
|
|
||||||
# The condition to determine which case to match.
|
|
||||||
cond: Var[Any] = dataclasses.field(default_factory=lambda: Var.create(True))
|
|
||||||
|
|
||||||
# The list of match cases to be matched.
|
|
||||||
match_cases: List[Any] = dataclasses.field(default_factory=list)
|
|
||||||
|
|
||||||
# The catchall case to match.
|
|
||||||
default: Any = dataclasses.field(default=Var.create(None))
|
|
@ -2,7 +2,7 @@ from typing import Dict, List
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from reflex.components.tags import CondTag, Tag, tagless
|
from reflex.components.tags import Tag, tagless
|
||||||
from reflex.vars.base import LiteralVar, Var
|
from reflex.vars.base import LiteralVar, Var
|
||||||
|
|
||||||
|
|
||||||
@ -105,29 +105,6 @@ def test_format_tag(tag: Tag, expected: Dict):
|
|||||||
assert prop_value.equals(LiteralVar.create(expected["props"][prop]))
|
assert prop_value.equals(LiteralVar.create(expected["props"][prop]))
|
||||||
|
|
||||||
|
|
||||||
def test_format_cond_tag():
|
|
||||||
"""Test that the cond tag dict is correct."""
|
|
||||||
tag = CondTag(
|
|
||||||
true_value=dict(Tag(name="h1", contents="True content")),
|
|
||||||
false_value=dict(Tag(name="h2", contents="False content")),
|
|
||||||
cond=Var(_js_expr="logged_in", _var_type=bool),
|
|
||||||
)
|
|
||||||
tag_dict = dict(tag)
|
|
||||||
cond, true_value, false_value = (
|
|
||||||
tag_dict["cond"],
|
|
||||||
tag_dict["true_value"],
|
|
||||||
tag_dict["false_value"],
|
|
||||||
)
|
|
||||||
assert cond._js_expr == "logged_in"
|
|
||||||
assert cond._var_type is bool
|
|
||||||
|
|
||||||
assert true_value["name"] == "h1"
|
|
||||||
assert true_value["contents"] == "True content"
|
|
||||||
|
|
||||||
assert false_value["name"] == "h2"
|
|
||||||
assert false_value["contents"] == "False content"
|
|
||||||
|
|
||||||
|
|
||||||
def test_tagless_string_representation():
|
def test_tagless_string_representation():
|
||||||
"""Test that the string representation of a tagless is correct."""
|
"""Test that the string representation of a tagless is correct."""
|
||||||
tag = tagless.Tagless(contents="Hello world")
|
tag = tagless.Tagless(contents="Hello world")
|
||||||
|
Loading…
Reference in New Issue
Block a user