-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Reimplement drag fill #3766
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
Merged
Merged
Reimplement drag fill #3766
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e480e54
Re-implement drag fill
amanmahajan7 e93312c
scroll while dragging
amanmahajan7 58f4de5
set pointer capture in pointer down event
amanmahajan7 e9f8854
set events on drag handle
amanmahajan7 4998183
inline styles
amanmahajan7 610a1ac
Fix types
amanmahajan7 b931abf
Do not scroll on click
amanmahajan7 a641d49
pin vitest
amanmahajan7 daf6f63
Fix test and scroll
amanmahajan7 b08d138
Add comment
amanmahajan7 a9b7a7f
Rename
amanmahajan7 5d60434
Merge branch 'main' into am-drag-fill
amanmahajan7 3b78830
Merge branch 'main' into am-drag-fill
amanmahajan7 00ff51e
Update src/DataGrid.tsx
amanmahajan7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import { | |
assertIsValidKeyGetter, | ||
canExitGrid, | ||
createCellEvent, | ||
getCellStyle, | ||
getColSpan, | ||
getLeftRightKey, | ||
getNextSelectedCellPosition, | ||
|
@@ -67,14 +68,14 @@ import { | |
DataGridDefaultRenderersContext, | ||
useDefaultRenderers | ||
} from './DataGridDefaultRenderersContext'; | ||
import DragHandle from './DragHandle'; | ||
import EditCell from './EditCell'; | ||
import GroupedColumnHeaderRow from './GroupedColumnHeaderRow'; | ||
import HeaderRow from './HeaderRow'; | ||
import { defaultRenderRow } from './Row'; | ||
import type { PartialPosition } from './ScrollToCell'; | ||
import ScrollToCell from './ScrollToCell'; | ||
import { default as defaultRenderSortStatus } from './sortStatus'; | ||
import { cellDragHandleClassname, cellDragHandleFrozenClassname } from './style/cell'; | ||
import { | ||
focusSinkClassname, | ||
focusSinkHeaderAndSummaryClassname, | ||
|
@@ -332,7 +333,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr | |
); | ||
const [isColumnResizing, setColumnResizing] = useState(false); | ||
const [isDragging, setDragging] = useState(false); | ||
const [draggedOverRowIdx, setOverRowIdx] = useState<number | undefined>(undefined); | ||
const [draggedOverRowIdx, setDraggedOverRowIdx] = useState<number | undefined>(undefined); | ||
const [scrollToPosition, setScrollToPosition] = useState<PartialPosition | null>(null); | ||
const [shouldFocusCell, setShouldFocusCell] = useState(false); | ||
const [previousRowIdx, setPreviousRowIdx] = useState(-1); | ||
|
@@ -391,7 +392,6 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr | |
/** | ||
* refs | ||
*/ | ||
const latestDraggedOverRowIdx = useRef(draggedOverRowIdx); | ||
const focusSinkRef = useRef<HTMLDivElement>(null); | ||
|
||
/** | ||
|
@@ -506,20 +506,22 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr | |
/** | ||
* callbacks | ||
*/ | ||
const setDraggedOverRowIdx = useCallback((rowIdx?: number) => { | ||
setOverRowIdx(rowIdx); | ||
latestDraggedOverRowIdx.current = rowIdx; | ||
}, []); | ||
const focusCellOrCellContent = useCallback( | ||
(shouldScroll = true) => { | ||
const cell = getCellToScroll(gridRef.current!); | ||
if (cell === null) return; | ||
|
||
const focusCellOrCellContent = useCallback(() => { | ||
const cell = getCellToScroll(gridRef.current!); | ||
if (cell === null) return; | ||
if (shouldScroll) { | ||
scrollIntoView(cell); | ||
} | ||
|
||
scrollIntoView(cell); | ||
// Focus cell content when available instead of the cell itself | ||
const elementToFocus = cell.querySelector<Element & HTMLOrSVGElement>('[tabindex="0"]') ?? cell; | ||
elementToFocus.focus({ preventScroll: true }); | ||
}, [gridRef]); | ||
// Focus cell content when available instead of the cell itself | ||
const elementToFocus = | ||
cell.querySelector<Element & HTMLOrSVGElement>('[tabindex="0"]') ?? cell; | ||
elementToFocus.focus({ preventScroll: true }); | ||
}, | ||
[gridRef] | ||
); | ||
|
||
/** | ||
* effects | ||
|
@@ -735,6 +737,80 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr | |
} | ||
} | ||
|
||
function handleDragHandlePointerDown(event: React.PointerEvent<HTMLDivElement>) { | ||
// keep the focus on the cell | ||
event.preventDefault(); | ||
if (event.pointerType === 'mouse' && event.buttons !== 1) { | ||
return; | ||
} | ||
setDragging(true); | ||
event.currentTarget.setPointerCapture(event.pointerId); | ||
} | ||
|
||
function handleDragHandlePointerMove(event: React.PointerEvent<HTMLDivElement>) { | ||
// find dragged over row using the pointer position | ||
const gridEl = gridRef.current!; | ||
const headerAndTopSummaryRowsHeight = headerRowsHeight + topSummaryRowsCount * summaryRowHeight; | ||
const offset = | ||
scrollTop - | ||
headerAndTopSummaryRowsHeight + | ||
event.clientY - | ||
gridEl.getBoundingClientRect().top; | ||
Comment on lines
+754
to
+758
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better way? |
||
const overRowIdx = findRowIdx(offset); | ||
setDraggedOverRowIdx(overRowIdx); | ||
const ariaRowIndex = headerAndTopSummaryRowsCount + overRowIdx + 1; | ||
const el = gridEl.querySelector( | ||
`:scope > [aria-rowindex="${ariaRowIndex}"] > [aria-colindex="${selectedPosition.idx + 1}"]` | ||
); | ||
scrollIntoView(el); | ||
} | ||
|
||
function handleDragHandleLostPointerCapture() { | ||
setDragging(false); | ||
if (draggedOverRowIdx === undefined) return; | ||
|
||
const { rowIdx } = selectedPosition; | ||
const [startRowIndex, endRowIndex] = | ||
rowIdx < draggedOverRowIdx | ||
? [rowIdx + 1, draggedOverRowIdx + 1] | ||
: [draggedOverRowIdx, rowIdx]; | ||
updateRows(startRowIndex, endRowIndex); | ||
setDraggedOverRowIdx(undefined); | ||
} | ||
|
||
function handleDragHandleClick() { | ||
// keep the focus on the cell but do not scroll | ||
focusCellOrCellContent(false); | ||
} | ||
|
||
function handleDragHandleDoubleClick(event: React.MouseEvent<HTMLDivElement>) { | ||
event.stopPropagation(); | ||
updateRows(selectedPosition.rowIdx + 1, rows.length); | ||
} | ||
|
||
function updateRows(startRowIdx: number, endRowIdx: number) { | ||
if (onRowsChange == null) return; | ||
|
||
const { rowIdx, idx } = selectedPosition; | ||
const column = columns[idx]; | ||
const sourceRow = rows[rowIdx]; | ||
const updatedRows = [...rows]; | ||
const indexes: number[] = []; | ||
for (let i = startRowIdx; i < endRowIdx; i++) { | ||
if (isCellEditable({ rowIdx: i, idx })) { | ||
const updatedRow = onFill!({ columnKey: column.key, sourceRow, targetRow: rows[i] }); | ||
if (updatedRow !== rows[i]) { | ||
updatedRows[i] = updatedRow; | ||
indexes.push(i); | ||
} | ||
} | ||
} | ||
|
||
if (indexes.length > 0) { | ||
onRowsChange(updatedRows, { indexes, column }); | ||
} | ||
} | ||
|
||
/** | ||
* utils | ||
*/ | ||
|
@@ -890,7 +966,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr | |
return isDraggedOver ? selectedPosition.idx : undefined; | ||
} | ||
|
||
function renderDragHandle() { | ||
function getDragHandle() { | ||
if ( | ||
onFill == null || | ||
selectedPosition.mode === 'EDIT' || | ||
|
@@ -905,24 +981,31 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr | |
return; | ||
} | ||
|
||
const isLastRow = rowIdx === maxRowIdx; | ||
const columnWidth = getColumnWidth(column); | ||
const colSpan = column.colSpan?.({ type: 'ROW', row: rows[rowIdx] }) ?? 1; | ||
const { insetInlineStart, ...style } = getCellStyle(column, colSpan); | ||
const marginEnd = 'calc(var(--rdg-drag-handle-size) * -0.5 + 1px)'; | ||
const isLastColumn = column.idx + colSpan - 1 === maxColIdx; | ||
const dragHandleStyle: React.CSSProperties = { | ||
...style, | ||
gridRowStart: headerAndTopSummaryRowsCount + rowIdx + 1, | ||
marginInlineEnd: isLastColumn ? undefined : marginEnd, | ||
marginBlockEnd: isLastRow ? undefined : marginEnd, | ||
insetInlineStart: insetInlineStart | ||
? `calc(${insetInlineStart} + ${columnWidth}px + var(--rdg-drag-handle-size) * -0.5 - 1px)` | ||
: undefined | ||
}; | ||
|
||
return ( | ||
<DragHandle | ||
gridRowStart={headerAndTopSummaryRowsCount + rowIdx + 1} | ||
rows={rows} | ||
column={column} | ||
columnWidth={columnWidth} | ||
maxColIdx={maxColIdx} | ||
isLastRow={rowIdx === maxRowIdx} | ||
selectedPosition={selectedPosition} | ||
isCellEditable={isCellEditable} | ||
latestDraggedOverRowIdx={latestDraggedOverRowIdx} | ||
onRowsChange={onRowsChange} | ||
onClick={focusCellOrCellContent} | ||
onFill={onFill} | ||
setDragging={setDragging} | ||
setDraggedOverRowIdx={setDraggedOverRowIdx} | ||
<div | ||
style={dragHandleStyle} | ||
className={clsx(cellDragHandleClassname, column.frozen && cellDragHandleFrozenClassname)} | ||
onPointerDown={handleDragHandlePointerDown} | ||
onPointerMove={isDragging ? handleDragHandlePointerMove : undefined} | ||
onLostPointerCapture={isDragging ? handleDragHandleLostPointerCapture : undefined} | ||
onClick={handleDragHandleClick} | ||
onDoubleClick={handleDragHandleDoubleClick} | ||
/> | ||
); | ||
} | ||
|
@@ -1055,7 +1138,6 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr | |
gridRowStart, | ||
selectedCellIdx: selectedRowIdx === rowIdx ? selectedIdx : undefined, | ||
draggedOverCellIdx: getDraggedOverCellIdx(rowIdx), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably add another div and get rid of |
||
setDraggedOverRowIdx: isDragging ? setDraggedOverRowIdx : undefined, | ||
lastFrozenColumnIndex, | ||
onRowChange: handleFormatterRowChangeLatest, | ||
selectCell: selectCellLatest, | ||
|
@@ -1239,7 +1321,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr | |
)} | ||
</DataGridDefaultRenderersContext> | ||
|
||
{renderDragHandle()} | ||
{getDragHandle()} | ||
|
||
{/* render empty cells that span only 1 column so we can safely measure column widths, regardless of colSpan */} | ||
{renderMeasuringCells(viewportColumns)} | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed as we are using react event handlers