Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate component children #1647

Merged
merged 5 commits into from
Aug 23, 2023
Merged

Validate component children #1647

merged 5 commits into from
Aug 23, 2023

Conversation

ElijahAhianyo
Copy link
Collaborator

@ElijahAhianyo ElijahAhianyo commented Aug 21, 2023

This PR allows a component to explicitly state only allowed child components

usage:

         class TestComponent5(Component):
              tag = "RandomComponent"
      
              invalid_children: List[str] = ["Table"]
      
              valid_children: List[str] = ["Text", "Box"]

closes #1648 #1588

This PR allows a component to explicitly state only allowed child components
@ElijahAhianyo ElijahAhianyo marked this pull request as ready for review August 21, 2023 14:33
Copy link
Collaborator

@Lendemor Lendemor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add

  • Text and Center as invalid children of Text
  • Tr as the only valid child of Thead, Tbody, and Tfoot
  • TableCaption, Thead, Tbody, and Tfoot as the only valid children of Table

This should help us reduce incidence of hydrations errors from silly mistakes

Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Some comments but I don't think they're critical, we can come back to them in a follow up

@@ -435,30 +443,44 @@ def render(self) -> Dict:
),
autofocus=self.autofocus,
)
self._validate_component_children(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we keep the check here? Only reason is because if they somehow added children dynamically after creating their component, then we wouldn't catch it. Though I don't think I've seen people really do that...


comp_name = type(self).__name__

def validate_invalid_child(child_name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we could inline these functions ore define them outside of the function. I've noticed before in Python that defining functions within tight loops is often a performance issue, and these checks will be called many times per compile.

@@ -66,6 +66,10 @@ class Component(Base, ABC):

# components that cannot be children
invalid_children: List[str] = []

# components that are only allowed as children
valid_children: List[str] = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also thinking in a follow up if these should maybe be List[Type[Component]] rather than str

@picklelo picklelo merged commit 217a580 into main Aug 23, 2023
@picklelo picklelo deleted the elijah/valid_components branch September 16, 2023 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Valid children of component
5 participants