react-hotkeys-hook 3.4.6 → 4.0.0-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/BoundHotkeysProxyProvider.d.ts +14 -0
- package/dist/HotkeysProvider.d.ts +16 -0
- package/dist/index.d.ts +4 -7
- package/dist/index.js +2 -8
- package/dist/index.js.map +7 -0
- package/dist/parseHotkeys.d.ts +3 -0
- package/dist/types.d.ts +32 -0
- package/dist/useHotkeys.d.ts +2 -18
- package/dist/validators.d.ts +7 -0
- package/package.json +30 -39
- package/src/BoundHotkeysProxyProvider.tsx +23 -0
- package/src/HotkeysProvider.tsx +78 -0
- package/src/index.ts +9 -6
- package/src/isHotkeyPressed.ts +51 -0
- package/src/parseHotkeys.ts +33 -0
- package/src/types.ts +41 -0
- package/src/useHotkeys.ts +89 -77
- package/src/validators.ts +92 -0
- package/dist/react-hotkeys-hook.cjs.development.js +0 -93
- package/dist/react-hotkeys-hook.cjs.development.js.map +0 -1
- package/dist/react-hotkeys-hook.cjs.production.min.js +0 -2
- package/dist/react-hotkeys-hook.cjs.production.min.js.map +0 -1
- package/dist/react-hotkeys-hook.esm.js +0 -87
- package/dist/react-hotkeys-hook.esm.js.map +0 -1
- package/dist/useIsHotkeyPressed.d.ts +0 -7
- package/src/index.test.tsx +0 -230
- package/src/useIsHotkeyPressed.ts +0 -8
package/src/index.test.tsx
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { useHotkeys } from './index';
|
|
3
|
-
import { renderHook } from '@testing-library/react-hooks';
|
|
4
|
-
import { fireEvent, render, screen } from '@testing-library/react';
|
|
5
|
-
import userEvent from '@testing-library/user-event';
|
|
6
|
-
import hotkeys from 'hotkeys-js';
|
|
7
|
-
|
|
8
|
-
const HotkeysOnInput = ({ onPress, useTags }: { onPress: () => void, useTags?: boolean }) => {
|
|
9
|
-
useHotkeys('a', onPress, { enableOnTags: useTags ? ['INPUT'] : undefined });
|
|
10
|
-
|
|
11
|
-
return (
|
|
12
|
-
<input type='text' data-testid={'input'} />
|
|
13
|
-
);
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const HotkeysFilteredOnInput = ({ onPress, useTags }: { onPress: () => void, useTags?: boolean }) => {
|
|
17
|
-
useHotkeys('a', onPress, { enableOnTags: useTags ? ['TEXTAREA'] : undefined });
|
|
18
|
-
|
|
19
|
-
return (
|
|
20
|
-
<input type='text' data-testid={'input'} />
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const HotkeysOnKeyup = ({ onPress, keyup, keydown }: { onPress: () => void, keyup?: boolean, keydown?: boolean }) => {
|
|
25
|
-
useHotkeys('a', onPress, { keyup, keydown });
|
|
26
|
-
|
|
27
|
-
return (
|
|
28
|
-
<input type='text' data-testid={'input'} />
|
|
29
|
-
);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const HotkeysWithRef = ({ onPress }: { onPress: () => void }) => {
|
|
33
|
-
const ref = useHotkeys<HTMLElement>('a', onPress);
|
|
34
|
-
|
|
35
|
-
return (
|
|
36
|
-
<section ref={ref} tabIndex={0} data-testid={'container'}>
|
|
37
|
-
<input type='text' data-testid={'input'} />
|
|
38
|
-
</section>
|
|
39
|
-
);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
test('useHotkeys should only fire when element is focused if a ref is set.', () => {
|
|
43
|
-
const onPress = jest.fn();
|
|
44
|
-
|
|
45
|
-
render(<HotkeysWithRef onPress={onPress} />);
|
|
46
|
-
|
|
47
|
-
userEvent.keyboard('A');
|
|
48
|
-
|
|
49
|
-
expect(onPress).not.toBeCalled();
|
|
50
|
-
|
|
51
|
-
userEvent.click(screen.getByTestId('container'));
|
|
52
|
-
userEvent.keyboard('A');
|
|
53
|
-
|
|
54
|
-
expect(onPress).toBeCalled();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('useHotkeys should listen to key presses', () => {
|
|
58
|
-
const callback = jest.fn();
|
|
59
|
-
|
|
60
|
-
renderHook(() => useHotkeys('a', callback));
|
|
61
|
-
|
|
62
|
-
userEvent.keyboard('A');
|
|
63
|
-
|
|
64
|
-
expect(callback).toHaveBeenCalledTimes(1);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test('useHotkeys correctly assign deps when used as third argument and options being omitted', async () => {
|
|
68
|
-
let count = 0;
|
|
69
|
-
const callback = jest.fn();
|
|
70
|
-
|
|
71
|
-
renderHook(() => useHotkeys('a', () => callback(++count), [count]));
|
|
72
|
-
|
|
73
|
-
userEvent.keyboard('A');
|
|
74
|
-
|
|
75
|
-
expect(callback).toHaveBeenCalledTimes(1);
|
|
76
|
-
expect(callback.mock.calls[0][0]).toEqual(1);
|
|
77
|
-
|
|
78
|
-
userEvent.keyboard('A');
|
|
79
|
-
|
|
80
|
-
expect(callback).toHaveBeenCalledTimes(2);
|
|
81
|
-
expect(callback.mock.calls[1][0]).toEqual(2);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('useHotkeys should use correct char to split combinations', () => {
|
|
85
|
-
const callback = jest.fn();
|
|
86
|
-
|
|
87
|
-
renderHook(() => useHotkeys('Shift-A', callback, { splitKey: '-' }));
|
|
88
|
-
|
|
89
|
-
userEvent.keyboard('{Shift>}A{/Shift}');
|
|
90
|
-
|
|
91
|
-
expect(callback).toHaveBeenCalledTimes(1);
|
|
92
|
-
|
|
93
|
-
userEvent.keyboard('{Shift>}A{/Shift}');
|
|
94
|
-
|
|
95
|
-
expect(callback).toHaveBeenCalledTimes(2);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test('useHotkeys should use correctly assign options and deps argument when using all four arguments', () => {
|
|
99
|
-
const callback = jest.fn();
|
|
100
|
-
|
|
101
|
-
renderHook(() => useHotkeys('shift-a', callback, { splitKey: '-' }, []));
|
|
102
|
-
|
|
103
|
-
userEvent.keyboard('{Shift>}A{/Shift}');
|
|
104
|
-
|
|
105
|
-
expect(callback).toHaveBeenCalledTimes(1);
|
|
106
|
-
|
|
107
|
-
userEvent.keyboard('{Shift>}A{/Shift}');
|
|
108
|
-
|
|
109
|
-
expect(callback).toHaveBeenCalledTimes(2);
|
|
110
|
-
|
|
111
|
-
userEvent.keyboard('{Shift>}A{/Shift}');
|
|
112
|
-
|
|
113
|
-
expect(callback).toHaveBeenCalledTimes(3);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
test('useHotkeys should only trigger once if neither keyup nor keydown are set', () => {
|
|
117
|
-
const onPress = jest.fn();
|
|
118
|
-
|
|
119
|
-
render(<HotkeysOnKeyup onPress={onPress} />);
|
|
120
|
-
|
|
121
|
-
fireEvent.keyUp(document.body, { key: 'a', keyCode: 65 });
|
|
122
|
-
|
|
123
|
-
expect(onPress).not.toHaveBeenCalled();
|
|
124
|
-
|
|
125
|
-
fireEvent.keyDown(document.body, { key: 'a', keyCode: 65 });
|
|
126
|
-
|
|
127
|
-
expect(onPress).toHaveBeenCalled();
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test('useHotkeys should only trigger once if keyup is set and keydown is not', () => {
|
|
131
|
-
const onPress = jest.fn();
|
|
132
|
-
|
|
133
|
-
render(<HotkeysOnKeyup onPress={onPress} keyup={true} />);
|
|
134
|
-
|
|
135
|
-
fireEvent.keyDown(document.body, { key: 'a', keyCode: 65 });
|
|
136
|
-
|
|
137
|
-
expect(onPress).not.toHaveBeenCalled();
|
|
138
|
-
|
|
139
|
-
fireEvent.keyUp(document.body, { key: 'a', keyCode: 65 });
|
|
140
|
-
|
|
141
|
-
expect(onPress).toHaveBeenCalled();
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
test('useHotkeys should trigger twice if keyup and keydown is set to true', () => {
|
|
145
|
-
let called = false;
|
|
146
|
-
|
|
147
|
-
render(<HotkeysOnKeyup onPress={() => called = true} keyup={true} keydown={true} />);
|
|
148
|
-
|
|
149
|
-
userEvent.keyboard('A');
|
|
150
|
-
|
|
151
|
-
expect(called).toBe(true);
|
|
152
|
-
|
|
153
|
-
userEvent.keyboard('A');
|
|
154
|
-
|
|
155
|
-
expect(called).toBe(true);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
test('useHotkeys should be enabled on given form tags', async () => {
|
|
159
|
-
const onPress = jest.fn();
|
|
160
|
-
render(<HotkeysOnInput onPress={onPress} useTags={true} />);
|
|
161
|
-
|
|
162
|
-
const input = document.querySelector('input');
|
|
163
|
-
|
|
164
|
-
expect(input).not.toBe(null);
|
|
165
|
-
|
|
166
|
-
userEvent.keyboard('A');
|
|
167
|
-
|
|
168
|
-
expect(onPress).toHaveBeenCalled();
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
test('useHotkeys should not be enabled on given form tags when filter specifies different input field', async () => {
|
|
172
|
-
const onPress = jest.fn();
|
|
173
|
-
render(<HotkeysFilteredOnInput onPress={onPress} useTags={true} />);
|
|
174
|
-
|
|
175
|
-
userEvent.type(screen.getByRole('textbox'), 'A');
|
|
176
|
-
|
|
177
|
-
expect(onPress).toHaveBeenCalledTimes(0);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
test('useHotkeys should not be enabled on given form tags when tags is not set', async () => {
|
|
181
|
-
const onPress = jest.fn();
|
|
182
|
-
render(<HotkeysFilteredOnInput onPress={onPress} useTags={false} />);
|
|
183
|
-
|
|
184
|
-
userEvent.type(screen.getByRole('textbox'), 'A');
|
|
185
|
-
|
|
186
|
-
expect(onPress).toHaveBeenCalledTimes(0);
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
test('useHotkeys should use its own custom filter system instead of the global hotkeys one', () => {
|
|
190
|
-
const callback = jest.fn();
|
|
191
|
-
const { rerender } = renderHook((returnFilterVal: boolean = false) => useHotkeys('a', callback, { filter: () => returnFilterVal }));
|
|
192
|
-
|
|
193
|
-
userEvent.keyboard('A');
|
|
194
|
-
|
|
195
|
-
expect(callback).not.toHaveBeenCalled();
|
|
196
|
-
|
|
197
|
-
rerender(true);
|
|
198
|
-
|
|
199
|
-
userEvent.keyboard('A');
|
|
200
|
-
|
|
201
|
-
expect(callback).toHaveBeenCalledTimes(1);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
test('useHotkeys should not be enabled when enabled flag is set to false', () => {
|
|
205
|
-
const callback = jest.fn();
|
|
206
|
-
|
|
207
|
-
const { rerender } = renderHook((enabled: boolean = false) => useHotkeys('a', callback, { enabled }));
|
|
208
|
-
|
|
209
|
-
userEvent.keyboard('A');
|
|
210
|
-
|
|
211
|
-
expect(callback).not.toHaveBeenCalled();
|
|
212
|
-
|
|
213
|
-
rerender(true);
|
|
214
|
-
|
|
215
|
-
userEvent.keyboard('A');
|
|
216
|
-
|
|
217
|
-
expect(callback).toHaveBeenCalledTimes(1);
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
test('useHotkeys should unbind the hotkey when enabled is set from true to false', () => {
|
|
221
|
-
hotkeys.unbind = jest.fn();
|
|
222
|
-
|
|
223
|
-
const { rerender } = renderHook((enabled: boolean = true) => useHotkeys('a', () => true, { enabled }));
|
|
224
|
-
|
|
225
|
-
expect(hotkeys.unbind).not.toHaveBeenCalled()
|
|
226
|
-
|
|
227
|
-
rerender(false);
|
|
228
|
-
|
|
229
|
-
expect(hotkeys.unbind).toHaveBeenCalledTimes(2)
|
|
230
|
-
})
|