@carbon/react
Version:
React components for the Carbon Design System
37 lines (35 loc) • 1.46 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
//#region src/internal/keyboard/match.ts
/**
* Check to see if at least one key code matches the key code of the
* given event.
*
* @param event - The event to test.
* @param keysToMatch - An array of key definitions to match against.
* @returns `true` if one of the keys matches.
*/
const 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 use the given event.
*
* @param eventOrCode - A `KeyboardEvent`, a number, or a string value.
* @param keyObj - An object with key properties to match against.
* @returns `true` if the event or code matches the key definition.
*/
const match = (eventOrCode, { key, which, keyCode, code }) => {
if (typeof eventOrCode === "string") return eventOrCode === key;
if (typeof eventOrCode === "number") return eventOrCode === which || eventOrCode === keyCode;
if (eventOrCode.key && Array.isArray(key)) return key.includes(eventOrCode.key);
return eventOrCode.key === key || eventOrCode.which === which || eventOrCode.keyCode === keyCode || eventOrCode.code === code;
};
//#endregion
export { match, matches };