@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
45 lines (43 loc) • 1.44 kB
JavaScript
//
// Copyright IBM Corp. 2020, 2020
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//
export var keys = {
SPACE: ' ',
TAB: 'Tab',
CLICK: 'click',
KEYPRESS: 'keypress',
KEYDOWN: 'keydown',
KEYUP: 'keyup',
ENTER: 'Enter'
};
export var handlers = {
onClick: 'onClick',
onKeyDown: 'onKeyDown',
onKeyUp: 'onKeyUp',
onKeyPress: 'onKeyPress'
};
var defaultValidKeys = [keys.CLICK, keys.SPACE, keys.ENTER];
var defaultHandlers = [handlers.onClick, handlers.onKeyDown];
// triggers a callback if event key matches one in validKeys
export var keyAndClickHandler = function keyAndClickHandler(callback) {
var validKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultValidKeys;
return function (evt) {
if (evt && (evt.type || evt.key)) {
if (validKeys.includes(evt.type) || validKeys.includes(evt.key)) {
callback(evt);
}
}
};
};
export var createInteractionHandler = function createInteractionHandler(callback) {
var handlers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultHandlers;
var validKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultValidKeys;
var handler = {};
for (var i = 0; i < handlers.length; i++) {
handler[handlers[i]] = keyAndClickHandler(callback, validKeys);
}
return handler;
};