Initial commit: NestJS NextJS pnpm monorepo
This commit is contained in:
commit
610dcf202b
175 files changed
+25172
No files matched your search
@@ -0,0 +1,133 @@
|
||||
"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);
|
||||
|
||||
useEffect(() => {
|
||||
console.log('Change Value' + formValue)
|
||||
}, [formValue])
|
||||
// Sync Input mit RHF-Wert (wenn z.B. defaultValue gesetzt ist)
|
||||
useEffect(() => {
|
||||
console.log(`FromValue(${formValue})`)
|
||||
const selected = options.find((o) => o.id === formValue);
|
||||
if (selected) {
|
||||
setInputValue(selected.value);
|
||||
}
|
||||
}, [formValue, options]);
|
||||
|
||||
// Klick außerhalb schließen
|
||||
useEffect(() => {
|
||||
function handleClickOutside(e: MouseEvent) {
|
||||
if (
|
||||
wrapperRef.current &&
|
||||
!wrapperRef.current.contains(e.target as Node)
|
||||
) {
|
||||
setOpen(false);
|
||||
onBlur();
|
||||
}
|
||||
}
|
||||
document.addEventListener("mousedown", handleClickOutside);
|
||||
return () =>
|
||||
document.removeEventListener("mousedown", handleClickOutside);
|
||||
}, [onBlur]);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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") {
|
||||
setOpen(false);
|
||||
onBlur();
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div ref={wrapperRef} className="relative w-64">
|
||||
<input
|
||||
value={inputValue}
|
||||
placeholder={placeholder}
|
||||
onChange={(e) => {
|
||||
setInputValue(e.target.value); // ✅ frei tippen
|
||||
setOpen(true);
|
||||
}}
|
||||
onFocus={() => setOpen(true)}
|
||||
onBlur={(e) => {
|
||||
if (!wrapperRef.current?.contains(e.relatedTarget as Node)) {
|
||||
setOpen(false);
|
||||
onBlur();
|
||||
}
|
||||
}}
|
||||
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 && 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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user