Skip to content

Commit

Permalink
fix(Dataspreadsheet): Reduce duplication with isHoldingCommandKey car…
Browse files Browse the repository at this point in the history
  • Loading branch information
amal-k-joy authored and paul-balchin-ibm committed Feb 26, 2024
1 parent 47be3ea commit ef93720
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 ef93720

Please sign in to comment.