import type { KeyboardEvent, ReactNode } from "react"; import { Card } from "@mantine/core"; import classes from "./catalog-item.module.css"; interface CatalogItemProps { label: string; status?: string; selected?: boolean; disabled?: boolean; busy?: boolean; height?: number; onSelect: () => void; onFocus?: () => void; onPointerEnter?: () => void; onMoveFocus?: (direction: "next" | "previous" | "first" | "last") => void; children: ReactNode; } export const CatalogItem = ({ label, status, selected, disabled = false, busy = false, height, onSelect, onFocus, onPointerEnter, onMoveFocus, children, }: CatalogItemProps) => { const handleKeyDown = (event: KeyboardEvent) => { const direction = getCatalogKeyboardDirection(event.key); if (!direction) return; event.preventDefault(); if (onMoveFocus) onMoveFocus(direction); else moveCatalogFocus(event.currentTarget, direction); }; return ( {children} ); }; export const moveCatalogFocus = (current: HTMLButtonElement, direction: "next" | "previous" | "first" | "last") => { const items = Array.from( current.parentElement?.querySelectorAll("button[data-catalog-item]:not(:disabled)") ?? [], ); if (items.length === 0) return; const currentIndex = items.indexOf(current); const targetIndex = direction === "first" ? 0 : direction === "last" ? items.length - 1 : direction === "next" ? (currentIndex + 1) % items.length : (currentIndex - 1 + items.length) % items.length; items[targetIndex]?.focus(); }; export const getCatalogKeyboardDirection = (key: string) => { if (key === "ArrowRight" || key === "ArrowDown") return "next" as const; if (key === "ArrowLeft" || key === "ArrowUp") return "previous" as const; if (key === "Home") return "first" as const; if (key === "End") return "last" as const; return null; };