-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
BaseControl: Migrate to TypeScript #39468
Conversation
Size Change: +4 B (0%) Total Size: 1.21 MB
ℹ️ View Unchanged
|
export const WithVisualLabel: ComponentStory< typeof BaseControl > = ( | ||
props | ||
) => { | ||
BaseControl.VisualLabel.displayName = 'BaseControl.VisualLabel'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, when I try to set this display name in the base-control/index.tsx
file instead of here, the docgen refuses to extract types from BaseControl
🤷 I don't think we'll run into this problem a lot though. Also I guess it isn't too weird that we're setting the display name here, because it's mainly to make the Storybook code snippet understandable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be related to the subcomponents
bug that you mentioned before?
the docgen refuses to extract types from
BaseControl
What do you mean by that? I tried to comment this line out but didn't notice any changes in Storybook (although I'm probably missing something)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be related to the
subcomponents
bug that you mentioned before?
My hunch is that it's more related to the thing where we need to export
the component individually as a named export (e.g. here) for the docgen to extract prop types. If you try to change a property on that exported module directly in the same file (e.g. ConnectedHeading.displayName = 'foo'
), then the export
suddenly stops doing its magic.
🙂 Docgen works as expected
// component.tsx
export const MyComponent = /* ... */;
// consumer.tsx
import { MyComponent } from "./component";
MyComponent.displayName = "Hello";
☠️ Docgen ignores my prop types
// component.tsx
export const MyComponent = /* ... */;
MyComponent.displayName = "Hello";
I'm not familiar enough with the internals to say why this happens, but to someone who is familiar, I bet the reason is quite obvious!
f1eade4
to
181d3a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this, @mirka !
I left some comments, but my main piece of feedback is if you have you considered using the WordPressComponentProps
type, instead of FunctionComponent
? We tend to use this type for the majority of our TypeScript components.
The new Storybook examples are great!
If I understand correctly, this type shouldn't be used if the component isn't forwarding its rest props to the given element type. So like |
Thanks for the review! All points have been addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🚀 Feel free to merge once the remaining comments are addressed.
Hopefully we'll be soon able to convert the unit tests to TypeScript too (#39436)!
will imply that all div props are allowed, for example style. And that would be incorrect in this case.
This is indeed a fair point. We can look into migrating to WordPressComponentProps
at a later stage if/when needed.
export const WithVisualLabel: ComponentStory< typeof BaseControl > = ( | ||
props | ||
) => { | ||
BaseControl.VisualLabel.displayName = 'BaseControl.VisualLabel'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be related to the subcomponents
bug that you mentioned before?
the docgen refuses to extract types from
BaseControl
What do you mean by that? I tried to comment this line out but didn't notice any changes in Storybook (although I'm probably missing something)
Probably related: styleguidist/react-docgen-typescript#339 Noting that I hit an interesting issue where the For context: When the |
This matches the readme and common-sense usage of this component
This reverts commit 6e59dbe480f162683504cdb3c4fa691e6e43d0d6. # Conflicts: # packages/components/src/base-control/types.ts
Without this, the docgen is confused and fails to mark `children` as required
e90416d
to
e6fa3dd
Compare
Part of #35744
What?
Converts the
BaseControl
component to TypeScript.Why?
Improves devex and code quality through type safety, IDE type hints, and better Storybook documentation.
How?
Testing Instructions
npm run storybook:dev
and check the Docs tab forBaseControl
. It should be useful and understandable.For easier code review, exclude the commit where I just changed the file extensions.
✅ Tests pass