Selaa lähdekoodia

Use classes for allowed components (#2662)

Elijah Ahianyo 1 vuosi sitten
vanhempi
säilyke
37f66207fc
2 muutettua tiedostoa jossa 13 lisäystä ja 6 poistoa
  1. 11 4
      reflex/components/component.py
  2. 2 2
      reflex/components/core/cond.py

+ 11 - 4
reflex/components/component.py

@@ -734,6 +734,11 @@ class Component(BaseComponent, ABC):
             children: The children of the component.
 
         """
+        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.match import Match
+
         no_valid_parents_defined = all(child._valid_parents == [] for child in children)
         if (
             not self._invalid_children
@@ -743,21 +748,23 @@ class Component(BaseComponent, ABC):
             return
 
         comp_name = type(self).__name__
-        allowed_components = ["Fragment", "Foreach", "Cond", "Match"]
+        allowed_components = [
+            comp.__name__ for comp in (Fragment, Foreach, Cond, Match)
+        ]
 
         def validate_child(child):
             child_name = type(child).__name__
 
             # Iterate through the immediate children of fragment
-            if child_name == "Fragment":
+            if isinstance(child, Fragment):
                 for c in child.children:
                     validate_child(c)
 
-            if child_name == "Cond":
+            if isinstance(child, Cond):
                 validate_child(child.comp1)
                 validate_child(child.comp2)
 
-            if child_name == "Match":
+            if isinstance(child, Match):
                 for cases in child.match_cases:
                     validate_child(cases[-1])
                 validate_child(child.default)

+ 2 - 2
reflex/components/core/cond.py

@@ -24,10 +24,10 @@ class Cond(MemoizationLeaf):
     cond: Var[Any]
 
     # The component to render if the cond is true.
-    comp1: BaseComponent = Fragment.create()
+    comp1: BaseComponent = None  # type: ignore
 
     # The component to render if the cond is false.
-    comp2: BaseComponent = Fragment.create()
+    comp2: BaseComponent = None  # type: ignore
 
     @classmethod
     def create(