UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

82 lines (75 loc) 1.9 kB
/** * MSKCC 2021, 2024 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); /** * @typedef Key * @property key {Array<string>|string} * @property which {number} * @property keyCode {number} */ /** * Check to see if at least one key code matches the key code of the * given event. * * @example * import * as keys from '../keys'; * import { matches } from '../match'; * * function handleOnKeyDown(event) { * if (matches(event, [keys.Enter, keys.Space]) { * // ... * } * } * * @param {Event|React.SyntheticEvent} event * @param {Array<Key>} keysToMatch * @returns {boolean} */ function matches(event, keysToMatch) { for (let i = 0; i < keysToMatch.length; i++) { if (match(event, keysToMatch[i])) { return true; } } return false; } /** * Check to see if the given key matches the corresponding keyboard event. Also * supports passing in the value directly if you can't used the given event. * * @example * import * as keys from '../keys'; * import { matches } from '../match'; * * function handleOnKeyDown(event) { * if (match(event, keys.Enter) { * // ... * } * } * * @param {React.SyntheticEvent|Event|number|string} eventOrCode * @param {Key} key * @returns {boolean} */ function match(eventOrCode) { let { key, which, keyCode, code } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (typeof eventOrCode === 'string') { return eventOrCode === key; } if (typeof eventOrCode === 'number') { return eventOrCode === which || eventOrCode === keyCode; } if (eventOrCode.key && Array.isArray(key)) { return key.indexOf(eventOrCode.key) !== -1; } return eventOrCode.key === key || eventOrCode.which === which || eventOrCode.keyCode === keyCode || eventOrCode.code === code; } exports.match = match; exports.matches = matches;