Skip to content

Commit

Permalink
Fixed bug in 2.1.0 that every filter returns false that has no availa…
Browse files Browse the repository at this point in the history
…ble tags given.
  • Loading branch information
JohannesKlauss committed Apr 5, 2020
1 parent dfad9c5 commit 575a652
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
15 changes: 1 addition & 14 deletions docs/useHotkeys.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,6 @@ to listen to the `+` character, you can set `splitKey` to i.e. `-` and listen fo
* `keyup: boolean` Determine if you want to listen on the keyup event
* `keydown: boolean` Determine if want to listen on the keydown event

<Playground>
{() => {
const [amount, setAmount] = useState(0);
useHotkeys('ctrl+t', () => setAmount(amount => amount + 100), {enableOnTags: ['INPUT']});
return (
<div>
<input type="text"/>
Add {amount} dollars to my bank account.
</div>
);
}}
</Playground>

## Memoisation

Let's check out a slightly different example to see how memoisation effects your application.
Expand Down Expand Up @@ -139,7 +126,7 @@ approach for `setAmount` like in the very first example:
<Playground>
{() => {
const [amount, setAmount] = useState(0);
useHotkeys('m', () => setAmount(amount => amount + 100));
useHotkeys('l', () => setAmount(amount => amount + 100));
return (
<div>
Add {amount} dollars to my bank account.
Expand Down
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import hotkeys, {KeyHandler} from "hotkeys-js";
import {useCallback, useEffect} from "react";
import {useCallback, useEffect, useMemo} from "react";

type AvailableTags = 'INPUT' | 'TEXTAREA' | 'SELECT';

Expand All @@ -21,16 +21,14 @@ export function useHotkeys(
const memoisedCallback = useCallback(callback, deps);

useEffect(() => {
hotkeys.filter = ({target, srcElement}) => {
if (options.enableOnTags) {
if (options.enableOnTags) {
hotkeys.filter = ({target, srcElement}) => {
// @ts-ignore
const targetTagName = (target && target.tagName) || (srcElement && srcElement.tagName);

return Boolean(targetTagName && options.enableOnTags.includes(targetTagName as AvailableTags));
}

return false;
};
return Boolean(targetTagName && options.enableOnTags && options.enableOnTags.includes(targetTagName as AvailableTags));
};
}

if (options.filter) hotkeys.filter = options.filter;

Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"compilerOptions": {
"target": "es2015",
"module": "esnext",
"target": "ES5",
"module": "ESNext",
"moduleResolution": "node",
"jsx": "react",
"strict": true,
"allowSyntheticDefaultImports": true
},
"include": ["src"]
}
}

0 comments on commit 575a652

Please sign in to comment.