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.
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.
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.
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.
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.
| type WPKeyboardShortcutConfig = NonNullable< | ||
| Parameters< typeof useKeyboardShortcut >[ 2 ] | ||
| >; |
There was a problem hiding this comment.
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.
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.
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} |
| */ | ||
| import KeyboardShortcuts from '..'; | ||
|
|
||
| const meta: ComponentMeta< typeof KeyboardShortcuts > = { |
There was a problem hiding this comment.
Thank you for adding Storybook examples!
ciampo
left a comment
There was a problem hiding this comment.
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
KeyboardShortcutscomponent to TypeScript.Testing Instructions
npm run storybook:devand see the story/docs for KeyboardShortcuts.