[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | var tslib_1 = require("tslib");
|
---|
| 4 | var react_1 = require("react");
|
---|
| 5 | var useEvent_1 = tslib_1.__importDefault(require("./useEvent"));
|
---|
| 6 | var util_1 = require("./misc/util");
|
---|
| 7 | var 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 | };
|
---|
| 16 | var 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 | };
|
---|
| 32 | exports.default = useKey;
|
---|