-
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
Refactor Button
component to TypeScript
#46997
Conversation
Size Change: +66 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
Flaky tests detected in dfef117. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3999561501
|
This reverts commit 0b616c7.
@@ -166,7 +166,7 @@ const BorderControlDropdown = ( | |||
onClick={ onToggle } | |||
variant="tertiary" | |||
aria-label={ toggleAriaLabel } | |||
position={ dropdownPosition } | |||
tooltipPosition={ dropdownPosition } |
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.
<Button>
has no position
prop, so maybe this should be tooltipPosition
.
This also has a showTooltip
prop below.
As Lena mentioned, the intent of this test looks like ensuring that additional props are passed to the element.
// @ts-expect-error | ||
render( <Button type="invalidtype" /> ); | ||
// @ts-expect-error - although the runtime behavior will allow this to be an anchor, this is probably a mistake. | ||
render( <Button disabled __experimentalIsFocusable href="foo" /> ); |
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.
If it's alright, this is basically copy-pasted from your #46997 (review).
There's no expect()
call, which is a little strange.
But it's not testing rendering, only typing.
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.
Right. Also it doesn't even need to be in a render()
call 😄 I don't think there's an established convention for this kind of static TS test, so I'm just going freestyle (for example these).
To take it further, it doesn't even need to be in describe()
or it()
calls, but I'm trying to stay somewhat in the realm of normal unit tests to at least signal that it's some kind of test, just so other devs aren't overly confused.
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.
Yeah, it'd definitely be simpler to not wrap them in render()
and it()
.
I can't get that to work, without getting 'JSX expressions must have one parent element':
describe( 'static typing', () => {
<Button href="foo" />
// @ts-expect-error - `target` requires `href`
<Button target="foo" />
// @ts-expect-error - `disabled` is only for buttons
<Button href="foo" disabled />
<Button href="foo" type="image/png" />
// @ts-expect-error - if button, type must be submit/reset/button
<Button type="image/png" />;
// @ts-expect-error
<Button type="invalidtype" />
// @ts-expect-error - although the runtime behavior will allow this to be an anchor, this is probably a mistake.
<Button disabled __experimentalIsFocusable href="foo" />
} );
The examples you linked to work, though 😄
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.
Lol. We can use a Fragment, I guess?
describe( 'static typing', () => {
<>
<Button href="foo" />
{ /* @ts-expect-error - `target` requires `href` */ }
<Button target="foo" />
{ /* @ts-expect-error - `disabled` is only for buttons */ }
<Button href="foo" disabled />
<Button href="foo" type="image/png" />
{ /* @ts-expect-error - if button, type must be submit/reset/button */ }
<Button type="image/png" />
{ /* @ts-expect-error */ }
<Button type="invalidtype" />
{ /* @ts-expect-error - although the runtime behavior will allow this to be an anchor, this is probably a mistake. */ }
<Button disabled __experimentalIsFocusable href="foo" />
</>;
} );
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.
Ah, good idea.
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.
Thanks, committed in dfef117
Good idea! I cherry-picked your 165ef21 with only a name change, and copy-pasted your static TypeScript tests. |
Hi @mirka, Thanks for all the care and code you put into this. |
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.
Really great work here. I'm happy we finally have types for possibly the single most widely used component in the package!
// @ts-expect-error | ||
render( <Button type="invalidtype" /> ); | ||
// @ts-expect-error - although the runtime behavior will allow this to be an anchor, this is probably a mistake. | ||
render( <Button disabled __experimentalIsFocusable href="foo" /> ); |
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.
Right. Also it doesn't even need to be in a render()
call 😄 I don't think there's an established convention for this kind of static TS test, so I'm just going freestyle (for example these).
To take it further, it doesn't even need to be in describe()
or it()
calls, but I'm trying to stay somewhat in the realm of normal unit tests to at least signal that it's some kind of test, just so other devs aren't overly confused.
…es/index.tsx Co-authored-by: Lena Morita <[email protected]>
Thanks so much, Lena! Just responded to both of your comments. |
Going to merge this in 30 minutes. |
Just passing by to thank you, Ryan! We really appreciate your contributions to the project and the components library 🚀 |
Thanks so much, Marco! |
(Ready for review)
What?
Convert the
Button
component to TypeScriptWhy?
As part of an effort to convert
@wordpress/components
to TypeScriptHow?
Mainly by adding types to the
Button
componentTesting Instructions
npm run storybook:dev
Screenshots