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

Added a prop to disable Keyboard Events #3559

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ A number defining the height of summary rows.

###### `onSelectedCellChange?: Maybe<(args: CellSelectArgs<R, SR>) => void>;`

###### `disableKeyboardEvents?: Maybe<boolean>`

**Default:** `false`

Disables keyboard interaction for the header cells

Triggered when the selected cell is changed.

Arguments:
Expand Down
4 changes: 4 additions & 0 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
/**
* Event props
*/
/** Disables all keyboard events like using space or enter to sort the columns */
disableKeyboardEvents?: Maybe<number;
/** Function called whenever a cell is clicked */
onCellClick?: Maybe<
(args: CellClickArgs<NoInfer<R>, NoInfer<SR>>, event: CellMouseEvent) => void
Expand Down Expand Up @@ -248,6 +250,7 @@ function DataGrid<R, SR, K extends Key>(
className,
style,
rowClass,
disableKeyboardEvents,
direction: rawDirection,
// ARIA
role: rawRole,
Expand Down Expand Up @@ -1114,6 +1117,7 @@ function DataGrid<R, SR, K extends Key>(
selectedCellIdx={
selectedPosition.rowIdx === mainHeaderRowIdx ? selectedPosition.idx : undefined
}
disableKeyboardEvents={disableKeyboardEvents}
selectCell={selectHeaderCellLatest}
shouldFocusGrid={!selectedCellIsWithinSelectionBounds}
direction={direction}
Expand Down
6 changes: 5 additions & 1 deletion src/HeaderCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface HeaderCellProps<R, SR> extends SharedHeaderRowProps<R, SR> {
rowIdx: number;
isCellSelected: boolean;
dragDropKey: string;
disableKeyboardEvents: boolean;
}

export default function HeaderCell<R, SR>({
Expand All @@ -84,7 +85,8 @@ export default function HeaderCell<R, SR>({
selectCell,
shouldFocusGrid,
direction,
dragDropKey
dragDropKey,
disableKeyboardEvents
}: HeaderCellProps<R, SR>) {
const [isDragging, setIsDragging] = useState(false);
const [isOver, setIsOver] = useState(false);
Expand Down Expand Up @@ -199,6 +201,8 @@ export default function HeaderCell<R, SR>({
}

function onKeyDown(event: React.KeyboardEvent<HTMLSpanElement>) {
if(disableKeyboardEvents) return;

if (event.key === ' ' || event.key === 'Enter') {
// prevent scrolling
event.preventDefault();
Expand Down
5 changes: 4 additions & 1 deletion src/HeaderRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface HeaderRowProps<R, SR, K extends React.Key> extends SharedDataGr
selectedCellIdx: number | undefined;
shouldFocusGrid: boolean;
direction: Direction;
disableKeyboardEvents: boolean;
}

const headerRow = css`
Expand Down Expand Up @@ -56,7 +57,8 @@ function HeaderRow<R, SR, K extends React.Key>({
selectedCellIdx,
selectCell,
shouldFocusGrid,
direction
direction,
disableKeyboardEvents
}: HeaderRowProps<R, SR, K>) {
const dragDropKey = useId();

Expand All @@ -83,6 +85,7 @@ function HeaderRow<R, SR, K extends React.Key>({
shouldFocusGrid={shouldFocusGrid && index === 0}
direction={direction}
dragDropKey={dragDropKey}
disableKeyboardEvents={disableKeyboardEvents}
/>
);
}
Expand Down