From 1b42224f8f0fa259f82925b7bd1df7e2530c6d12 Mon Sep 17 00:00:00 2001 From: Amal K Joy <153802538+amal-k-joy@users.noreply.github.com> Date: Mon, 19 Feb 2024 19:48:42 +0530 Subject: [PATCH] fix(Dataspreadsheet): Reduce duplication with isHoldingCommandKey #4188 (#4227) --- .../DataSpreadsheet/DataSpreadsheetBody.js | 7 ++++--- .../DataSpreadsheet/DataSpreadsheetHeader.js | 5 +++-- .../DataSpreadsheet/utils/checkForHoldingKey.js | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 packages/ibm-products/src/components/DataSpreadsheet/utils/checkForHoldingKey.js diff --git a/packages/ibm-products/src/components/DataSpreadsheet/DataSpreadsheetBody.js b/packages/ibm-products/src/components/DataSpreadsheet/DataSpreadsheetBody.js index 8604024e2f..fc8787509a 100644 --- a/packages/ibm-products/src/components/DataSpreadsheet/DataSpreadsheetBody.js +++ b/packages/ibm-products/src/components/DataSpreadsheet/DataSpreadsheetBody.js @@ -30,6 +30,7 @@ import { handleHeaderCellSelection } from './utils/handleHeaderCellSelection'; import { getSpreadsheetWidth } from './utils/getSpreadsheetWidth'; import { useSpreadsheetMouseUp } from './hooks'; +import { checkForHoldingKey } from './utils/checkForHoldingKey'; const blockClass = `${pkg.prefix}--data-spreadsheet`; @@ -277,8 +278,8 @@ export const DataSpreadsheetBody = forwardRef( `${blockClass}__body--td` ); setValidStartingPoint(isValidSelectionAreaStart); - const isHoldingCommandKey = event.metaKey || event.ctrlKey; - const isHoldingShiftKey = event.shiftKey; + const isHoldingCommandKey = checkForHoldingKey(event, 'cmd'); + const isHoldingShiftKey = checkForHoldingKey(event, 'shiftKey'); setContainerHasFocus(true); const activeCoordinates = { row: cell.row.index, @@ -407,7 +408,7 @@ export const DataSpreadsheetBody = forwardRef( const handleRowHeaderClick = useCallback( (index) => { return (event) => { - const isHoldingCommandKey = event.metaKey || event.ctrlKey; + const isHoldingCommandKey = checkForHoldingKey(event, 'cmd'); handleHeaderCellSelection({ type: 'row', activeCellCoordinates, diff --git a/packages/ibm-products/src/components/DataSpreadsheet/DataSpreadsheetHeader.js b/packages/ibm-products/src/components/DataSpreadsheet/DataSpreadsheetHeader.js index 4a896c27c3..7a2e2ce7db 100644 --- a/packages/ibm-products/src/components/DataSpreadsheet/DataSpreadsheetHeader.js +++ b/packages/ibm-products/src/components/DataSpreadsheet/DataSpreadsheetHeader.js @@ -17,6 +17,7 @@ import { handleHeaderCellSelection } from './utils/handleHeaderCellSelection'; import { selectAllCells } from './utils/selectAllCells'; import { getSpreadsheetWidth } from './utils/getSpreadsheetWidth'; import { useSpreadsheetMouseMove } from './hooks'; +import { checkForHoldingKey } from './utils/checkForHoldingKey'; const blockClass = `${pkg.prefix}--data-spreadsheet`; @@ -63,8 +64,8 @@ export const DataSpreadsheetHeader = forwardRef( const handleColumnHeaderClick = (index) => { return (event) => { - const isHoldingCommandKey = event.metaKey || event.ctrlKey; - const isHoldingShiftKey = event.shiftKey; + const isHoldingCommandKey = checkForHoldingKey(event, 'cmd'); + const isHoldingShiftKey = checkForHoldingKey(event, 'shiftKey'); handleHeaderCellSelection({ type: 'column', activeCellCoordinates, diff --git a/packages/ibm-products/src/components/DataSpreadsheet/utils/checkForHoldingKey.js b/packages/ibm-products/src/components/DataSpreadsheet/utils/checkForHoldingKey.js new file mode 100644 index 0000000000..9c9431feed --- /dev/null +++ b/packages/ibm-products/src/components/DataSpreadsheet/utils/checkForHoldingKey.js @@ -0,0 +1,15 @@ +/** + * Copyright IBM Corp. 2022, 2022 + * + * 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 const checkForHoldingKey = (event, key) => { + // possible key inputs: altKey,ctrlKey,metaKey,shiftKey + if (key == 'cmd') { + return event.metaKey || event.ctrlKey; + } else { + return event[key]; + } +};