From 6f0f4417722f23656b10fce474da649695d9f655 Mon Sep 17 00:00:00 2001 From: Chaitanya Aaditya Date: Sun, 11 Aug 2024 23:12:38 +0530 Subject: [PATCH 1/2] fix: disable sorting of rows from header if interacting with input elements --- src/HeaderCell.tsx | 19 +++++++++++++++++-- src/utils/eventUtils.ts | 6 ++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/HeaderCell.tsx b/src/HeaderCell.tsx index 2dde6d2fe0..db64e8d0de 100644 --- a/src/HeaderCell.tsx +++ b/src/HeaderCell.tsx @@ -8,6 +8,7 @@ import { getCellStyle, getHeaderCellRowSpan, getHeaderCellStyle, + isInteractingWithInput, stopPropagation } from './utils'; import type { CalculatedColumn, SortColumn } from './types'; @@ -182,7 +183,15 @@ export default function HeaderCell({ selectCell({ idx: column.idx, rowIdx }); if (sortable) { - onSort(event.ctrlKey || event.metaKey); + // prevent sorting when interacting with input elements + const shouldAvoidSorting = isInteractingWithInput(event); + // const shouldAvoidSorting = event.target instanceof HTMLInputElement || + // event.target instanceof HTMLTextAreaElement || + // event.target instanceof HTMLSelectElement; + console.log("shouldAvoidSorting ", shouldAvoidSorting); + if (!shouldAvoidSorting) { + onSort(event.ctrlKey || event.metaKey); + } } } @@ -202,7 +211,13 @@ export default function HeaderCell({ if (event.key === ' ' || event.key === 'Enter') { // prevent scrolling event.preventDefault(); - onSort(event.ctrlKey || event.metaKey); + + // prevents sorting when interacting with input elements + const shouldAvoidSorting = isInteractingWithInput(event); + + if (!shouldAvoidSorting) { + onSort(event.ctrlKey || event.metaKey); + } } } diff --git a/src/utils/eventUtils.ts b/src/utils/eventUtils.ts index d8d47a4892..683d379281 100644 --- a/src/utils/eventUtils.ts +++ b/src/utils/eventUtils.ts @@ -18,3 +18,9 @@ export function createCellEvent>( return cellEvent; } + +export function isInteractingWithInput(event: React.KeyboardEvent | React.MouseEvent) { + return event.target instanceof HTMLInputElement || + event.target instanceof HTMLTextAreaElement || + event.target instanceof HTMLSelectElement; +} From 1619ff31cabf20d4a43dd744f57e35776e6fbd15 Mon Sep 17 00:00:00 2001 From: Chaitanya Aaditya Date: Sun, 11 Aug 2024 23:28:54 +0530 Subject: [PATCH 2/2] cleanup --- src/HeaderCell.tsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/HeaderCell.tsx b/src/HeaderCell.tsx index db64e8d0de..05b879fd86 100644 --- a/src/HeaderCell.tsx +++ b/src/HeaderCell.tsx @@ -185,10 +185,6 @@ export default function HeaderCell({ if (sortable) { // prevent sorting when interacting with input elements const shouldAvoidSorting = isInteractingWithInput(event); - // const shouldAvoidSorting = event.target instanceof HTMLInputElement || - // event.target instanceof HTMLTextAreaElement || - // event.target instanceof HTMLSelectElement; - console.log("shouldAvoidSorting ", shouldAvoidSorting); if (!shouldAvoidSorting) { onSort(event.ctrlKey || event.metaKey); }