Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable sorting of rows from header if interacting with input elements #3574

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/HeaderCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getCellStyle,
getHeaderCellRowSpan,
getHeaderCellStyle,
isInteractingWithInput,
stopPropagation
} from './utils';
import type { CalculatedColumn, SortColumn } from './types';
Expand Down Expand Up @@ -197,7 +198,11 @@ export default function HeaderCell<R, SR>({
selectCell({ idx: column.idx, rowIdx });

if (sortable) {
onSort(event.ctrlKey || event.metaKey);
// prevent sorting when interacting with input elements
const shouldAvoidSorting = isInteractingWithInput(event);
if (!shouldAvoidSorting) {
onSort(event.ctrlKey || event.metaKey);
}
}
}

Expand All @@ -213,7 +218,13 @@ export default function HeaderCell<R, SR>({
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);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/eventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ export function createCellEvent<E extends React.SyntheticEvent<HTMLDivElement>>(

return cellEvent;
}

export function isInteractingWithInput(event: React.KeyboardEvent<HTMLSpanElement> | React.MouseEvent<HTMLSpanElement>) {
return event.target instanceof HTMLInputElement ||
event.target instanceof HTMLTextAreaElement ||
event.target instanceof HTMLSelectElement;
}