import React, { useState, useRef, useEffect } from "react"; import { createPortal } from "react-dom"; import { Search, Mail, User, Building, Phone, Globe, Calendar, Clock, MapPin, FileText, Lock, Star, Heart, MessageSquare, Tag, Link2, Download, Upload, Edit, Trash2, Check, Plus, Minus, Settings, Info, HelpCircle, AlertCircle, AlertTriangle, CheckCircle, XCircle, X } from "lucide-react"; export const ICON_OPTIONS = [ { value: "none", label: "No Icon", icon: X }, { value: "mail", label: "Email", icon: Mail }, { value: "user", label: "User", icon: User }, { value: "building", label: "Building", icon: Building }, { value: "phone", label: "Phone", icon: Phone }, { value: "globe", label: "Globe", icon: Globe }, { value: "calendar", label: "Calendar", icon: Calendar }, { value: "clock", label: "Clock", icon: Clock }, { value: "map-pin", label: "Location", icon: MapPin }, { value: "file", label: "File", icon: FileText }, { value: "lock", label: "Lock", icon: Lock }, { value: "search", label: "Search", icon: Search }, { value: "star", label: "Star", icon: Star }, { value: "heart", label: "Heart", icon: Heart }, { value: "message", label: "Comment", icon: MessageSquare }, { value: "tag", label: "Tag", icon: Tag }, { value: "link", label: "Link", icon: Link2 }, { value: "download", label: "Download", icon: Download }, { value: "upload", label: "Upload", icon: Upload }, { value: "edit", label: "Edit", icon: Edit }, { value: "trash", label: "Trash", icon: Trash2 }, { value: "check", label: "Check", icon: Check }, { value: "plus", label: "Plus", icon: Plus }, { value: "minus", label: "Minus", icon: Minus }, { value: "settings", label: "Settings", icon: Settings }, { value: "info", label: "Info", icon: Info }, { value: "help", label: "Question", icon: HelpCircle }, { value: "alert", label: "Warning", icon: AlertTriangle }, { value: "success", label: "Success", icon: CheckCircle }, { value: "error", label: "Error", icon: XCircle } ]; export default function InlineIconEditor({ field, isOpen, onClose, onUpdate, anchorRef, setIsDirty, }) { const [searchTerm, setSearchTerm] = useState(""); const [position, setPosition] = useState({ top: 0, left: 0 }); const [iconColor, setIconColor] = useState(field.iconColor || "#6B7280"); const toolbarRef = useRef(null); const colorInputRef = useRef(null); useEffect(() => { if (field.iconColor) { setIconColor(field.iconColor); } }, [field.iconColor]); const filteredIcons = ICON_OPTIONS.filter(option => option.label.toLowerCase().includes(searchTerm.toLowerCase()) ); useEffect(() => { if (isOpen && anchorRef.current) { const updatePosition = () => { const anchorRect = anchorRef.current.getBoundingClientRect(); const toolbarWidth = 240; const toolbarHeight = 300; let top = anchorRect.bottom + 8; let left = anchorRect.left - (toolbarWidth / 2) + (anchorRect.width / 2); const viewportWidth = window.innerWidth; const viewportHeight = window.innerHeight; if (top + toolbarHeight > viewportHeight) { top = anchorRect.top - toolbarHeight - 8; } if (left < 8) left = 8; if (left + toolbarWidth > viewportWidth - 8) { left = viewportWidth - toolbarWidth - 8; } setPosition({ top, left }); }; updatePosition(); window.addEventListener("scroll", updatePosition, true); window.addEventListener("resize", updatePosition); return () => { window.removeEventListener("scroll", updatePosition, true); window.removeEventListener("resize", updatePosition); }; } }, [isOpen, anchorRef]); useEffect(() => { if (isOpen) { const handleClickOutside = (e) => { if (anchorRef.current && anchorRef.current.contains(e.target)) return; if (toolbarRef.current && toolbarRef.current.contains(e.target)) return; onClose(); }; document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); } }, [isOpen, onClose, anchorRef]); const handleColorChange = (newColor) => { setIconColor(newColor); if (setIsDirty) setIsDirty(true); onUpdate({ iconColor: newColor }); }; if (!isOpen) return null; return createPortal(
e.stopPropagation()} >
setSearchTerm(e.target.value)} className="w-full pl-7 pr-2 py-1 text-xs border border-gray-200 rounded-lg focus:outline-none focus:ring-1 focus:ring-gray-900 bg-white" autoFocus />
{filteredIcons.length > 0 ? (
{filteredIcons.map((option) => { const IconComp = option.icon; const isSelected = field.fieldIcon === option.value || field.icon === option.value; return ( ); })}
) : (
No icons found
)}
{/* Color Picker Section */}
Icon Color
{filteredIcons.length} icons
, document.body ); }