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

Range selection #2865

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
9 changes: 9 additions & 0 deletions src/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Cell<R, SR>({
onRowDoubleClick,
onRowChange,
selectCell,
rangeSelectionMode,
...props
}: CellRendererProps<R, SR>) {
const { ref, tabIndex, onFocus } = useRovingCellRef(isCellSelected);
Expand Down Expand Up @@ -65,6 +66,13 @@ function Cell<R, SR>({
onRowDoubleClick?.(row, column);
}

function onMouseDown(){
if(rangeSelectionMode){
selectCellWrapper(false);
onRowClick?.(row, column);
}
}

return (
<div
role="gridcell"
Expand All @@ -80,6 +88,7 @@ function Cell<R, SR>({
onDoubleClick={handleDoubleClick}
onContextMenu={handleContextMenu}
onFocus={onFocus}
onMouseDown={onMouseDown}
{...props}
>
{!column.rowGroup && (
Expand Down
163 changes: 125 additions & 38 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ import type {
RowHeightArgs,
Maybe,
Components,
Direction
Direction,
MultiPasteEvent, CellsRange, MultiCopyEvent
} from './types';
import {isValueInBetween} from "./utils/Helpers";

export interface SelectCellState extends Position {
readonly mode: 'SELECT';
Expand All @@ -82,6 +84,13 @@ const initialPosition: SelectCellState = {
mode: 'SELECT'
};

const initialSelectedRange: CellsRange = {
startRowIdx: -1,
startColumnIdx: -1,
endRowIdx: -1,
endColumnIdx: -1,
};

export interface DataGridHandle {
element: HTMLDivElement | null;
scrollToColumn: (colIdx: number) => void;
Expand Down Expand Up @@ -148,6 +157,10 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
onFill?: Maybe<(event: FillEvent<R>) => R>;
onCopy?: Maybe<(event: CopyEvent<R>) => void>;
onPaste?: Maybe<(event: PasteEvent<R>) => R>;
onMultiPaste?: Maybe<(event: MultiPasteEvent) => void>;
onMultiCopy?: Maybe<(event: MultiCopyEvent) => void>;
onMultiCopySuccess?: Maybe<(copiedText: string) => void>;
onMultiCopyFail?: Maybe<(copiedText: string) => void>;

/**
* Event props
Expand All @@ -168,6 +181,8 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
cellNavigationMode?: Maybe<CellNavigationMode>;
/** @default true */
enableVirtualization?: Maybe<boolean>;
/** @default false, set true to enable range selection with copy and paste through clipboard */
enableRangeSelection?: Maybe<boolean>

/**
* Miscellaneous
Expand Down Expand Up @@ -215,9 +230,13 @@ function DataGrid<R, SR, K extends Key>(
onFill,
onCopy,
onPaste,
onMultiPaste,
onMultiCopy,

// Toggles and modes
cellNavigationMode: rawCellNavigationMode,
enableVirtualization,
enableRangeSelection,
// Miscellaneous
components,
className,
Expand Down Expand Up @@ -246,6 +265,7 @@ function DataGrid<R, SR, K extends Key>(
const noRowsFallback = components?.noRowsFallback ?? defaultComponents?.noRowsFallback;
const cellNavigationMode = rawCellNavigationMode ?? 'NONE';
enableVirtualization ??= true;
enableRangeSelection ??= false;
direction ??= 'ltr';

/**
Expand All @@ -262,6 +282,10 @@ function DataGrid<R, SR, K extends Key>(
const [draggedOverRowIdx, setOverRowIdx] = useState<number | undefined>(undefined);
const [autoResizeColumn, setAutoResizeColumn] = useState<CalculatedColumn<R, SR> | null>(null);

const [selectedRange, setSelectedRange] = useState<CellsRange>(initialSelectedRange);
const [copiedRange, setCopiedRange] = useState<CellsRange | null>(null);
const [isMouseRangeSelectionMode, setIsMouseRangeSelectionMode] = useState<boolean>(false);

/**
* refs
*/
Expand Down Expand Up @@ -551,7 +575,7 @@ function DataGrid<R, SR, K extends Key>(

if (
selectedCellIsWithinViewportBounds &&
(onPaste != null || onCopy != null) &&
(onPaste != null || onCopy != null || onMultiCopy != null || onMultiPaste != null) &&
isCtrlKeyHeldDown(event) &&
!isGroupRow(rows[rowIdx]) &&
selectedPosition.mode === 'SELECT'
Expand Down Expand Up @@ -587,24 +611,51 @@ function DataGrid<R, SR, K extends Key>(
}
}

switch (event.key) {
case 'Escape':
setCopiedCell(null);
return;
case 'ArrowUp':
case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
case 'Tab':
case 'Home':
case 'End':
case 'PageUp':
case 'PageDown':
navigate(event);
break;
default:
handleCellInput(event);
break;
if(event.shiftKey){
switch (event.key) {
case 'ArrowUp':
if (selectedRange.endRowIdx > 0) {
setSelectedRange({...selectedRange, endRowIdx: selectedRange.endRowIdx - 1})
}
break;
case 'ArrowDown':
if (selectedRange.endRowIdx < rows.length - 1) {
setSelectedRange({...selectedRange, endRowIdx: selectedRange.endRowIdx + 1})
}
break;
case 'ArrowRight':
if (selectedRange.endColumnIdx < columns.length - 1) {
setSelectedRange({...selectedRange, endColumnIdx: selectedRange.endColumnIdx + 1})
}
break;
case 'ArrowLeft':
if (selectedRange.endColumnIdx > 0) {
setSelectedRange({...selectedRange, endColumnIdx: selectedRange.endColumnIdx - 1})
}
break;
default:
break;
}
}else{
switch ( event.key ) {
case 'Escape':
setCopiedCell ( null );
return;
case 'ArrowUp':
case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
case 'Tab':
case 'Home':
case 'End':
case 'PageUp':
case 'PageDown':
navigate ( event );
break;
default:
handleCellInput ( event );
break;
}
}
}

Expand Down Expand Up @@ -638,32 +689,49 @@ function DataGrid<R, SR, K extends Key>(
}

function handleCopy() {
const { idx, rowIdx } = selectedPosition;
const sourceRow = rawRows[getRawRowIdx(rowIdx)];
const sourceColumnKey = columns[idx].key;
setCopiedCell({ row: sourceRow, columnKey: sourceColumnKey });
onCopy?.({ sourceRow, sourceColumnKey });
if(enableRangeSelection){
setCopiedRange(selectedRange)
onMultiCopy?.({cellsRange: selectedRange})
}else{
const { idx, rowIdx } = selectedPosition;
const sourceRow = rawRows[getRawRowIdx(rowIdx)];
const sourceColumnKey = columns[idx].key;
setCopiedCell({ row: sourceRow, columnKey: sourceColumnKey });
onCopy?.({ sourceRow, sourceColumnKey });
}
}

function handlePaste() {
if (!onPaste || !onRowsChange || copiedCell === null || !isCellEditable(selectedPosition)) {
return;
}
if (enableRangeSelection) {
if (!onMultiPaste || !onRowsChange || copiedRange === null) {
return;
}

const { idx, rowIdx } = selectedPosition;
const targetRow = rawRows[getRawRowIdx(rowIdx)];
onMultiPaste({
copiedRange,
targetRange: selectedRange
})
} else {

const updatedTargetRow = onPaste({
sourceRow: copiedCell.row,
sourceColumnKey: copiedCell.columnKey,
targetRow,
targetColumnKey: columns[idx].key
});
if (!onPaste || !onRowsChange || copiedCell === null || !isCellEditable(selectedPosition)) {
return;
}

updateRow(rowIdx, updatedTargetRow);
const {idx, rowIdx} = selectedPosition;
const targetRow = rawRows[getRawRowIdx(rowIdx)];

const updatedTargetRow = onPaste({
sourceRow: copiedCell.row,
sourceColumnKey: copiedCell.columnKey,
targetRow,
targetColumnKey: columns[idx].key
});

updateRow(rowIdx, updatedTargetRow);
}
}

function handleCellInput(event: React.KeyboardEvent<HTMLDivElement>) {
function handleCellInput(event: React.KeyboardEvent<HTMLDivElement>) {
if (!selectedCellIsWithinViewportBounds) return;
const row = rows[selectedPosition.rowIdx];
if (isGroupRow(row)) return;
Expand Down Expand Up @@ -733,6 +801,12 @@ function DataGrid<R, SR, K extends Key>(
scrollToCell(position);
} else {
setSelectedPosition({ ...position, mode: 'SELECT' });
setSelectedRange({
startColumnIdx: position.idx,
startRowIdx: position.rowIdx,
endColumnIdx: position.idx,
endRowIdx: position.rowIdx
})
}
}

Expand Down Expand Up @@ -1091,13 +1165,25 @@ function DataGrid<R, SR, K extends Key>(
: undefined
}
selectedCellIdx={selectedRowIdx === rowIdx ? selectedIdx : undefined}
selectedCellsRange={ enableRangeSelection && isValueInBetween(rowIdx, selectedRange?.startRowIdx, selectedRange?.endRowIdx) ? {
startIdx: selectedRange.startColumnIdx,
endIdx: selectedRange.endColumnIdx
} : {startIdx: -1, endIdx: -1}}
draggedOverCellIdx={getDraggedOverCellIdx(rowIdx)}
setDraggedOverRowIdx={isDragging ? setDraggedOverRowIdx : undefined}
lastFrozenColumnIndex={lastFrozenColumnIndex}
onRowChange={handleFormatterRowChangeLatest}
selectCell={selectViewportCellLatest}
rangeSelectionMode={enableRangeSelection ?? false}
selectedCellDragHandle={getDragHandle(rowIdx)}
selectedCellEditor={getCellEditor(rowIdx)}
onCellMouseDown={() => setIsMouseRangeSelectionMode(true)}
onCellMouseUp={() => setIsMouseRangeSelectionMode(false)}
onCellMouseEnter={(columnIdx: number) => {
if (isMouseRangeSelectionMode && enableRangeSelection) {
setSelectedRange({...selectedRange, endRowIdx: rowIdx, endColumnIdx: columnIdx})
}
}}
/>
);
}
Expand All @@ -1109,6 +1195,7 @@ function DataGrid<R, SR, K extends Key>(
if (selectedPosition.idx > maxColIdx || selectedPosition.rowIdx > maxRowIdx) {
setSelectedPosition(initialPosition);
setDraggedOverRowIdx(undefined);
setSelectedRange(initialSelectedRange)
}

let templateRows = `${headerRowHeight}px`;
Expand Down
Loading