Relax type requirement on pc.cond (#323)
This commit is contained in:
parent
d376d2972b
commit
f7e35771e9
@ -1,9 +1,7 @@
|
|||||||
"""Create a list of components from an iterable."""
|
"""Create a list of components from an iterable."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
import pydantic
|
|
||||||
|
|
||||||
from pynecone.components.component import Component
|
from pynecone.components.component import Component
|
||||||
from pynecone.components.layout.fragment import Fragment
|
from pynecone.components.layout.fragment import Fragment
|
||||||
@ -15,7 +13,7 @@ class Cond(Component):
|
|||||||
"""Render one of two components based on a condition."""
|
"""Render one of two components based on a condition."""
|
||||||
|
|
||||||
# The cond to determine which component to render.
|
# The cond to determine which component to render.
|
||||||
cond: Var[bool]
|
cond: Var[Any]
|
||||||
|
|
||||||
# The component to render if the cond is true.
|
# The component to render if the cond is true.
|
||||||
comp1: Component
|
comp1: Component
|
||||||
@ -26,19 +24,6 @@ class Cond(Component):
|
|||||||
# Whether the cond is within another cond.
|
# Whether the cond is within another cond.
|
||||||
is_nested: bool = False
|
is_nested: bool = False
|
||||||
|
|
||||||
@pydantic.validator("cond")
|
|
||||||
def validate_cond(cls, cond: Var) -> Var:
|
|
||||||
"""Validate that the cond is a boolean.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
cond: The cond to validate.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The validated cond.
|
|
||||||
"""
|
|
||||||
assert issubclass(cond.type_, bool), "The var must be a boolean."
|
|
||||||
return cond
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(
|
def create(
|
||||||
cls, cond: Var, comp1: Component, comp2: Optional[Component] = None
|
cls, cond: Var, comp1: Component, comp2: Optional[Component] = None
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
"""Tag to conditionally render components."""
|
"""Tag to conditionally render components."""
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from pynecone import utils
|
from pynecone import utils
|
||||||
from pynecone.components.tags.tag import Tag
|
from pynecone.components.tags.tag import Tag
|
||||||
from pynecone.var import Var
|
from pynecone.var import Var
|
||||||
@ -9,7 +11,7 @@ class CondTag(Tag):
|
|||||||
"""A conditional tag."""
|
"""A conditional tag."""
|
||||||
|
|
||||||
# The condition to determine which component to render.
|
# The condition to determine which component to render.
|
||||||
cond: Var[bool]
|
cond: Var[Any]
|
||||||
|
|
||||||
# The code to render if the condition is true.
|
# The code to render if the condition is true.
|
||||||
true_value: str
|
true_value: str
|
||||||
|
41
tests/components/layout/test_cond.py
Normal file
41
tests/components/layout/test_cond.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
import pynecone as pc
|
||||||
|
from pynecone.components.layout.cond import Cond
|
||||||
|
from pynecone.components.typography.text import Text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def cond_state(request):
|
||||||
|
class CondState(pc.State):
|
||||||
|
value: request.param["value_type"] = request.param["value"]
|
||||||
|
|
||||||
|
return CondState
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"cond_state",
|
||||||
|
[
|
||||||
|
pytest.param({"value_type": bool, "value": True}),
|
||||||
|
pytest.param({"value_type": int, "value": 0}),
|
||||||
|
pytest.param({"value_type": str, "value": "true"}),
|
||||||
|
],
|
||||||
|
indirect=True,
|
||||||
|
)
|
||||||
|
def test_validate_cond(cond_state: pc.Var):
|
||||||
|
"""Test if cond can be a pc.Val with any values
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cond_state: A fixture.
|
||||||
|
"""
|
||||||
|
cond_component = Cond.create(
|
||||||
|
cond_state.value,
|
||||||
|
Text.create("cond is True"),
|
||||||
|
Text.create("cond is False"),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert str(cond_component) == (
|
||||||
|
"{cond_state.value ? "
|
||||||
|
"<Text>{`cond is True`}</Text> : "
|
||||||
|
"<Text>{`cond is False`}</Text>}"
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user