source: imaps-frontend/node_modules/react-use/lib/useKey.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.2 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var react_1 = require("react");
5var useEvent_1 = tslib_1.__importDefault(require("./useEvent"));
6var util_1 = require("./misc/util");
7var createKeyPredicate = function (keyFilter) {
8 return typeof keyFilter === 'function'
9 ? keyFilter
10 : typeof keyFilter === 'string'
11 ? function (event) { return event.key === keyFilter; }
12 : keyFilter
13 ? function () { return true; }
14 : function () { return false; };
15};
16var useKey = function (key, fn, opts, deps) {
17 if (fn === void 0) { fn = util_1.noop; }
18 if (opts === void 0) { opts = {}; }
19 if (deps === void 0) { deps = [key]; }
20 var _a = opts.event, event = _a === void 0 ? 'keydown' : _a, target = opts.target, options = opts.options;
21 var useMemoHandler = react_1.useMemo(function () {
22 var predicate = createKeyPredicate(key);
23 var handler = function (handlerEvent) {
24 if (predicate(handlerEvent)) {
25 return fn(handlerEvent);
26 }
27 };
28 return handler;
29 }, deps);
30 useEvent_1.default(event, useMemoHandler, target, options);
31};
32exports.default = useKey;
Note: See TracBrowser for help on using the repository browser.