"use client"; import { useState, useRef, useEffect } from "react"; import { Control, useController } from "react-hook-form"; export type Option = { id: string; value: string; }; type ComboBoxRHFProps = { name: string; control: Control; options: Option[]; placeholder?: string; }; export default function ComboBoxR({ name, control, options, placeholder = "Option wählen...", }: ComboBoxRHFProps) { const { field: { value: formValue, onChange, onBlur }, } = useController({ name, control }); const [open, setOpen] = useState(false); const [highlight, setHighlight] = useState(0); const [inputValue, setInputValue] = useState(""); const wrapperRef = useRef(null); const inputRef = useRef(null); const divRef = useRef(null); // Sync Input mit RHF-Wert (wenn z.B. defaultValue gesetzt ist) useEffect(() => { const selected = options.find((o) => o.id === formValue); if (selected) setInputValue(selected.value); }, [formValue, options]); useEffect(() => { if (open) { setInputValue(""); requestAnimationFrame(() => { inputRef.current?.focus(); }); } }, [open]); // Klick außerhalb schließen useEffect(() => { function handleClickOutside(e: MouseEvent) { console.log('Close outside',open) if (!open) return; console.log('Close outside2') if (wrapperRef.current && !wrapperRef.current.contains(e.target as Node)) { closeOnly(); } } document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, [open]); function closeOnly() { console.log('Aufruf close only') setOpen(false); onBlur(); requestAnimationFrame(() => { divRef.current?.focus(); }); } function closeAndFocusDiv() { setOpen(false); onBlur(); requestAnimationFrame(() => { divRef.current?.focus(); }); } const filtered = options.filter((opt) => opt.value.toLowerCase().includes(inputValue.toLowerCase()) ); function selectOption(opt: Option) { setInputValue(opt.value); // Text anzeigen onChange(opt.id); // ID ins Form-State setOpen(false); requestAnimationFrame(() => { divRef.current?.focus(); }); } function handleKeyDown(e: React.KeyboardEvent) { if (!open) setOpen(true); if (e.key === "ArrowDown") { e.preventDefault(); setHighlight((h) => Math.min(h + 1, filtered.length - 1)); } if (e.key === "ArrowUp") { e.preventDefault(); setHighlight((h) => Math.max(h - 1, 0)); } if (e.key === "Enter") { e.preventDefault(); selectOption(filtered[highlight]); } if (e.key === "Escape") { setOpen(false); } if (e.key === "Tab") { closeAndFocusDiv(); } } const selectedValue = options.find((item) => item.id == formValue) return (
{open && { setInputValue(e.target.value); setOpen(true); }} onKeyDown={handleKeyDown} className="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500" /> } {!open && (
setOpen(true)} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); setOpen(true); } }} className="border px-3 py-2 rounded cursor-pointer focus:ring-2 focus:ring-blue-500" > {selectedValue?.value || placeholder}
)} {open && filtered.length > 0 && (
    {filtered.map((opt, i) => (
  • selectOption(opt)} onMouseEnter={() => setHighlight(i)} className={`px-3 py-2 cursor-pointer ${i === highlight ? "bg-blue-100" : "hover:bg-gray-100" }`} > {opt.value}
  • ))}
)}
); }