Skip to content

Commit

Permalink
fix(Dataspreadsheet): Reduce duplication with isHoldingCommandKey #4188
Browse files Browse the repository at this point in the history
… (#4227)
  • Loading branch information
amal-k-joy authored Feb 19, 2024
1 parent 76cdfc6 commit 1b42224
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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];
}
};

0 comments on commit 1b42224

Please sign in to comment.