_valid_children and _invalid_children accessible from class (#2192)
This commit is contained in:
parent
0870658be2
commit
1a83f85783
@ -67,10 +67,10 @@ class Component(Base, ABC):
|
||||
autofocus: bool = False
|
||||
|
||||
# components that cannot be children
|
||||
invalid_children: List[str] = []
|
||||
_invalid_children: List[str] = []
|
||||
|
||||
# components that are only allowed as children
|
||||
valid_children: List[str] = []
|
||||
# only components that are allowed as children
|
||||
_valid_children: List[str] = []
|
||||
|
||||
# custom attribute
|
||||
custom_attrs: Dict[str, str] = {}
|
||||
@ -532,21 +532,21 @@ class Component(Base, ABC):
|
||||
children: The children of the component.
|
||||
|
||||
"""
|
||||
if not self.invalid_children and not self.valid_children:
|
||||
if not self._invalid_children and not self._valid_children:
|
||||
return
|
||||
|
||||
comp_name = type(self).__name__
|
||||
|
||||
def validate_invalid_child(child_name):
|
||||
if child_name in self.invalid_children:
|
||||
if child_name in self._invalid_children:
|
||||
raise ValueError(
|
||||
f"The component `{comp_name}` cannot have `{child_name}` as a child component"
|
||||
)
|
||||
|
||||
def validate_valid_child(child_name):
|
||||
if child_name not in self.valid_children:
|
||||
if child_name not in self._valid_children:
|
||||
valid_child_list = ", ".join(
|
||||
[f"`{v_child}`" for v_child in self.valid_children]
|
||||
[f"`{v_child}`" for v_child in self._valid_children]
|
||||
)
|
||||
raise ValueError(
|
||||
f"The component `{comp_name}` only allows the components: {valid_child_list} as children. Got `{child_name}` instead."
|
||||
@ -555,10 +555,10 @@ class Component(Base, ABC):
|
||||
for child in children:
|
||||
name = type(child).__name__
|
||||
|
||||
if self.invalid_children:
|
||||
if self._invalid_children:
|
||||
validate_invalid_child(name)
|
||||
|
||||
if self.valid_children:
|
||||
if self._valid_children:
|
||||
validate_valid_child(name)
|
||||
|
||||
def _get_custom_code(self) -> str | None:
|
||||
|
@ -65,7 +65,7 @@ class Thead(ChakraComponent):
|
||||
tag = "Thead"
|
||||
|
||||
# invalid children components
|
||||
invalid_children: List[str] = ["Tbody", "Thead", "Tfoot"]
|
||||
_invalid_children: List[str] = ["Tbody", "Thead", "Tfoot"]
|
||||
|
||||
@classmethod
|
||||
def create(cls, *children, headers=None, **props) -> Component:
|
||||
@ -117,7 +117,7 @@ class Tbody(ChakraComponent):
|
||||
tag = "Tbody"
|
||||
|
||||
# invalid children components
|
||||
invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Td", "Th"]
|
||||
_invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Td", "Th"]
|
||||
|
||||
@classmethod
|
||||
def create(cls, *children, rows=None, **props) -> Component:
|
||||
@ -191,7 +191,7 @@ class Tfoot(ChakraComponent):
|
||||
tag = "Tfoot"
|
||||
|
||||
# invalid children components
|
||||
invalid_children: List[str] = ["Tbody", "Thead", "Td", "Th", "Tfoot"]
|
||||
_invalid_children: List[str] = ["Tbody", "Thead", "Td", "Th", "Tfoot"]
|
||||
|
||||
@classmethod
|
||||
def create(cls, *children, footers=None, **props) -> Component:
|
||||
@ -240,7 +240,7 @@ class Tr(ChakraComponent):
|
||||
tag = "Tr"
|
||||
|
||||
# invalid children components
|
||||
invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Tr"]
|
||||
_invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Tr"]
|
||||
|
||||
@classmethod
|
||||
def create(cls, *children, cell_type: str = "", cells=None, **props) -> Component:
|
||||
@ -271,7 +271,7 @@ class Th(ChakraComponent):
|
||||
tag = "Th"
|
||||
|
||||
# invalid children components
|
||||
invalid_children: List[str] = ["Tbody", "Thead", "Tr", "Td", "Th"]
|
||||
_invalid_children: List[str] = ["Tbody", "Thead", "Tr", "Td", "Th"]
|
||||
|
||||
# Aligns the cell content to the right.
|
||||
is_numeric: Var[bool]
|
||||
@ -283,7 +283,7 @@ class Td(ChakraComponent):
|
||||
tag = "Td"
|
||||
|
||||
# invalid children components
|
||||
invalid_children: List[str] = ["Tbody", "Thead"]
|
||||
_invalid_children: List[str] = ["Tbody", "Thead"]
|
||||
|
||||
# Aligns the cell content to the right.
|
||||
is_numeric: Var[bool]
|
||||
|
@ -54,7 +54,7 @@ class Button(ChakraComponent):
|
||||
type_: Var[str]
|
||||
|
||||
# Components that are not allowed as children.
|
||||
invalid_children: List[str] = ["Button", "MenuButton"]
|
||||
_invalid_children: List[str] = ["Button", "MenuButton"]
|
||||
|
||||
# The name of the form field
|
||||
name: Var[str]
|
||||
|
@ -243,7 +243,7 @@ class Area(Cartesian):
|
||||
stack_id: Var[str]
|
||||
|
||||
# Valid children components
|
||||
valid_children: List[str] = ["LabelList"]
|
||||
_valid_children: List[str] = ["LabelList"]
|
||||
|
||||
|
||||
class Bar(Cartesian):
|
||||
|
@ -121,7 +121,7 @@ class MenuButton(ChakraComponent):
|
||||
variant: Var[str]
|
||||
|
||||
# Components that are not allowed as children.
|
||||
invalid_children: List[str] = ["Button", "MenuButton"]
|
||||
_invalid_children: List[str] = ["Button", "MenuButton"]
|
||||
|
||||
# The tag to use for the menu button.
|
||||
as_: Var[str]
|
||||
|
@ -126,9 +126,9 @@ def component5() -> Type[Component]:
|
||||
class TestComponent5(Component):
|
||||
tag = "RandomComponent"
|
||||
|
||||
invalid_children: List[str] = ["Text"]
|
||||
_invalid_children: List[str] = ["Text"]
|
||||
|
||||
valid_children: List[str] = ["Text"]
|
||||
_valid_children: List[str] = ["Text"]
|
||||
|
||||
return TestComponent5
|
||||
|
||||
@ -144,7 +144,7 @@ def component6() -> Type[Component]:
|
||||
class TestComponent6(Component):
|
||||
tag = "RandomComponent"
|
||||
|
||||
invalid_children: List[str] = ["Text"]
|
||||
_invalid_children: List[str] = ["Text"]
|
||||
|
||||
return TestComponent6
|
||||
|
||||
@ -160,7 +160,7 @@ def component7() -> Type[Component]:
|
||||
class TestComponent7(Component):
|
||||
tag = "RandomComponent"
|
||||
|
||||
valid_children: List[str] = ["Text"]
|
||||
_valid_children: List[str] = ["Text"]
|
||||
|
||||
return TestComponent7
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user