import React, { useEffect, useRef, useState } from 'react' import { Plus } from 'lucide-react' import { getColumnFieldTypes } from './fieldTypes' /** * "Add Field" button + menu of a container column (edit chrome, floats over the column). Lists every palette * field; picking one calls onAdd(type, label). Escape or a click outside closes the menu. */ export default function ColumnAddFieldMenu({ open, onToggle, onClose, onAdd }) { const [query, setQuery] = useState('') const menuRef = useRef(null) useEffect(() => { if (!open) { setQuery(''); return undefined } const onKey = (e) => { if (e.key === 'Escape') { e.stopPropagation(); onClose() } } const onDown = (e) => { if (menuRef.current && !menuRef.current.contains(e.target)) onClose() } document.addEventListener('keydown', onKey, true) document.addEventListener('mousedown', onDown) return () => { document.removeEventListener('keydown', onKey, true) document.removeEventListener('mousedown', onDown) } }, [open, onClose]) const items = getColumnFieldTypes().filter((t) => t.label.toLowerCase().includes(query.trim().toLowerCase())) return (
{open && (
e.stopPropagation()} onClick={(e) => e.stopPropagation()} className="absolute left-1/2 -translate-x-1/2 w-60 mt-1 bg-white border border-gray-200 rounded-xl shadow-lg z-50 p-2 text-[11px]" > setQuery(e.target.value)} onKeyDown={(e) => e.stopPropagation()} placeholder="Search fields..." aria-label="Search fields" className="w-full mb-1.5 px-2 py-1.5 border border-gray-200 rounded-md text-[11px] outline-none focus:border-gray-900" style={{ minHeight: 30 }} />
{items.map((t) => ( ))} {items.length === 0 &&
No matching fields
}
)}
) }