Skip to content
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

Fix: duplicate hotkey activiation #1080

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey,
const keyCode = mapKey(code)
const pressedKey = pressedKeyUppercase.toLowerCase()

if (!keys?.includes(keyCode) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(keyCode)) {
return false;
}

if (!ignoreModifiers) {
// We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.
if (alt === !altKey && pressedKey !== 'alt') {
Expand Down
32 changes: 32 additions & 0 deletions tests/useHotkeys.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,38 @@ test('should listen to multiple hotkeys', async () => {
expect(callback).toHaveBeenCalledTimes(2)
})

test('should be able to always output correct keys on multiple hotkeys', async () => {
const user = userEvent.setup()

const callbackA = jest.fn()
const callbackB = jest.fn()

renderHook(() => useHotkeys(['a'], callbackA))
renderHook(() => useHotkeys(['b'], callbackB))

await user.keyboard('{A>}')

expect(callbackA).toHaveBeenCalledTimes(1)

await user.keyboard('B')
expect(callbackA).toHaveBeenCalledTimes(1)
expect(callbackB).toHaveBeenCalledTimes(1)

await user.keyboard('C')

expect(callbackA).toHaveBeenCalledTimes(1)
expect(callbackB).toHaveBeenCalledTimes(1)

await user.keyboard('B')
expect(callbackA).toHaveBeenCalledTimes(1)
expect(callbackB).toHaveBeenCalledTimes(2)

await user.keyboard('{/A}')
expect(callbackA).toHaveBeenCalledTimes(1)
expect(callbackB).toHaveBeenCalledTimes(2)

})

test('should be able to parse first argument as string, array or readonly array', async () => {
const user = userEvent.setup()
const callback = jest.fn()
Expand Down