(event: ZrevEvent, ctx: TableRoutingCtx<T>)
| 28 | * - Ctrl+A: Select all (multi mode) |
| 29 | */ |
| 30 | export function routeTableKey<T>(event: ZrevEvent, ctx: TableRoutingCtx<T>): TableRoutingResult { |
| 31 | if (event.kind !== "key") return Object.freeze({ consumed: false }); |
| 32 | if (event.action !== "down") return Object.freeze({ consumed: false }); |
| 33 | if (!ctx.keyboardNavigation) return Object.freeze({ consumed: false }); |
| 34 | |
| 35 | const { tableId, rowKeys, rowKeyToIndex, rowHeight, state, selection, selectionMode } = ctx; |
| 36 | const { focusedRowIndex, scrollTop, viewportHeight, lastClickedKey } = state; |
| 37 | const rowCount = rowKeys.length; |
| 38 | |
| 39 | if (rowCount === 0) return Object.freeze({ consumed: false }); |
| 40 | const clampedFocusedRowIndex = Math.max(0, Math.min(rowCount - 1, focusedRowIndex)); |
| 41 | |
| 42 | // Keep keyboard routing consistent with renderer math. |
| 43 | const safeRowHeight = rowHeight > 0 ? rowHeight : 1; |
| 44 | const safeViewportHeight = Math.max(0, viewportHeight); |
| 45 | const maxScrollTop = Math.max(0, rowCount * safeRowHeight - safeViewportHeight); |
| 46 | const normalizedScrollTop = Number.isFinite(scrollTop) |
| 47 | ? Math.max(0, Math.min(maxScrollTop, scrollTop)) |
| 48 | : 0; |
| 49 | const consumedNoMove = (): TableRoutingResult => { |
| 50 | const scrollPatch = |
| 51 | normalizedScrollTop !== scrollTop ? { nextScrollTop: normalizedScrollTop } : {}; |
| 52 | const focusPatch = |
| 53 | clampedFocusedRowIndex !== focusedRowIndex |
| 54 | ? { nextFocusedRowIndex: clampedFocusedRowIndex } |
| 55 | : {}; |
| 56 | return Object.freeze({ |
| 57 | consumed: true, |
| 58 | ...scrollPatch, |
| 59 | ...focusPatch, |
| 60 | }); |
| 61 | }; |
| 62 | |
| 63 | // Helper to compute scroll position for a given row index |
| 64 | const scrollToRow = (rowIndex: number): number => { |
| 65 | const rowTop = rowIndex * safeRowHeight; |
| 66 | const rowBottom = rowTop + safeRowHeight; |
| 67 | const viewportBottom = normalizedScrollTop + safeViewportHeight; |
| 68 | |
| 69 | // If row is above viewport, scroll up |
| 70 | if (rowTop < normalizedScrollTop) { |
| 71 | return rowTop; |
| 72 | } |
| 73 | // If row is below viewport, scroll down |
| 74 | if (rowBottom > viewportBottom) { |
| 75 | return Math.max(0, Math.min(maxScrollTop, rowBottom - safeViewportHeight)); |
| 76 | } |
| 77 | // Row is visible |
| 78 | return normalizedScrollTop; |
| 79 | }; |
| 80 | |
| 81 | // Compute page size |
| 82 | const pageSize = Math.max(1, Math.floor(safeViewportHeight / safeRowHeight)); |
| 83 | |
| 84 | // Arrow Up |
| 85 | if (event.key === ZR_KEY_UP) { |
| 86 | const nextIndex = Math.max(0, clampedFocusedRowIndex - 1); |
| 87 | if (nextIndex === clampedFocusedRowIndex) return consumedNoMove(); |
no test coverage detected