why not, remove cond
This commit is contained in:
parent
392c5b5a69
commit
b7579f4d8d
@ -932,7 +932,6 @@ class Component(BaseComponent, ABC):
|
|||||||
"""
|
"""
|
||||||
from reflex.components.base.bare import Bare
|
from reflex.components.base.bare import Bare
|
||||||
from reflex.components.base.fragment import Fragment
|
from reflex.components.base.fragment import Fragment
|
||||||
from reflex.components.core.cond import Cond
|
|
||||||
from reflex.components.core.foreach import Foreach
|
from reflex.components.core.foreach import Foreach
|
||||||
|
|
||||||
no_valid_parents_defined = all(child._valid_parents == [] for child in children)
|
no_valid_parents_defined = all(child._valid_parents == [] for child in children)
|
||||||
@ -944,7 +943,7 @@ class Component(BaseComponent, ABC):
|
|||||||
return
|
return
|
||||||
|
|
||||||
comp_name = type(self).__name__
|
comp_name = type(self).__name__
|
||||||
allowed_components = [comp.__name__ for comp in (Fragment, Foreach, Cond)]
|
allowed_components = [comp.__name__ for comp in (Fragment, Foreach)]
|
||||||
|
|
||||||
def validate_child(child):
|
def validate_child(child):
|
||||||
child_name = type(child).__name__
|
child_name = type(child).__name__
|
||||||
@ -954,10 +953,6 @@ class Component(BaseComponent, ABC):
|
|||||||
for c in child.children:
|
for c in child.children:
|
||||||
validate_child(c)
|
validate_child(c)
|
||||||
|
|
||||||
if isinstance(child, Cond):
|
|
||||||
validate_child(child.comp1)
|
|
||||||
validate_child(child.comp2)
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
isinstance(child, Bare)
|
isinstance(child, Bare)
|
||||||
and child.contents is not None
|
and child.contents is not None
|
||||||
@ -2063,13 +2058,10 @@ class StatefulComponent(BaseComponent):
|
|||||||
The Var from the child component or the child itself (for regular cases).
|
The Var from the child component or the child itself (for regular cases).
|
||||||
"""
|
"""
|
||||||
from reflex.components.base.bare import Bare
|
from reflex.components.base.bare import Bare
|
||||||
from reflex.components.core.cond import Cond
|
|
||||||
from reflex.components.core.foreach import Foreach
|
from reflex.components.core.foreach import Foreach
|
||||||
|
|
||||||
if isinstance(child, Bare):
|
if isinstance(child, Bare):
|
||||||
return child.contents
|
return child.contents
|
||||||
if isinstance(child, Cond):
|
|
||||||
return child.cond
|
|
||||||
if isinstance(child, Foreach):
|
if isinstance(child, Foreach):
|
||||||
return child.iterable
|
return child.iterable
|
||||||
return child
|
return child
|
||||||
|
@ -21,7 +21,7 @@ _SUBMOD_ATTRS: dict[str, list[str]] = {
|
|||||||
"colors": [
|
"colors": [
|
||||||
"color",
|
"color",
|
||||||
],
|
],
|
||||||
"cond": ["Cond", "color_mode_cond", "cond"],
|
"cond": ["color_mode_cond", "cond"],
|
||||||
"debounce": ["DebounceInput", "debounce_input"],
|
"debounce": ["DebounceInput", "debounce_input"],
|
||||||
"foreach": [
|
"foreach": [
|
||||||
"foreach",
|
"foreach",
|
||||||
|
@ -17,7 +17,6 @@ from .breakpoints import set_breakpoints as set_breakpoints
|
|||||||
from .clipboard import Clipboard as Clipboard
|
from .clipboard import Clipboard as Clipboard
|
||||||
from .clipboard import clipboard as clipboard
|
from .clipboard import clipboard as clipboard
|
||||||
from .colors import color as color
|
from .colors import color as color
|
||||||
from .cond import Cond as Cond
|
|
||||||
from .cond import color_mode_cond as color_mode_cond
|
from .cond import color_mode_cond as color_mode_cond
|
||||||
from .cond import cond as cond
|
from .cond import cond as cond
|
||||||
from .debounce import DebounceInput as DebounceInput
|
from .debounce import DebounceInput as DebounceInput
|
||||||
|
@ -2,115 +2,16 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, Dict, Optional, overload
|
from typing import Any, overload
|
||||||
|
|
||||||
from reflex.components.base.fragment import Fragment
|
from reflex.components.base.fragment import Fragment
|
||||||
from reflex.components.component import BaseComponent, Component, MemoizationLeaf
|
from reflex.components.component import BaseComponent, Component
|
||||||
from reflex.components.tags import CondTag, Tag
|
|
||||||
from reflex.constants import Dirs
|
|
||||||
from reflex.style import LIGHT_COLOR_MODE, resolved_color_mode
|
from reflex.style import LIGHT_COLOR_MODE, resolved_color_mode
|
||||||
from reflex.utils.imports import ImportDict, ImportVar
|
|
||||||
from reflex.utils.types import infallible_issubclass
|
from reflex.utils.types import infallible_issubclass
|
||||||
from reflex.vars import VarData
|
|
||||||
from reflex.vars.base import LiteralVar, ReflexCallable, Var
|
from reflex.vars.base import LiteralVar, ReflexCallable, Var
|
||||||
from reflex.vars.function import ArgsFunctionOperation
|
from reflex.vars.function import ArgsFunctionOperation
|
||||||
from reflex.vars.number import ternary_operation
|
from reflex.vars.number import ternary_operation
|
||||||
|
|
||||||
_IS_TRUE_IMPORT: ImportDict = {
|
|
||||||
f"$/{Dirs.STATE_PATH}": [ImportVar(tag="isTrue")],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class Cond(MemoizationLeaf):
|
|
||||||
"""Render one of two components based on a condition."""
|
|
||||||
|
|
||||||
# The cond to determine which component to render.
|
|
||||||
cond: Var[Any]
|
|
||||||
|
|
||||||
# The component to render if the cond is true.
|
|
||||||
comp1: BaseComponent = None # type: ignore
|
|
||||||
|
|
||||||
# The component to render if the cond is false.
|
|
||||||
comp2: BaseComponent = None # type: ignore
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def create(
|
|
||||||
cls,
|
|
||||||
cond: Var,
|
|
||||||
comp1: BaseComponent,
|
|
||||||
comp2: Optional[BaseComponent] = None,
|
|
||||||
) -> Component:
|
|
||||||
"""Create a conditional component.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
cond: The cond to determine which component to render.
|
|
||||||
comp1: The component to render if the cond is true.
|
|
||||||
comp2: The component to render if the cond is false.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The conditional component.
|
|
||||||
"""
|
|
||||||
# Wrap everything in fragments.
|
|
||||||
if type(comp1).__name__ != "Fragment":
|
|
||||||
comp1 = Fragment.create(comp1)
|
|
||||||
if comp2 is None or type(comp2).__name__ != "Fragment":
|
|
||||||
comp2 = Fragment.create(comp2) if comp2 else Fragment.create()
|
|
||||||
return Fragment.create(
|
|
||||||
cls(
|
|
||||||
cond=cond,
|
|
||||||
comp1=comp1,
|
|
||||||
comp2=comp2,
|
|
||||||
children=[comp1, comp2],
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
def _get_props_imports(self):
|
|
||||||
"""Get the imports needed for component's props.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The imports for the component's props of the component.
|
|
||||||
"""
|
|
||||||
return []
|
|
||||||
|
|
||||||
def _render(self) -> Tag:
|
|
||||||
return CondTag(
|
|
||||||
cond=self.cond,
|
|
||||||
true_value=self.comp1.render(),
|
|
||||||
false_value=self.comp2.render(),
|
|
||||||
)
|
|
||||||
|
|
||||||
def render(self) -> Dict:
|
|
||||||
"""Render the component.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The dictionary for template of component.
|
|
||||||
"""
|
|
||||||
tag = self._render()
|
|
||||||
return dict(
|
|
||||||
tag.add_props(
|
|
||||||
**self.event_triggers,
|
|
||||||
key=self.key,
|
|
||||||
sx=self.style,
|
|
||||||
id=self.id,
|
|
||||||
class_name=self.class_name,
|
|
||||||
).set(
|
|
||||||
props=tag.format_props(),
|
|
||||||
),
|
|
||||||
cond_state=f"isTrue({self.cond!s})",
|
|
||||||
)
|
|
||||||
|
|
||||||
def add_imports(self) -> ImportDict:
|
|
||||||
"""Add imports for the Cond component.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The import dict for the component.
|
|
||||||
"""
|
|
||||||
var_data = VarData.merge(self.cond._get_all_var_data())
|
|
||||||
|
|
||||||
imports = var_data.old_school_imports() if var_data else {}
|
|
||||||
|
|
||||||
return {**imports, **_IS_TRUE_IMPORT}
|
|
||||||
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def cond(condition: Any, c1: Component, c2: Any = None) -> Component: ...
|
def cond(condition: Any, c1: Component, c2: Any = None) -> Component: ...
|
||||||
|
@ -40,7 +40,7 @@ DEFAULT_LIGHT_ICON: Icon = Icon.create(tag="sun")
|
|||||||
DEFAULT_DARK_ICON: Icon = Icon.create(tag="moon")
|
DEFAULT_DARK_ICON: Icon = Icon.create(tag="moon")
|
||||||
|
|
||||||
|
|
||||||
def icon(
|
def color_mode_icon(
|
||||||
light_component: BaseComponent | None = None,
|
light_component: BaseComponent | None = None,
|
||||||
dark_component: BaseComponent | None = None,
|
dark_component: BaseComponent | None = None,
|
||||||
):
|
):
|
||||||
@ -145,7 +145,7 @@ class ColorModeIconButton(IconButton):
|
|||||||
return dropdown_menu.root(
|
return dropdown_menu.root(
|
||||||
dropdown_menu.trigger(
|
dropdown_menu.trigger(
|
||||||
super().create(
|
super().create(
|
||||||
icon(),
|
color_mode_icon(),
|
||||||
),
|
),
|
||||||
**props,
|
**props,
|
||||||
),
|
),
|
||||||
@ -156,7 +156,7 @@ class ColorModeIconButton(IconButton):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
return IconButton.create(
|
return IconButton.create(
|
||||||
icon(),
|
color_mode_icon(),
|
||||||
on_click=toggle_color_mode,
|
on_click=toggle_color_mode,
|
||||||
**props,
|
**props,
|
||||||
)
|
)
|
||||||
@ -190,7 +190,7 @@ class ColorModeSwitch(Switch):
|
|||||||
class ColorModeNamespace(Var):
|
class ColorModeNamespace(Var):
|
||||||
"""Namespace for color mode components."""
|
"""Namespace for color mode components."""
|
||||||
|
|
||||||
icon = icon
|
icon = color_mode_icon
|
||||||
button = staticmethod(ColorModeIconButton.create)
|
button = staticmethod(ColorModeIconButton.create)
|
||||||
switch = staticmethod(ColorModeSwitch.create)
|
switch = staticmethod(ColorModeSwitch.create)
|
||||||
|
|
||||||
|
@ -114,7 +114,6 @@ class IterTag(Tag):
|
|||||||
"""
|
"""
|
||||||
# Import here to avoid circular imports.
|
# Import here to avoid circular imports.
|
||||||
from reflex.components.base.fragment import Fragment
|
from reflex.components.base.fragment import Fragment
|
||||||
from reflex.components.core.cond import Cond
|
|
||||||
from reflex.components.core.foreach import Foreach
|
from reflex.components.core.foreach import Foreach
|
||||||
|
|
||||||
# Get the render function arguments.
|
# Get the render function arguments.
|
||||||
@ -132,7 +131,7 @@ class IterTag(Tag):
|
|||||||
component = self.render_fn(arg, index)
|
component = self.render_fn(arg, index)
|
||||||
|
|
||||||
# Nested foreach components or cond must be wrapped in fragments.
|
# Nested foreach components or cond must be wrapped in fragments.
|
||||||
if isinstance(component, (Foreach, Cond, Var)):
|
if isinstance(component, (Foreach, Var)):
|
||||||
component = Fragment.create(component)
|
component = Fragment.create(component)
|
||||||
|
|
||||||
# Set the component key.
|
# Set the component key.
|
||||||
|
@ -1445,7 +1445,6 @@ def test_instantiate_all_components():
|
|||||||
# These components all have required arguments and cannot be trivially instantiated.
|
# These components all have required arguments and cannot be trivially instantiated.
|
||||||
untested_components = {
|
untested_components = {
|
||||||
"Card",
|
"Card",
|
||||||
"Cond",
|
|
||||||
"DebounceInput",
|
"DebounceInput",
|
||||||
"Foreach",
|
"Foreach",
|
||||||
"FormControl",
|
"FormControl",
|
||||||
|
@ -31,7 +31,6 @@ from reflex.app import (
|
|||||||
)
|
)
|
||||||
from reflex.components import Component
|
from reflex.components import Component
|
||||||
from reflex.components.base.fragment import Fragment
|
from reflex.components.base.fragment import Fragment
|
||||||
from reflex.components.core.cond import Cond
|
|
||||||
from reflex.components.radix.themes.typography.text import Text
|
from reflex.components.radix.themes.typography.text import Text
|
||||||
from reflex.event import Event, EventHandler
|
from reflex.event import Event, EventHandler
|
||||||
from reflex.middleware import HydrateMiddleware
|
from reflex.middleware import HydrateMiddleware
|
||||||
@ -1228,10 +1227,6 @@ def test_overlay_component(
|
|||||||
assert app.overlay_component is not None
|
assert app.overlay_component is not None
|
||||||
generated_component = app._generate_component(app.overlay_component) # type: ignore
|
generated_component = app._generate_component(app.overlay_component) # type: ignore
|
||||||
assert isinstance(generated_component, OverlayFragment)
|
assert isinstance(generated_component, OverlayFragment)
|
||||||
assert isinstance(
|
|
||||||
generated_component.children[0],
|
|
||||||
Cond, # ConnectionModal is a Cond under the hood
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
assert app.overlay_component is not None
|
assert app.overlay_component is not None
|
||||||
assert isinstance(
|
assert isinstance(
|
||||||
|
Loading…
Reference in New Issue
Block a user