import React, { useCallback, useRef } from "react"; export interface SelectableListProps { items: T[]; selectedValue?: T | null; renderLabel: (item: T) => React.ReactNode; onSelect: (item: T) => void; } export const SelectableList = ( props: SelectableListProps, ): JSX.Element => { const { items, selectedValue, renderLabel, onSelect } = props; 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" : ""; return (
  • onSelect(item)} className={className}> {renderLabel(item)}
  • ); })}
); };