Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Keep the arrow keys working after a row is added

Damian Legawiec3d719e0fb00d
diff --git a/packages/dashboard-ui/src/spree/data-grid/cells.tsx b/packages/dashboard-ui/src/spree/data-grid/cells.tsx
index 510d0b2069..d6fac0b70e 100644
--- a/packages/dashboard-ui/src/spree/data-grid/cells.tsx
+++ b/packages/dashboard-ui/src/spree/data-grid/cells.tsx
@@ -80,10 +80,13 @@ export function NumberCell({
commit()
ctx.setEditing(null)
// Move down to mirror Excel.
- const next = { row: coords.row + 1, col: coords.col }
+ // Clamped like arrow navigation: on the last row Enter would otherwise
+ // anchor outside the grid, and every arrow key after it would compute
+ // from a position that does not exist.
+ const next = { row: Math.min(coords.row + 1, ctx.bounds.maxRow), col: coords.col }
ctx.setAnchor(next)
ctx.setExtent(next)
- ctx.cells.get(`${next.row}.${next.col}`)?.focus()
+ ctx.focusCell(next)
} else if (event.key === 'Escape') {
event.preventDefault()
setDraft(value.toString())
@@ -96,7 +99,7 @@ export function NumberCell({
const next = { row: coords.row, col: coords.col + dx }
ctx.setAnchor(next)
ctx.setExtent(next)
- ctx.cells.get(`${next.row}.${next.col}`)?.focus()
+ ctx.focusCell(next)
}
}
@@ -223,10 +226,13 @@ export function MoneyCell({
event.preventDefault()
commit()
ctx.setEditing(null)
- const next = { row: coords.row + 1, col: coords.col }
+ // Clamped like arrow navigation: on the last row Enter would otherwise
+ // anchor outside the grid, and every arrow key after it would compute
+ // from a position that does not exist.
+ const next = { row: Math.min(coords.row + 1, ctx.bounds.maxRow), col: coords.col }
ctx.setAnchor(next)
ctx.setExtent(next)
- ctx.cells.get(`${next.row}.${next.col}`)?.focus()
+ ctx.focusCell(next)
} else if (event.key === 'Escape') {
event.preventDefault()
setDraft(formatDisplay(value))
@@ -239,7 +245,7 @@ export function MoneyCell({
const next = { row: coords.row, col: coords.col + dx }
ctx.setAnchor(next)
ctx.setExtent(next)
- ctx.cells.get(`${next.row}.${next.col}`)?.focus()
+ ctx.focusCell(next)
}
}
@@ -350,10 +356,13 @@ export function TextCell({ coords, value, onChange, ariaLabel }: TextCellProps)
event.preventDefault()
commit()
ctx.setEditing(null)
- const next = { row: coords.row + 1, col: coords.col }
+ // Clamped like arrow navigation: on the last row Enter would otherwise
+ // anchor outside the grid, and every arrow key after it would compute
+ // from a position that does not exist.
+ const next = { row: Math.min(coords.row + 1, ctx.bounds.maxRow), col: coords.col }
ctx.setAnchor(next)
ctx.setExtent(next)
- ctx.cells.get(`${next.row}.${next.col}`)?.focus()
+ ctx.focusCell(next)
} else if (event.key === 'Escape') {
event.preventDefault()
setDraft(value ?? '')
@@ -366,7 +375,7 @@ export function TextCell({ coords, value, onChange, ariaLabel }: TextCellProps)
const next = { row: coords.row, col: coords.col + dx }
ctx.setAnchor(next)
ctx.setExtent(next)
- ctx.cells.get(`${next.row}.${next.col}`)?.focus()
+ ctx.focusCell(next)
}
}
diff --git a/packages/dashboard-ui/src/spree/data-grid/context.ts b/packages/dashboard-ui/src/spree/data-grid/context.ts
index 2203eb9349..ba99db9b6a 100644
--- a/packages/dashboard-ui/src/spree/data-grid/context.ts
+++ b/packages/dashboard-ui/src/spree/data-grid/context.ts
@@ -23,6 +23,12 @@ export interface DataGridContextValue {
/** Dispatchers — components react via subscribed selectors below. */
setAnchor: (coords: CellCoords | null) => void
+ /**
+ * Moves focus to a cell, waiting for it to register when the commit that
+ * asked for it also added the row. Prefer this over reading `cells`
+ * directly — a lookup that misses silently drops keyboard navigation.
+ */
+ focusCell: (coords: CellCoords) => void
setExtent: (coords: CellCoords | null) => void
setEditing: (coords: CellCoords | null) => void
diff --git a/packages/dashboard-ui/src/spree/data-grid/data-grid.tsx b/packages/dashboard-ui/src/spree/data-grid/data-grid.tsx
index c3d8715580..f64444ecca 100644
--- a/packages/dashboard-ui/src/spree/data-grid/data-grid.tsx
+++ b/packages/dashboard-ui/src/spree/data-grid/data-grid.tsx
@@ -83,11 +83,37 @@ function DataGridShell<T>({
)
}, [])
+ // A cell the grid wants focused that has not registered yet. Cells register
+ // in an effect, so a commit that ADDS a row — a spreadsheet whose last line
+ // is a blank waiting to be filled — asks for a cell that does not exist
+ // until the next render. Without this the focus is dropped on the floor and
+ // the arrow keys stop responding, since nothing holds the selection
+ // (docs/plans/6.0-volume-pricing.md).
+ const pendingFocusRef = useRef<CellCoords | null>(null)
+
+ const focusCell = useCallback((coords: CellCoords) => {
+ const target = cellsRef.current.get(cellKey(coords))
+ if (target) {
+ target.focus()
+ return
+ }
+ pendingFocusRef.current = coords
+ }, [])
+
const registerCell = useCallback(
(reg: CellRegistration) => {
const key = cellKey(reg.coords)
cellsRef.current.set(key, reg)
recomputeBounds()
+
+ // The cell someone asked for has arrived — give it the focus it was
+ // promised, after this render commits.
+ const pending = pendingFocusRef.current
+ if (pending && cellKey(pending) === key) {
+ pendingFocusRef.current = null
+ queueMicrotask(() => cellsRef.current.get(key)?.focus())
+ }
+
return () => {
cellsRef.current.delete(key)
recomputeBounds()
@@ -121,10 +147,11 @@ function DataGridShell<T>({
setExtent,
setEditing,
registerCell,
+ focusCell,
isSelected,
selectedCells,
}),
- [bounds, anchor, extent, editing, registerCell, isSelected, selectedCells],
+ [bounds, anchor, extent, editing, registerCell, focusCell, isSelected, selectedCells],
)
return (
diff --git a/packages/dashboard-ui/src/spree/data-grid/use-data-grid-keyboard.ts b/packages/dashboard-ui/src/spree/data-grid/use-data-grid-keyboard.ts
index 153bbc6cf1..89477ced47 100644
--- a/packages/dashboard-ui/src/spree/data-grid/use-data-grid-keyboard.ts
+++ b/packages/dashboard-ui/src/spree/data-grid/use-data-grid-keyboard.ts
@@ -9,8 +9,10 @@ export function useDataGridKeyboard(gridRef: RefObject<HTMLElement | null>) {
const target = gridRef
function focusCoords(coords: CellCoords) {
- const cell = ctx.cells.get(`${coords.row}.${coords.col}`)
- cell?.focus()
+ // Through the grid rather than the registry: a cell added by the same
+ // commit has not registered yet, and a missed lookup silently drops
+ // keyboard navigation.
+ ctx.focusCell(coords)
}
function navigate(dx: number, dy: number) {