(event: ZrevEvent, ctx: TreeRoutingCtx<T>)
| 34 | * - Asterisk (*): Expand all siblings |
| 35 | */ |
| 36 | export function routeTreeKey<T>(event: ZrevEvent, ctx: TreeRoutingCtx<T>): TreeRoutingResult { |
| 37 | if (event.kind !== "key") return Object.freeze({ consumed: false }); |
| 38 | if (event.action !== "down") return Object.freeze({ consumed: false }); |
| 39 | if (!ctx.keyboardNavigation) return Object.freeze({ consumed: false }); |
| 40 | |
| 41 | const { treeId, flatNodes, expanded, state } = ctx; |
| 42 | const { focusedKey } = state; |
| 43 | const nodeCount = flatNodes.length; |
| 44 | |
| 45 | if (nodeCount === 0) return Object.freeze({ consumed: false }); |
| 46 | |
| 47 | // Find current focused index |
| 48 | const currentIndex = focusedKey !== null ? findNodeIndex(flatNodes, focusedKey) : -1; |
| 49 | const currentNode = currentIndex >= 0 ? flatNodes[currentIndex] : undefined; |
| 50 | |
| 51 | // Arrow Up - move to previous visible node |
| 52 | if (event.key === ZR_KEY_UP) { |
| 53 | const nextIndex = currentIndex > 0 ? currentIndex - 1 : 0; |
| 54 | if (nextIndex === currentIndex && currentIndex >= 0) { |
| 55 | return Object.freeze({ consumed: true }); |
| 56 | } |
| 57 | |
| 58 | const nextNode = flatNodes[nextIndex]; |
| 59 | if (nextNode) { |
| 60 | return Object.freeze({ |
| 61 | nextFocusedKey: nextNode.key, |
| 62 | nodeToSelect: nextNode.key, |
| 63 | consumed: true, |
| 64 | }); |
| 65 | } |
| 66 | return Object.freeze({ consumed: true }); |
| 67 | } |
| 68 | |
| 69 | // Arrow Down - move to next visible node |
| 70 | if (event.key === ZR_KEY_DOWN) { |
| 71 | const nextIndex = currentIndex < nodeCount - 1 ? currentIndex + 1 : nodeCount - 1; |
| 72 | if (nextIndex === currentIndex) { |
| 73 | return Object.freeze({ consumed: true }); |
| 74 | } |
| 75 | |
| 76 | const nextNode = flatNodes[nextIndex]; |
| 77 | if (nextNode) { |
| 78 | return Object.freeze({ |
| 79 | nextFocusedKey: nextNode.key, |
| 80 | nodeToSelect: nextNode.key, |
| 81 | consumed: true, |
| 82 | }); |
| 83 | } |
| 84 | return Object.freeze({ consumed: true }); |
| 85 | } |
| 86 | |
| 87 | // Arrow Right - expand or move to first child |
| 88 | if (event.key === ZR_KEY_RIGHT) { |
| 89 | if (!currentNode) return Object.freeze({ consumed: true }); |
| 90 | |
| 91 | const isExpanded = expanded.includes(currentNode.key); |
| 92 | |
| 93 | if (currentNode.hasChildren) { |
no test coverage detected