-
-
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.
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { storiesOf } from "@storybook/react"; | ||
import * as React from "react"; | ||
import {useKey} from ".."; | ||
import ShowDocs from "../util/ShowDocs"; | ||
import {CenterStory} from "./util/CenterStory"; | ||
|
||
const Demo = () => { | ||
const [count, setCount] = React.useState(0); | ||
|
||
const increment = () => setCount(count => ++count); | ||
const decrement = () => setCount(count => --count); | ||
const reset = () => setCount(count => 0); | ||
|
||
useKey(']', increment); | ||
useKey('[', decrement); | ||
useKey('r', reset); | ||
|
||
return ( | ||
<CenterStory> | ||
<style dangerouslySetInnerHTML={{__html: `code {color: red}`}} /> | ||
<p> | ||
Try pressing <code>[</code>, <code>]</code>, and <code>r</code> to | ||
see the count incremented and decremented.</p> | ||
<p>Count: {count}</p> | ||
</CenterStory> | ||
); | ||
}; | ||
|
||
storiesOf("Sensors/useKey", module) | ||
.add("Docs", () => <ShowDocs md={require("../../docs/useKeyPressEvent.md")} />) | ||
.add("Demo", () => <Demo />); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {useMemo, DependencyList} from 'react'; | ||
import useEvent, {UseEventTarget} from './useEvent'; | ||
|
||
export type KeyPredicate = (event: KeyboardEvent) => boolean; | ||
export type KeyFilter = null | undefined | string | ((event: KeyboardEvent) => boolean); | ||
export type Handler = (event: KeyboardEvent) => void; | ||
export interface UseKeyOptions { | ||
event?: 'keydown' | 'keypress' | 'keyup', | ||
target?: UseEventTarget, | ||
options?: any, | ||
} | ||
|
||
const noop = () => {}; | ||
const createKeyPredicate = (keyFilter: KeyFilter): KeyPredicate => | ||
typeof keyFilter === 'function' | ||
? keyFilter | ||
: typeof keyFilter === 'string' | ||
? (event: KeyboardEvent) => event.key === keyFilter | ||
: keyFilter | ||
? () => true | ||
: () => false; | ||
|
||
const useKey = (key: KeyFilter, fn: Handler = noop, opts: UseKeyOptions = {}, deps: DependencyList = [key]) => { | ||
const {event = 'keydown', target, options} = opts; | ||
const handler = useMemo(() => { | ||
const predicate: KeyPredicate = createKeyPredicate(key); | ||
const handler: Handler = (event) => { | ||
if (predicate(event)) return fn(event); | ||
}; | ||
return handler; | ||
}, deps); | ||
useEvent(event, handler, target, options); | ||
}; | ||
|
||
export default useKey; |