-
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
KeyboardShortcuts: Convert to TypeScript #47429
Conversation
Size Change: +7 B (0%) Total Size: 1.31 MB
ℹ️ View Unchanged
|
bindGlobal, | ||
eventName, | ||
}: KeyboardShortcutsProps ) { | ||
const target = useRef( null ); |
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.
Runtime change. Should be safe.
- const target = useRef();
+ const target = useRef( null );
if ( ! Children.count( children ) ) { | ||
return <>{ element }</>; | ||
} |
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.
Runtime change. Without it, KeyboardShortcuts
basically becomes invalid as a React component.
- return element;
+ return <>{ element }</>;
// @ts-expect-error | ||
event.keyCode = which; | ||
// @ts-expect-error | ||
event.which = which; |
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.
I think these are inevitable, because fireEvent()
requires the event argument to be of type Event
and not the more specific KeyboardEvent
. I'm happy to be wrong 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.
I tried using createEvent
(also imported from @testing-library/react
) locally which seems to work and circumvent the TS error:
function keyPress(
which: number,
target: Parameters< typeof fireEvent >[ 0 ]
) {
[ 'keydown', 'keypress', 'keyup' ].forEach( ( eventName ) => {
const event = createEvent(
eventName,
target,
{
bubbles: true,
keyCode: which,
which,
},
{ EventType: 'KeyboardEvent' }
);
fireEvent( target, event );
} );
}
Although if you'd assign event.which
after the event was created it'd still warn that which
(or keyCode
) doesn't exist on Event
.
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.
TIL, thank you!
type WPKeyboardShortcutConfig = NonNullable< | ||
Parameters< typeof useKeyboardShortcut >[ 2 ] | ||
>; |
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.
A bit weird, but the WPKeyboardShortcutConfig
type is not directly exported from the wp-compose package. I generally want to avoid doing fragile index-based access on Parameters<>
, but 🤷
I've tried to balance readability and DRYness in this types file. Hope it doesn't look too random 😅
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, this looks like a good compromise. The only improvement that comes to mind is to add a little inline comment, basically explaining that the reason why we're doing this, is because that type is not exported.
As a small follow-up PR, we may even consider exporting that type from compose
package and therefore update this line of code too.
Flaky tests detected in 7893c96. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4008594181
|
type WPKeyboardShortcutConfig = NonNullable< | ||
Parameters< typeof useKeyboardShortcut >[ 2 ] | ||
>; |
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, this looks like a good compromise. The only improvement that comes to mind is to add a little inline comment, basically explaining that the reason why we're doing this, is because that type is not exported.
As a small follow-up PR, we may even consider exporting that type from compose
package and therefore update this line of code too.
export type KeyboardShortcutProps = { | ||
shortcut: string | string[]; | ||
/** | ||
* @see {@link https://craig.is/killing/mice Mousetrap documentation} |
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.
Nice!
*/ | ||
import KeyboardShortcuts from '..'; | ||
|
||
const meta: ComponentMeta< typeof KeyboardShortcuts > = { |
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 adding Storybook examples!
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 🚀
CI failures don't seem related.
Co-authored-by: flootr <m.do@posteo.net>
359efa5
to
096bf8f
Compare
Part of #35744
What?
Converts the
KeyboardShortcuts
component to TypeScript.Testing Instructions
npm run storybook:dev
and see the story/docs for KeyboardShortcuts.