From 237267ff3ac00dd82452894df7814f0fa7b2ffba Mon Sep 17 00:00:00 2001 From: Colin Dawson Date: Mon, 16 Feb 2026 00:00:17 +0000 Subject: [PATCH] Selectable list now responds to arrow key presses --- src/components/common/SelectableList.tsx | 38 ++++++++++++++++++++---- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/components/common/SelectableList.tsx b/src/components/common/SelectableList.tsx index 0df7dad..0ae0fdb 100644 --- a/src/components/common/SelectableList.tsx +++ b/src/components/common/SelectableList.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useCallback, useRef } from "react"; export interface SelectableListProps { items: T[]; @@ -12,15 +12,41 @@ export const SelectableList = ( ): JSX.Element => { const { items, selectedValue, renderLabel, onSelect } = props; - const listClassName = "selectable-list"; + const listRef = useRef(null); + + const handleKeyDown = useCallback( + (e: React.KeyboardEvent) => { + if (!items.length) return; + + const currentIndex = selectedValue ? items.indexOf(selectedValue) : -1; + + if (e.key === "ArrowDown") { + e.preventDefault(); + const nextIndex = + currentIndex < items.length - 1 ? currentIndex + 1 : 0; + onSelect(items[nextIndex]); + } + + if (e.key === "ArrowUp") { + e.preventDefault(); + const prevIndex = + currentIndex > 0 ? currentIndex - 1 : items.length - 1; + onSelect(items[prevIndex]); + } + }, + [items, selectedValue, onSelect], + ); return ( -
    +
      {items.map((item, index) => { const isSelected = selectedValue === item; - const className = isSelected - ? ["selected"].filter(Boolean).join(" ") - : ""; + const className = isSelected ? "selected" : ""; return (
    • onSelect(item)} className={className}>