Use classes for allowed components (#2662)

This commit is contained in:
Elijah Ahianyo 2024-02-20 22:40:48 +00:00 committed by GitHub
parent ec31f00185
commit 37f66207fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 6 deletions

View File

@ -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)

View File

@ -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(