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

Disable cell navigation while editing, add column.editorOptions.onNavigation #2196

Merged
merged 8 commits into from
Nov 5, 2020
Merged
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
4 changes: 0 additions & 4 deletions src/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ function Cell<R, SR>({
eventBus,
dragHandleProps,
onRowClick,
onFocus,
onKeyDown,
onClick,
onDoubleClick,
onContextMenu,
Expand Down Expand Up @@ -72,8 +70,6 @@ function Cell<R, SR>({
width: column.width,
left: column.left
}}
onFocus={onFocus}
onKeyDown={onKeyDown}
onClick={wrapEvent(handleClick, onClick)}
onDoubleClick={wrapEvent(handleDoubleClick, onDoubleClick)}
onContextMenu={wrapEvent(handleContextMenu, onContextMenu)}
Expand Down
5 changes: 5 additions & 0 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import SummaryRow from './SummaryRow';
import {
assertIsValidKeyGetter,
getColumnScrollPosition,
onEditorNavigation,
getNextSelectedCellPosition,
isSelectedCellEditable,
canExitGrid,
Expand Down Expand Up @@ -722,6 +723,10 @@ function DataGrid<R, SR>({
}

function navigate(event: React.KeyboardEvent<HTMLDivElement>) {
if (selectedPosition.mode === 'EDIT') {
const onNavigation = columns[selectedPosition.idx].editorOptions?.onNavigation ?? onEditorNavigation;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably add an example of custom onEditorNavigation like

  • Navigate left if the cursor is at position 0
  • Navigate right if the cursor is at position selection.length

if (!onNavigation(event)) return;
}
const { key, shiftKey } = event;
const ctrlKey = isCtrlKeyHeldDown(event);
let nextPosition = getNextPosition(key, ctrlKey, shiftKey);
Expand Down
5 changes: 0 additions & 5 deletions src/editors/TextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ export default function TextEditor<TRow>({
value={row[column.key as keyof TRow] as unknown as string}
onChange={event => onRowChange({ ...row, [column.key]: event.target.value })}
onBlur={() => onClose(true)}
onKeyDown={event => {
if (/^(Arrow(Left|Right)|Home|End)$/.test(event.key)) {
event.stopPropagation();
}
}}
/>
);
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface Column<TRow, TSummaryRow = unknown> {
editOnClick?: boolean;
/** Prevent default to cancel editing */
onCellKeyDown?: (event: React.KeyboardEvent<HTMLDivElement>) => void;
/** Control the default cell navigation behavior while the editor is open */
onNavigation?: (event: React.KeyboardEvent<HTMLDivElement>) => boolean;
// TODO: Do we need these options
// editOnDoubleClick?: boolean;
/** @default false */
Expand Down
13 changes: 13 additions & 0 deletions src/utils/columnUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,16 @@ export function getColumnScrollPosition<R, SR>(columns: readonly CalculatedColum

return 0;
}

/**
* By default, the following navigation keys are enabled while an editor is open, under specific conditions:
* - Tab:
* - The editor must be an <input>, a <textarea>, or a <select> element.
* - The editor element must be the only immediate child of the editor container/a label.
*/
export function onEditorNavigation({ key, target }: React.KeyboardEvent<HTMLDivElement>): boolean {
if (key === 'Tab' && (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement)) {
return target.matches('.rdg-editor-container > :only-child, .rdg-editor-container > label:only-child > :only-child');
}
return false;
}
14 changes: 1 addition & 13 deletions stories/demos/components/Editors/SelectEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React from 'react';
import Select, { OptionTypeBase, OptionsType } from 'react-select';

interface SelectEditorProps {
Expand All @@ -10,7 +10,6 @@ interface SelectEditorProps {
}

export function SelectEditor({ value, onChange, options, rowHeight, menuPortalTarget }: SelectEditorProps) {
const [isMenuOpen, setMenuOpen] = useState(true);
return (
<Select
autoFocus
Expand All @@ -19,8 +18,6 @@ export function SelectEditor({ value, onChange, options, rowHeight, menuPortalTa
onChange={o => onChange(o.value)}
options={options}
menuPortalTarget={menuPortalTarget as HTMLElement}
onMenuOpen={() => setMenuOpen(true)}
onMenuClose={() => setMenuOpen(false)}
styles={{
control: (provided) => ({
...provided,
Expand All @@ -33,15 +30,6 @@ export function SelectEditor({ value, onChange, options, rowHeight, menuPortalTa
height: rowHeight - 1
})
}}
onKeyDown={event => {
if (['ArrowDown', 'ArrowUp', 'Enter'].includes(event.key) && isMenuOpen) {
event.stopPropagation();
}

if (['ArrowLeft', 'ArrowRight'].includes(event.key)) {
event.stopPropagation();
}
}}
/>
);
}