Skip to content

Commit

Permalink
Merge pull request #379 from Renumics/fix/broken-keyboard-scroll-in-d…
Browse files Browse the repository at this point in the history
…atagrid

Fix/broken keyboard scroll in datagrid
  • Loading branch information
neindochoh authored Nov 21, 2023
2 parents f753c7b + 722f68d commit ab6c0d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/systems/dnd/Draggable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ export default function Draggable({ data, children }: Props) {
id,
data,
});

// remove tabIndex from attributes
// as it prevents the keyboard controls of our table from working correctly
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { tabIndex, ...neededAttributes } = attributes;

return (
<div ref={setNodeRef} {...listeners} {...attributes}>
<div ref={setNodeRef} {...listeners} {...neededAttributes}>
{children}
</div>
);
Expand Down
18 changes: 15 additions & 3 deletions src/widgets/DataGrid/KeyboardControls.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyboardEvent, PropsWithChildren, useCallback } from 'react';
import { KeyboardEvent, PropsWithChildren, useCallback, useRef } from 'react';
import { Dataset, useDataset } from '../../stores/dataset';
import 'twin.macro';
import useSort from './hooks/useSort';
Expand All @@ -16,6 +16,8 @@ const KeyboardControls = ({
const selectRows = useDataset(selectRowsSelector);
const { getSortedIndex, getOriginalIndex } = useSort();

const containerRef = useRef<HTMLDivElement>(null);

const handleKeyDown = useCallback(
(e: KeyboardEvent<HTMLDivElement>) => {
const selectedIndices = useDataset.getState().selectedIndices;
Expand All @@ -26,6 +28,11 @@ const KeyboardControls = ({

if (step === 0) return;

e.preventDefault();
if (e.target != containerRef.current) {
containerRef.current?.focus();
}

const sortedIndex = getSortedIndex(lastSelectedIndex);
const nextSortedIndex = sortedIndex + step;
const nextSelectedIndex = getOriginalIndex(nextSortedIndex);
Expand All @@ -39,8 +46,13 @@ const KeyboardControls = ({
);

return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions,jsx-a11y/no-noninteractive-tabindex
<div tw="outline-none" tabIndex={0} onKeyDown={handleKeyDown}>
/* eslint-disable jsx-a11y/no-static-element-interactions,jsx-a11y/no-noninteractive-tabindex */
<div
tw="outline-none"
tabIndex={0}
ref={containerRef}
onKeyDown={handleKeyDown}
>
{children}
</div>
);
Expand Down

0 comments on commit ab6c0d9

Please sign in to comment.