diff --git a/src/index.test.tsx b/src/index.test.tsx index b5b9b792..a6c06664 100644 --- a/src/index.test.tsx +++ b/src/index.test.tsx @@ -164,7 +164,7 @@ const HotkeysWithRef = ({onPress}: { onPress: () => void }) => { const ref = useHotkeys('a', onPress); return ( -
+
); @@ -186,7 +186,10 @@ test('useHotkeys should only fire when element is focused if a ref is set.', asy expect(called).toBe(false); reactAct(() => { - section!.focus() + section!.focus(); + }); + + act(() => { fireEvent.keyDown(section!, {key: 'a', keyCode: 65}); }); diff --git a/src/index.ts b/src/index.ts index 8410f763..27a26a4c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,19 +21,18 @@ export function useHotkeys(keys: string, callback: KeyHandler deps = options; options = undefined; } - const {enableOnTags, filter} = options || {}; const ref = useRef(null); + const memoisedCallback = useCallback((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => { if (ref.current === null || document.activeElement === ReactDOM.findDOMNode(ref.current)) { callback(keyboardEvent, hotkeysEvent); - return true; } return false; - }, deps && deps.concat(ref) || [ref]); + }, deps ? [ref, ...deps] : [ref]); useEffect(() => { if (options && (options as Options).enableOnTags) { @@ -50,7 +49,7 @@ export function useHotkeys(keys: string, callback: KeyHandler hotkeys(keys, (options as Options) || {}, memoisedCallback); return () => hotkeys.unbind(keys, memoisedCallback); - }, [memoisedCallback, options]); + }, [memoisedCallback, options, enableOnTags, filter, keys]); return ref; -} +} \ No newline at end of file