Skip to content

Commit 75916d9

Browse files
committed
Avoid using deprecated React life cycle methods
1 parent a21d317 commit 75916d9

9 files changed

+122
-90
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,20 @@ For form control elements, React provides with `onKeyDown`, `onKeyPress` and `on
7474
| ----------------------- | -------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
7575
| 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']` |
7676
| 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. |
7878
| isDisabled | Boolean | false | Enable/Disable handling keyboard events |
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 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. |
8080
| 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> |
8181
| 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. |
8282

8383
# Key names and key alias
8484

8585
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))
8686

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.
8889
- 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'.
9091

9192
### Common keys
9293

@@ -143,7 +144,7 @@ You can handle one or more common keys by using an array of their names.
143144

144145
### Modifier keys
145146

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`.
147148

148149
```
149150
<KeyboardEventHandler
@@ -182,11 +183,11 @@ and put all handling logic for each key inside the handler callback function.
182183
| 'numeric' | '0', '1', ....'9 | 10 number keys |
183184
| 'alphanumeric' | 'a'...'z', '0'...'9' | 36 alphanumeric keys |
184185
| 'function' | 'f1'...'f19' | 19 Fn keys |
185-
| '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. |
186187

187188
**Note**:
188189

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.
190191
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`.
191192
1. Alias key names do not work with modifiers. e.g. `handleKeys=['ctrl+numeric'] // doesn't work`
192193
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
202203

203204
There could be other key handlers in your app, they all should be disabled to avoid unexpected results.
204205

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.
206207

207208
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.
208209

@@ -213,7 +214,7 @@ Technically, exclusive handlers are put into a stack upon mounted or when change
213214
# About Higher Order Component
214215

215216
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.
217218

218219
However, if you have a different view on this, please create an issue/request on GitHub.
219220

@@ -225,7 +226,7 @@ Unfortunately, there's no good way for testing keyboard events with [Enzyme](htt
225226

226227
Enzyme has two main limitations (ref: https://github.com/airbnb/enzyme/blob/master/docs/future.md):
227228

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;
229230

230231
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.
231232

@@ -241,7 +242,7 @@ Therefore, when testing with Enzyme:
241242
import simulateEvent from 'simulate-event';
242243
...
243244
244-
it('should be able to handle key events in case insensitive way ', () => {
245+
it('should be able to handle key events in case-insensitive way ', () => {
245246
const handleKeyEvent = Sinon.spy();
246247
render(<KeyboardEventHandler handleKeys={['ctRl + A']} onKeyEvent={handleKeyEvent} />);
247248
simulateEvent.simulate(document.body, 'keydown', { keyCode: 65, ctrlKey: true });

demo/dist/demo.js

Lines changed: 31 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)