-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 refactor useKeyPressEvent hook
Changes its interface, fixes bug of calling callback on initial mount, useKeyPress hook is injected as dependency and can be overwirtten to useKeyboardJs. BREAKING CHANGE: 🧨 useKeyPressEvent hook modified for dependency injection and providing event objects to user
- Loading branch information
Showing
4 changed files
with
31 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,18 @@ | ||
import * as React from 'react'; | ||
const { useEffect } = React; | ||
import useKeyPress from './useKeyPress'; | ||
|
||
type KeyPressCallback = ((targetKey: string) => void) | undefined | null; | ||
import {KeyFilter, Handler} from './useKey'; | ||
import useKeyPressDefault from './useKeyPress'; | ||
import useUpdateEffect from './useUpdateEffect'; | ||
|
||
const useKeyPressEvent = ( | ||
targetKey: string, | ||
onKeyup: KeyPressCallback = undefined, | ||
onKeydown: KeyPressCallback = undefined | ||
key: string | KeyFilter, | ||
keydown?: Handler | null | undefined, | ||
keyup?: Handler | null | undefined, | ||
useKeyPress = useKeyPressDefault, | ||
) => { | ||
const useKeyboardJS: boolean = targetKey.length > 1; | ||
const pressedKeys: boolean = useKeyPress(targetKey); | ||
|
||
if (onKeydown === undefined) { | ||
onKeydown = onKeyup; | ||
onKeyup = null; | ||
} | ||
|
||
useEffect( | ||
() => { | ||
if (!pressedKeys) { | ||
if (onKeyup) onKeyup(targetKey); | ||
return; | ||
} | ||
|
||
if (onKeydown) onKeydown(targetKey); | ||
}, | ||
[pressedKeys] | ||
); | ||
const [pressed, event] = useKeyPress(key); | ||
useUpdateEffect(() => { | ||
if (!pressed && keyup) keyup(event!); | ||
else if (keydown) keydown(event!); | ||
}, [pressed]); | ||
}; | ||
|
||
export default useKeyPressEvent; |