Skip to content

Commit

Permalink
Add key mappings for arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKlauss committed Nov 4, 2022
1 parent 21480ff commit bca3de7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/parseHotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { Hotkey, KeyboardModifiers, Keys } from './types'

const reservedModifierKeywords = ['ctrl', 'shift', 'alt', 'meta', 'mod']

const mappedKeys: Record<string, string> = {
esc: 'escape',
return: 'enter',
left: 'arrowleft',
up: 'arrowup',
right: 'arrowright',
down: 'arrowdown',
}

export function parseKeysHookInput(keys: Keys, splitKey: string = ','): string[] {
if (typeof keys === 'string') {
return keys.split(splitKey)
Expand All @@ -15,8 +24,7 @@ export function parseHotkey(hotkey: string, combinationKey: string = '+'): Hotke
.toLocaleLowerCase()
.split(combinationKey)
.map(k => k.trim())
.map(k => k === 'esc' ? 'escape' : k)
.map(k => k === 'return' ? 'enter' : k)
.map(k => mappedKeys[k] || k)

const modifiers: KeyboardModifiers = {
alt: keys.includes('alt'),
Expand Down
32 changes: 26 additions & 6 deletions tests/useHotkeys.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { createEvent, fireEvent, render, screen } from '@testing-library/react'

const wrapper =
(initialScopes: string[]): WrapperComponent<any> =>
({ children }: { children?: ReactNode }) =>
<HotkeysProvider initiallyActiveScopes={initialScopes}>{children}</HotkeysProvider>
({ children }: { children?: ReactNode }) =>
<HotkeysProvider initiallyActiveScopes={initialScopes}>{children}</HotkeysProvider>

type HookParameters = {
keys: Keys
Expand Down Expand Up @@ -48,7 +48,7 @@ test('should log a warning when trying to set a scope without a wrapped provider
renderHook(() => useHotkeys('a', callback, { scopes: 'foo' }))

expect(console.warn).toHaveBeenCalledWith(
'A hotkey has the "scopes" option set, however no active scopes were found. If you want to use the global scopes feature, you need to wrap your app in a <HotkeysProvider>'
'A hotkey has the "scopes" option set, however no active scopes were found. If you want to use the global scopes feature, you need to wrap your app in a <HotkeysProvider>',
)
expect(callback).not.toHaveBeenCalled()
})
Expand Down Expand Up @@ -327,7 +327,7 @@ test('should reflect set splitKey character', async () => {
({ keys, options }) => useHotkeys(keys, callback, options),
{
initialProps: { keys: 'a, b', options: undefined },
}
},
)

await user.keyboard('A')
Expand Down Expand Up @@ -720,7 +720,8 @@ test('should allow named keys like arrow keys, space, enter, backspace, etc.', a
expect(callback).toHaveBeenCalledTimes(7)
})

test.skip('should trigger when used in portals', async () => {})
test.skip('should trigger when used in portals', async () => {
})

test('should parse options and dependencies correctly no matter their position', async () => {
const user = userEvent.setup()
Expand Down Expand Up @@ -965,7 +966,8 @@ test('should call preventDefault option function with hotkey and keyboard event'
const user = userEvent.setup()
const preventDefault = jest.fn()

renderHook(() => useHotkeys('a', async () => {}, { preventDefault }))
renderHook(() => useHotkeys('a', async () => {
}, { preventDefault }))

await user.keyboard('A')

Expand All @@ -990,3 +992,21 @@ test('Should listen to space key', async () => {

expect(callback).toHaveBeenCalledTimes(1)
})

test.each([
['esc', 'Escape'],
['return', 'Enter'],
['left', 'ArrowLeft'],
['up', 'ArrowUp'],
['right', 'ArrowRight'],
['down', 'ArrowDown']
])('Should map key %s to %s', async (hotkey, keyboardKey) => {
const user = userEvent.setup()
const callback = jest.fn()

renderHook(() => useHotkeys(hotkey, callback))

await user.keyboard(`{${keyboardKey}}`)

expect(callback).toHaveBeenCalledTimes(1)
})

0 comments on commit bca3de7

Please sign in to comment.