From b7579f4d8ddc09504b11f92d0c91c7f65b58abf5 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Fri, 17 Jan 2025 16:27:30 -0800 Subject: [PATCH] why not, remove cond --- reflex/components/component.py | 10 +- reflex/components/core/__init__.py | 2 +- reflex/components/core/__init__.pyi | 1 - reflex/components/core/cond.py | 103 +------------------ reflex/components/radix/themes/color_mode.py | 8 +- reflex/components/tags/iter_tag.py | 3 +- tests/units/components/test_component.py | 1 - tests/units/test_app.py | 5 - 8 files changed, 9 insertions(+), 124 deletions(-) diff --git a/reflex/components/component.py b/reflex/components/component.py index f3f69bbb2..60f024d63 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -932,7 +932,6 @@ class Component(BaseComponent, ABC): """ from reflex.components.base.bare import Bare from reflex.components.base.fragment import Fragment - from reflex.components.core.cond import Cond from reflex.components.core.foreach import Foreach no_valid_parents_defined = all(child._valid_parents == [] for child in children) @@ -944,7 +943,7 @@ class Component(BaseComponent, ABC): return 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): child_name = type(child).__name__ @@ -954,10 +953,6 @@ class Component(BaseComponent, ABC): for c in child.children: validate_child(c) - if isinstance(child, Cond): - validate_child(child.comp1) - validate_child(child.comp2) - if ( isinstance(child, Bare) 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). """ from reflex.components.base.bare import Bare - from reflex.components.core.cond import Cond from reflex.components.core.foreach import Foreach if isinstance(child, Bare): return child.contents - if isinstance(child, Cond): - return child.cond if isinstance(child, Foreach): return child.iterable return child diff --git a/reflex/components/core/__init__.py b/reflex/components/core/__init__.py index c61bf90e3..237dac11a 100644 --- a/reflex/components/core/__init__.py +++ b/reflex/components/core/__init__.py @@ -21,7 +21,7 @@ _SUBMOD_ATTRS: dict[str, list[str]] = { "colors": [ "color", ], - "cond": ["Cond", "color_mode_cond", "cond"], + "cond": ["color_mode_cond", "cond"], "debounce": ["DebounceInput", "debounce_input"], "foreach": [ "foreach", diff --git a/reflex/components/core/__init__.pyi b/reflex/components/core/__init__.pyi index 39971e462..902433d66 100644 --- a/reflex/components/core/__init__.pyi +++ b/reflex/components/core/__init__.pyi @@ -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 .colors import color as color -from .cond import Cond as Cond from .cond import color_mode_cond as color_mode_cond from .cond import cond as cond from .debounce import DebounceInput as DebounceInput diff --git a/reflex/components/core/cond.py b/reflex/components/core/cond.py index e34201c74..fbe91043f 100644 --- a/reflex/components/core/cond.py +++ b/reflex/components/core/cond.py @@ -2,115 +2,16 @@ 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.component import BaseComponent, Component, MemoizationLeaf -from reflex.components.tags import CondTag, Tag -from reflex.constants import Dirs +from reflex.components.component import BaseComponent, Component 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.vars import VarData from reflex.vars.base import LiteralVar, ReflexCallable, Var from reflex.vars.function import ArgsFunctionOperation 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 def cond(condition: Any, c1: Component, c2: Any = None) -> Component: ... diff --git a/reflex/components/radix/themes/color_mode.py b/reflex/components/radix/themes/color_mode.py index 2f46bf72d..dae82903f 100644 --- a/reflex/components/radix/themes/color_mode.py +++ b/reflex/components/radix/themes/color_mode.py @@ -40,7 +40,7 @@ DEFAULT_LIGHT_ICON: Icon = Icon.create(tag="sun") DEFAULT_DARK_ICON: Icon = Icon.create(tag="moon") -def icon( +def color_mode_icon( light_component: BaseComponent | None = None, dark_component: BaseComponent | None = None, ): @@ -145,7 +145,7 @@ class ColorModeIconButton(IconButton): return dropdown_menu.root( dropdown_menu.trigger( super().create( - icon(), + color_mode_icon(), ), **props, ), @@ -156,7 +156,7 @@ class ColorModeIconButton(IconButton): ), ) return IconButton.create( - icon(), + color_mode_icon(), on_click=toggle_color_mode, **props, ) @@ -190,7 +190,7 @@ class ColorModeSwitch(Switch): class ColorModeNamespace(Var): """Namespace for color mode components.""" - icon = icon + icon = color_mode_icon button = staticmethod(ColorModeIconButton.create) switch = staticmethod(ColorModeSwitch.create) diff --git a/reflex/components/tags/iter_tag.py b/reflex/components/tags/iter_tag.py index f3d9c4d8f..076a993e8 100644 --- a/reflex/components/tags/iter_tag.py +++ b/reflex/components/tags/iter_tag.py @@ -114,7 +114,6 @@ class IterTag(Tag): """ # Import here to avoid circular imports. from reflex.components.base.fragment import Fragment - from reflex.components.core.cond import Cond from reflex.components.core.foreach import Foreach # Get the render function arguments. @@ -132,7 +131,7 @@ class IterTag(Tag): component = self.render_fn(arg, index) # 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) # Set the component key. diff --git a/tests/units/components/test_component.py b/tests/units/components/test_component.py index bf2e18140..64b798cf3 100644 --- a/tests/units/components/test_component.py +++ b/tests/units/components/test_component.py @@ -1445,7 +1445,6 @@ def test_instantiate_all_components(): # These components all have required arguments and cannot be trivially instantiated. untested_components = { "Card", - "Cond", "DebounceInput", "Foreach", "FormControl", diff --git a/tests/units/test_app.py b/tests/units/test_app.py index c59b560cc..3a072dc29 100644 --- a/tests/units/test_app.py +++ b/tests/units/test_app.py @@ -31,7 +31,6 @@ from reflex.app import ( ) from reflex.components import Component 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.event import Event, EventHandler from reflex.middleware import HydrateMiddleware @@ -1228,10 +1227,6 @@ def test_overlay_component( assert app.overlay_component is not None generated_component = app._generate_component(app.overlay_component) # type: ignore assert isinstance(generated_component, OverlayFragment) - assert isinstance( - generated_component.children[0], - Cond, # ConnectionModal is a Cond under the hood - ) else: assert app.overlay_component is not None assert isinstance(