168 lines
5.2 KiB
TypeScript
168 lines
5.2 KiB
TypeScript
"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<any>;
|
|
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<HTMLDivElement | null>(null);
|
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
const divRef = useRef<HTMLDivElement | null>(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<HTMLInputElement>) {
|
|
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 (
|
|
<div ref={wrapperRef} className="relative w-64">
|
|
{open &&
|
|
<input
|
|
ref={inputRef}
|
|
value={inputValue}
|
|
placeholder={placeholder}
|
|
onChange={(e) => { 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 && (
|
|
<div
|
|
ref={divRef}
|
|
tabIndex={0}
|
|
onClick={() => 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}
|
|
</div>
|
|
)}
|
|
|
|
{open && filtered.length > 0 && (
|
|
<ul className="absolute top-full left-0 w-full bg-white border border-gray-300 rounded shadow-lg max-h-40 overflow-y-auto z-50">
|
|
{filtered.map((opt, i) => (
|
|
<li
|
|
key={opt.id}
|
|
onMouseDown={() => selectOption(opt)}
|
|
onMouseEnter={() => setHighlight(i)}
|
|
className={`px-3 py-2 cursor-pointer ${i === highlight ? "bg-blue-100" : "hover:bg-gray-100"
|
|
}`}
|
|
>
|
|
{opt.value}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|