You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| handleKeys | Array |[]| An array of keys this handler should handle. <br/> There are also some handy alias for keys, see bellow for details. e.g. `['a', 'b', 'numeric']`|
76
76
| handleEventType | String | keydown | Keyboard event type. <br />This can be 'keyup', 'keydown' or 'keypress'. <br /><sup>\*</sup>**Note**: 'keypress' event only support printable keys. i.e. no support for modifier keys or 'tab', 'enter' etc. |
77
-
| handleFocusableElements | Bool | false | By default, handler only handles key events sourced from `doucment.body`. When this props is set to `true`, it will also handle key events from all focusable elements. This props only apply when there's no children. |
77
+
| handleFocusableElements | Bool | false | By default, handler only handles key events sourced from `doucment.body`. When this props is set to `true`, it will also handle key events from all focusable elements. This props only apply when there are no children. |
| isExclusive | Boolean | false | When set to `true`, all other handler instances are suspended. <br />This is useful for temporary disabling all other keyboard event handlers. <br />For example, for suppressing any other handlers on a page when a modal opens with its own keyboard event handling. |
79
+
| isExclusive | Boolean | false | When set to `true`, all other handler instances are suspended. <br />This is useful for temporary disabling all other keyboard event handlers. <br />For example, for suppressing any other handlers on a page when a modal opens with its keyboard event handling. |
80
80
| onKeyEvent | function |`() => null`| <p>A callback function to call when the handler detects a matched key event.</p><p>The signature of the callback function is: <br />`function(key, event) { ... }`<p><dl><dh>`key`</dh><dd>The key name matches the current keyboard event.</dd><dh>`event`</dh><dd>The native [keyboard event](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent). e.g. you can use `event.keyCode` to get the numeric key code. This is useful for handling keys that are not supported (i.e. does not have a key name defined for the keys).</dd></dl> |
81
81
| children | Any | null | If `KeyboardEventHandler` wraps around any children elements, it will handle and ONLY handle key events sourced from its descendant elements, including any form controls, links or tab-enabled elements. `handleFocusableElements` has no effect when `children` exists. |
82
82
83
83
# Key names and key alias
84
84
85
85
The `handleKeys` prop accepts an array of key names. Key names and key alias free developers from dealing with numeric char codes and/or key codes and browser compatibility issues with `KeyboardEvent.code` and `KeyboardEvent.key`. (Ref: [JavaScript Madness: Keyboard Events](https://unixpapa.com/js/key.html))
86
86
87
-
- Key names are in **LOWER CASE** for consistency. `handleKeys=['a']` will still handles key event for 'A' with caps lock on.
87
+
88
+
- Key names are in **LOWER CASE** for consistency. `handleKeys=['a']` will still handle key event for 'A' with caps lock on.
88
89
- To handle combined keys like `shift` and `a`, use key names in the format of `shift+a`;
89
-
- You can also use key name alias like 'numbers' or 'alphanumeric'.
90
+
- You can also use key name aliases like 'numbers' or 'alphanumeric'.
90
91
91
92
### Common keys
92
93
@@ -143,7 +144,7 @@ You can handle one or more common keys by using an array of their names.
143
144
144
145
### Modifier keys
145
146
146
-
You can handle modifier key combined with a common keys by using key name in the format of `ctrl+a` or `ctrl+shift+a`. To use the `+` common key with modifier keys, use the alias key 'plus'. e.g. `ctrl+plus`.
147
+
You can handle modifier keys combined with a common key by using key name in the format of `ctrl+a` or `ctrl+shift+a`. To use the `+` common key with modifier keys, use the alias key 'plus'. e.g. `ctrl+plus`.
147
148
148
149
```
149
150
<KeyboardEventHandler
@@ -182,11 +183,11 @@ and put all handling logic for each key inside the handler callback function.
| 'all' | n/a |Handle all keyboard events. If a key event does not match any common keys defined above, the `key` parameter to the callback function will have the value of 'other'. You can use the second parameter (the raw key event object) to implemnt you own key handling logic. |
186
+
| 'all' | n/a |All keyboard events. If a key event does not match any common keys defined above, the `key` parameter to the callback function will have the value of 'other'. You can use the second parameter (the raw key event object) to implement you own key handling logic. |
186
187
187
188
**Note**:
188
189
189
-
1. Alias keys are alias to a list of common keys. Expect the same behavior as if the respective array of common key names is in use.
190
+
1. Alias keys are aliases to a list of common keys. Expect the same behavior as if the respective array of common key names is in use.
190
191
1. When a keyboard event matches, the first (`key`) parameter to the callback function will be the matched lowercase common key name. e.g. `a` for alias `numeric`.
191
192
1. Alias key names do not work with modifiers. e.g. `handleKeys=['ctrl+numeric'] // doesn't work`
192
193
1. You can mix alias with common keys. e.g. `handleKeys=['numeric', 'a', 'enter', 'ctrl+b']`
@@ -202,7 +203,7 @@ Otherwise, the user will be navigating the product options in the modal and the
202
203
203
204
There could be other key handlers in your app, they all should be disabled to avoid unexpected results.
204
205
205
-
The `isExclusive` prop can be really helpful in this situation. When a handler set to `isExclusive`, all other key handlers will be suspended.
206
+
The `isExclusive` prop can be helpful in this situation. When a handler set to `isExclusive`, all other key handlers will be suspended.
206
207
207
208
In the above example, the key handler in the modal could set to be `isExclusive`. When the modal opens, all other handlers will be temporarily suspended. When the modal is closed/unmounted, they will be working again.
208
209
@@ -213,7 +214,7 @@ Technically, exclusive handlers are put into a stack upon mounted or when change
213
214
# About Higher Order Component
214
215
215
216
I believe this is not a good use case of HoC.
216
-
I found it hard to come up with a meaningful use case for passing an keyboard event object or the relevant key to a component.
217
+
I found it hard to come up with a meaningful use case for passing a keyboard event object or the relevant key to a component.
217
218
218
219
However, if you have a different view on this, please create an issue/request on GitHub.
219
220
@@ -225,7 +226,7 @@ Unfortunately, there's no good way for testing keyboard events with [Enzyme](htt
225
226
226
227
Enzyme has two main limitations (ref: https://github.com/airbnb/enzyme/blob/master/docs/future.md):
227
228
228
-
1. Event simulation is limited for Shallow rendering. But this component needs `componentWillMount` for registering keyboard events;
229
+
1. Event simulation is limited for Shallow rendering. But this component needs `componentDidMount` for registering keyboard events;
229
230
230
231
2. Event propagation is not supported. However, Key events on wrapped components are bubbled up and handled by at the document level by this component.
231
232
@@ -241,7 +242,7 @@ Therefore, when testing with Enzyme:
241
242
import simulateEvent from 'simulate-event';
242
243
...
243
244
244
-
it('should be able to handle key events in caseinsensitive way ', () => {
245
+
it('should be able to handle key events in case-insensitive way ', () => {
0 commit comments