Skip to content

Commit 17c075a

Browse files
authored
Merge pull request #2284
* [#CODAP-1067] Feature: Case Table editing behavior better matches V2 * chore: code review tweaks
1 parent ea942fa commit 17c075a

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

v3/src/components/case-table/collection-table.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,26 @@ export const CollectionTable = observer(function CollectionTable(props: IProps)
226226
}
227227
}, [collectionTableModel?.rows, collectionTableModel?.inputRowIndex, showInputRow])
228228

229-
const { handleSelectedCellChange, navigateToNextRow } = useSelectedCell(gridRef, columns, rows)
229+
const { handleSelectedCellChange, navigateToNextCell, navigateToNextRow } = useSelectedCell(gridRef, columns, rows)
230230

231231
const handleCellKeyDown = useCallback((args: TCellKeyDownArgs, event: CellKeyboardEvent) => {
232232
// By default in RDG, the enter/return key simply enters/exits edit mode without moving the
233233
// selected cell. In CODAP, the enter/return key should accept the edit _and_ advance to the
234234
// next row. To achieve this in RDG, we provide this callback, which is called before RDG
235235
// handles the event internally. If we get an enter/return key while in edit mode, we handle
236236
// it ourselves and call `preventGridDefault()` to prevent RDG from handling the event itself.
237-
if (args.mode === "EDIT" && event.key === "Enter") {
237+
if (args.mode === "EDIT" && ["Enter", "Tab", "ArrowUp", "ArrowDown"].includes(event.key)) {
238238
// complete the cell edit
239239
args.onClose(true)
240240
// prevent RDG from handling the event
241241
event.preventGridDefault()
242-
navigateToNextRow(event.shiftKey)
242+
if (["Enter", "ArrowUp", "ArrowDown"].includes(event.key)) {
243+
const reverse = event.shiftKey || event.key === "ArrowUp"
244+
navigateToNextRow(reverse)
245+
}
246+
if (event.key === "Tab") {
247+
navigateToNextCell(event.shiftKey)
248+
}
243249
}
244250
if ((event.key === "ArrowDown" || event.key === "ArrowUp")) {
245251
const caseId = args.row.__id__
@@ -286,7 +292,7 @@ export const CollectionTable = observer(function CollectionTable(props: IProps)
286292
}
287293
}
288294
}
289-
}, [collection, collectionId, data, navigateToNextRow, onScrollRowRangeIntoView])
295+
}, [collection, collectionId, data, navigateToNextCell, navigateToNextRow, onScrollRowRangeIntoView])
290296

291297
const handleClick = (event: React.PointerEvent<HTMLDivElement>) => {
292298
// See if mouse has moved beyond kMouseMovementThreshold since initial mousedown

v3/src/components/case-table/use-selected-cell.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ export function useSelectedCell(gridRef: React.RefObject<DataGridHandle | null>,
4747
}
4848
}, [collectionTableModel, columns, gridRef])
4949

50+
const navigateToNextCell = useCallback((back = false) => {
51+
if (selectedCell.current?.columnId) {
52+
// column index
53+
const currentRowIdx = selectedCell.current.rowIdx
54+
const currentIdx = columns.findIndex(column => column.key === selectedCell.current?.columnId)
55+
const rightmost = currentIdx === columns.length - 1
56+
const first = currentIdx === 1
57+
if (first && currentRowIdx === 0 && back) {
58+
// don't navigate left from the first cell
59+
return
60+
}
61+
const idx = back
62+
? first ? Math.max(1, columns.length - 1) : currentIdx - 1
63+
: rightmost ? 1 : currentIdx + 1
64+
// row index
65+
const rowIdx = back
66+
? first ? Math.max(0, currentRowIdx - 1) : currentRowIdx
67+
: rightmost ? currentRowIdx + 1 : currentRowIdx
68+
const position = { idx, rowIdx }
69+
// setTimeout so it occurs after handling of current event completes
70+
setTimeout(() => {
71+
collectionTableModel?.scrollRowIntoView(rowIdx)
72+
gridRef.current?.selectCell(position, true)
73+
})
74+
}
75+
}, [collectionTableModel, columns, gridRef])
76+
5077
const refreshSelectedCell = useCallback(() => {
5178
if (selectedCell.current) {
5279
const { columnId, rowId } = selectedCell.current
@@ -79,5 +106,5 @@ export function useSelectedCell(gridRef: React.RefObject<DataGridHandle | null>,
79106
}
80107
}, [blockingDataset, refreshSelectedCellDebounced])
81108

82-
return { selectedCell: selectedCell.current, handleSelectedCellChange, navigateToNextRow }
109+
return { selectedCell: selectedCell.current, handleSelectedCellChange, navigateToNextCell, navigateToNextRow }
83110
}

0 commit comments

Comments
 (0)