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