main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[d565449] | 1 | import { useMemo } from 'react';
|
---|
| 2 | import useEvent from './useEvent';
|
---|
| 3 | import { noop } from './misc/util';
|
---|
| 4 | var createKeyPredicate = function (keyFilter) {
|
---|
| 5 | return typeof keyFilter === 'function'
|
---|
| 6 | ? keyFilter
|
---|
| 7 | : typeof keyFilter === 'string'
|
---|
| 8 | ? function (event) { return event.key === keyFilter; }
|
---|
| 9 | : keyFilter
|
---|
| 10 | ? function () { return true; }
|
---|
| 11 | : function () { return false; };
|
---|
| 12 | };
|
---|
| 13 | var useKey = function (key, fn, opts, deps) {
|
---|
| 14 | if (fn === void 0) { fn = noop; }
|
---|
| 15 | if (opts === void 0) { opts = {}; }
|
---|
| 16 | if (deps === void 0) { deps = [key]; }
|
---|
| 17 | var _a = opts.event, event = _a === void 0 ? 'keydown' : _a, target = opts.target, options = opts.options;
|
---|
| 18 | var useMemoHandler = useMemo(function () {
|
---|
| 19 | var predicate = createKeyPredicate(key);
|
---|
| 20 | var handler = function (handlerEvent) {
|
---|
| 21 | if (predicate(handlerEvent)) {
|
---|
| 22 | return fn(handlerEvent);
|
---|
| 23 | }
|
---|
| 24 | };
|
---|
| 25 | return handler;
|
---|
| 26 | }, deps);
|
---|
| 27 | useEvent(event, useMemoHandler, target, options);
|
---|
| 28 | };
|
---|
| 29 | export default useKey;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.