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

feat(Tabs): enforce restricted type for children #6767

Merged
merged 3 commits into from
Jan 17, 2022

Conversation

jonkoops
Copy link
Contributor

Closes #6766

@jonkoops jonkoops force-pushed the enforce-tabs-children branch from 1ca97d2 to aecdce3 Compare January 12, 2022 13:14
@patternfly-build
Copy link
Contributor

patternfly-build commented Jan 12, 2022

/** Content rendered inside the tabs component. Must be React.ReactElement<TabProps>[] */
children: React.ReactNode;
/** Content rendered inside the tabs component. */
children: Child | Child[];
Copy link
Contributor

Choose a reason for hiding this comment

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

The way our docs are generated from the props interface, the website is not doing much to describe what the definition of Child is. (see here)
The easiest way to fix this for now would probably be to be more descriptive about the type def of Child in the prop description comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolethoen I've pushed a commit to clarify this a bit WDYT?

@jonkoops jonkoops requested a review from nicolethoen January 12, 2022 15:20
/** Content rendered inside the tabs component. Must be React.ReactElement<TabProps>[] */
children: React.ReactNode;
/** Content rendered inside the tabs component. */
children: Child | Child[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically this is a breaking change. It probably would not impact anyone but we try and not introduce anything that could be breaking like more restrictive type changes. We can pull this update into our next breaking change release.

Copy link
Contributor

Choose a reason for hiding this comment

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

Based on the issue - it sounds like this type enforcement is already necessary - using any other type would already produce a broken tab

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although the type change is technically breaking, the runtime behavior is actually improved. Before if you'd pass in a truthy value the component would attempt to render it, and since there are no props associated with these children this will cause a run-time error (see this example).

This is caused by the fact that before the children were filtered using a boolean filter, which would allow truthy values that are not React elements. In this PR these filters are replaced by isValidElement(), which asserts the value passed is a valid React element created with createElement(), meaning the required props will be present.

So considering that there could realistically not be any applications out there doing this, unless they created a new component that matches all of the required props of Tab I don't see this as a breaking change.

Copy link
Contributor

@tlabaj tlabaj Jan 12, 2022

Choose a reason for hiding this comment

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

@jonkoops I totally agree. I was thinking worst case scenario like you mention in your last point. If they create a component that matches all of the required props of Tab. Highly unlikely though.

@jonkoops jonkoops requested a review from tlabaj January 12, 2022 15:38
@jonkoops
Copy link
Contributor Author

@nicolethoen @tlabaj do we want to go ahead and merge this one? (I don't have permission to do this)

@nicolethoen
Copy link
Contributor

as soon as @tlabaj gives this a thumbs up, she can merge it :)

@tlabaj tlabaj merged commit 0d224df into patternfly:main Jan 17, 2022
@patternfly-build
Copy link
Contributor

Your changes have been released in:

  • eslint-plugin-patternfly-react@4.28.0
  • @patternfly/react-catalog-view-extension@4.40.0
  • @patternfly/react-charts@6.42.0
  • @patternfly/react-code-editor@4.30.0
  • @patternfly/react-console@4.40.0
  • @patternfly/react-core@4.189.0
  • @patternfly/react-docs@5.50.0
  • @patternfly/react-icons@4.40.0
  • @patternfly/react-inline-edit-extension@4.34.0
  • demo-app-ts@4.149.0
  • @patternfly/react-integration@4.151.0
  • @patternfly/react-log-viewer@4.34.0
  • @patternfly/react-styles@4.39.0
  • @patternfly/react-table@4.58.0
  • @patternfly/react-tokens@4.41.0
  • @patternfly/react-topology@4.36.0
  • @patternfly/react-virtualized-extension@4.36.0
  • transformer-cjs-imports@4.27.0

Thanks for your contribution! 🎉

@jonkoops jonkoops deleted the enforce-tabs-children branch January 19, 2022 10:01
kmcfaul added a commit that referenced this pull request Jan 25, 2022
tlabaj pushed a commit that referenced this pull request Jan 26, 2022
@jonkoops
Copy link
Contributor Author

@nicolethoen could you re-open this issue as it's solution was reverted?

@nicolethoen
Copy link
Contributor

I did reopen the associated issue: #6766

@jonkoops
Copy link
Contributor Author

Thanks!

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.

Enforce type for children of Tabs component
4 participants