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

Check for children of fragments in childrenOfType validator #71

Closed
knowler opened this issue Sep 4, 2020 · 1 comment · Fixed by #72
Closed

Check for children of fragments in childrenOfType validator #71

knowler opened this issue Sep 4, 2020 · 1 comment · Fixed by #72

Comments

@knowler
Copy link
Contributor

knowler commented Sep 4, 2020

I have a component with a sub-component API and I want to validate that the children of the component are a part of that sub-component API. The childrenOfType validator lets me do this (thanks!), but I have discovered an edge case that could be solved with slight modification to the existing validator. The edge case is when a fragment is used to wrap the children. For example, consider the following sub-component API + component prop-types spec:

// Sub-component API
Component.A = A;
Component.B = B;

Component.propTypes = {
  children: childrenOfType(Component.A, Component.B),
};

Now, if I am conditionally rendering the children of the component and a case where multiple child elements are the resulting expression I need to wrap those with a fragment so that it is valid JSX. This causes childrenOfType to err.

<Component>
  {someCondition ? (
    <>
      <Component.A>Some content</Component.A>
      <Component.B>Some more content</Component.B>
    </> 
  ) : (
    <Component.A>
      Some other content
    </Component.A>
  )}
</Component>

I do realize that the above could be solved by writing the conditional differently, however, I do think that it seems reasonable for the childrenOfType validator to recognize a fragment as a non-element and to forward its checking of the children to the fragment. Thoughts?

@ljharb
Copy link
Owner

ljharb commented Sep 4, 2020

That does seem reasonable to me, since a fragment should be equivalent to an array of children. Want to make a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants