Files
logbuch/components/ObjektSelector.tsx
Reinhard X. Fürst aea5cc08d6 Reduce form field height to show list entries below the form
py-3 → py-2 on all inputs, selects, buttons; space-y-5 → space-y-3;
textarea rows 3 → 2; label margin reduced.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-27 20:23:19 +02:00

117 lines
3.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { useEffect, useState } from 'react';
import type { ObjektOption, SelectedObjekt } from '@/types/logbuch';
import CustomSelect from './CustomSelect';
interface Props {
selected: SelectedObjekt[];
onChange: (objekte: SelectedObjekt[]) => void;
}
export default function ObjektSelector({ selected, onChange }: Props) {
const [all, setAll] = useState<ObjektOption[]>([]);
const [newName, setNewName] = useState('');
const [showNewInput, setShowNewInput] = useState(false);
useEffect(() => {
fetch('/api/objekte')
.then((r) => { if (!r.ok) throw new Error('Fehler'); return r.json(); })
.then(setAll)
.catch(() => {});
}, []);
const selectedNames = new Set(selected.map((o) => o.Name.toLowerCase()));
const available = all.filter((o) => !selectedNames.has(o.Name.toLowerCase()));
function add(value: string) {
const obj = all.find((o) => o.ID === parseInt(value));
if (obj && !selectedNames.has(obj.Name.toLowerCase())) {
onChange([...selected, { ID: obj.ID, Name: obj.Name }]);
}
}
function addNew() {
const name = newName.trim();
if (!name || selectedNames.has(name.toLowerCase())) return;
onChange([...selected, { ID: null, Name: name }]);
setNewName('');
setShowNewInput(false);
}
function remove(name: string) {
onChange(selected.filter((o) => o.Name !== name));
}
return (
<div className="space-y-3">
<div className="flex flex-wrap gap-2">
{selected.map((o) => (
<span
key={o.Name}
className="inline-flex items-center gap-2 bg-green-100 text-green-800 text-base px-3 py-1.5 rounded-full"
>
{o.Name}
<button
type="button"
onClick={() => remove(o.Name)}
className="flex items-center justify-center w-7 h-7 rounded-full text-green-600 hover:bg-red-100 hover:text-red-600 font-bold text-xl leading-none"
aria-label={`${o.Name} entfernen`}
>
×
</button>
</span>
))}
</div>
<div className="flex gap-2">
{available.length > 0 && (
<div className="flex-1">
<CustomSelect
placeholder="+ Objekte hinzufügen"
options={available.map((o) => ({ value: String(o.ID), label: o.Name }))}
onChange={add}
keepOpen
/>
</div>
)}
<button
type="button"
onClick={() => setShowNewInput((v) => !v)}
className="px-4 py-2 border-2 border-gray-400 rounded-lg bg-white text-base text-gray-700 hover:bg-gray-50 whitespace-nowrap"
>
+ Neu
</button>
</div>
{showNewInput && (
<div className="flex gap-2">
<input
type="text"
value={newName}
onChange={(e) => setNewName(e.target.value)}
onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); addNew(); } }}
placeholder="Objektname eingeben"
className="flex-1 px-3 py-2 border-2 border-gray-400 rounded-lg bg-white text-base focus:border-blue-500 focus:outline-none"
autoFocus
/>
<button
type="button"
onClick={addNew}
className="px-4 py-2 bg-green-600 text-white text-base rounded-lg hover:bg-green-700"
>
OK
</button>
<button
type="button"
onClick={() => { setShowNewInput(false); setNewName(''); }}
className="px-4 py-2 bg-gray-200 text-gray-700 text-base rounded-lg hover:bg-gray-300"
>
</button>
</div>
)}
</div>
);
}