1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import * as React from "react";
|
18 |
|
19 | import * as Errors from "../../common/errors";
|
20 | import { isNodeEnv } from "../../common/utils";
|
21 | import { HotkeyConfig, useHotkeys, UseHotkeysOptions } from "../../hooks";
|
22 |
|
23 |
|
24 | export interface HotkeysTarget2RenderProps {
|
25 | handleKeyDown: React.KeyboardEventHandler<HTMLElement>;
|
26 | handleKeyUp: React.KeyboardEventHandler<HTMLElement>;
|
27 | }
|
28 |
|
29 | export interface HotkeysTarget2Props {
|
30 | |
31 |
|
32 |
|
33 |
|
34 | children: JSX.Element | ((props: HotkeysTarget2RenderProps) => JSX.Element);
|
35 |
|
36 | /** Hotkey definitions. */
|
37 | hotkeys: HotkeyConfig[];
|
38 |
|
39 | /** Hook customization options. */
|
40 | options?: UseHotkeysOptions;
|
41 | }
|
42 |
|
43 | /**
|
44 | * Utility component which allows consumers to use the new `useHotkeys` hook inside
|
45 | * React component classes. The implementation simply passes through to the hook.
|
46 | */
|
47 | export const HotkeysTarget2 = ({ children, hotkeys, options }: HotkeysTarget2Props): JSX.Element => {
|
48 | const { handleKeyDown, handleKeyUp } = useHotkeys(hotkeys, options);
|
49 |
|
50 |
|
51 | React.useEffect(() => {
|
52 | if (!isNodeEnv("production")) {
|
53 | if (typeof children !== "function" && hotkeys.some(h => !h.global)) {
|
54 | console.error(Errors.HOTKEYS_TARGET2_CHILDREN_LOCAL_HOTKEYS);
|
55 | }
|
56 | }
|
57 | }, [hotkeys]);
|
58 |
|
59 | if (typeof children === "function") {
|
60 | return children({ handleKeyDown, handleKeyUp });
|
61 | } else {
|
62 | return children;
|
63 | }
|
64 | };
|