Use classes for allowed components (#2662)
This commit is contained in:
parent
ec31f00185
commit
37f66207fc
@ -734,6 +734,11 @@ class Component(BaseComponent, ABC):
|
|||||||
children: The children of the component.
|
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)
|
no_valid_parents_defined = all(child._valid_parents == [] for child in children)
|
||||||
if (
|
if (
|
||||||
not self._invalid_children
|
not self._invalid_children
|
||||||
@ -743,21 +748,23 @@ class Component(BaseComponent, ABC):
|
|||||||
return
|
return
|
||||||
|
|
||||||
comp_name = type(self).__name__
|
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):
|
def validate_child(child):
|
||||||
child_name = type(child).__name__
|
child_name = type(child).__name__
|
||||||
|
|
||||||
# Iterate through the immediate children of fragment
|
# Iterate through the immediate children of fragment
|
||||||
if child_name == "Fragment":
|
if isinstance(child, Fragment):
|
||||||
for c in child.children:
|
for c in child.children:
|
||||||
validate_child(c)
|
validate_child(c)
|
||||||
|
|
||||||
if child_name == "Cond":
|
if isinstance(child, Cond):
|
||||||
validate_child(child.comp1)
|
validate_child(child.comp1)
|
||||||
validate_child(child.comp2)
|
validate_child(child.comp2)
|
||||||
|
|
||||||
if child_name == "Match":
|
if isinstance(child, Match):
|
||||||
for cases in child.match_cases:
|
for cases in child.match_cases:
|
||||||
validate_child(cases[-1])
|
validate_child(cases[-1])
|
||||||
validate_child(child.default)
|
validate_child(child.default)
|
||||||
|
@ -24,10 +24,10 @@ class Cond(MemoizationLeaf):
|
|||||||
cond: Var[Any]
|
cond: Var[Any]
|
||||||
|
|
||||||
# The component to render if the cond is true.
|
# 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.
|
# The component to render if the cond is false.
|
||||||
comp2: BaseComponent = Fragment.create()
|
comp2: BaseComponent = None # type: ignore
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(
|
def create(
|
||||||
|
Loading…
Reference in New Issue
Block a user