import { Dropdown, Tooltip } from "@wordpress/components"; import { useEffect, useRef, useState } from "@wordpress/element"; import icons from "./icon-lists"; import "./editor.scss"; const SPIconPicker = ({ attributes, attributesKey, setAttributes, label, closeBtn = true, defaultCategory = "All", }) => { const activeIconRef = useRef(null); const [searchQuery, setSearchQuery] = useState(""); const [selectedCategory, setSelectedCategory] = useState(defaultCategory); const [isDropdownOpen, setIsDropdownOpen] = useState(false); const handleChange = (newIcon) => { // const icon = newIcon.split( ' ' )[ 1 ]; const icon = newIcon; setAttributes({ [attributesKey]: icon }); }; const handleClose = () => { setAttributes({ [attributesKey]: "" }); }; const flatIcons = selectedCategory === "All" ? Object.values(icons).flat() : icons[selectedCategory]; // const activeIcon = 'fa ' + attributes; const activeIcon = attributes; const categories = Object.keys(icons); // Filter icons based on search query const filteredIcons = flatIcons.filter((icon) => icon.toLowerCase().includes(searchQuery.toLowerCase())); const iconNameHandle = (icon) => { const iconName = icon?.replace("fa fa-", "")?.replaceAll("-", " "); const capitalizedName = iconName?.replace(/\b\w/g, (char) => char.toUpperCase()); return capitalizedName; }; useEffect(() => { if (isDropdownOpen && activeIconRef.current) { setTimeout(() => { activeIconRef.current.scrollIntoView({ behavior: "smooth", block: "center", }); }, 100); } }, [isDropdownOpen]); return (
{label} {/* modal */} (
{ setIsDropdownOpen((prev) => !prev); onToggle(); }} aria-expanded={isOpen} >
{attributes ? ( <>
{closeBtn && ( )} ) : ( )}
{attributes ? "Change Icon" : "Choose Icon"}
)} onClose={(event, isInside) => { setIsDropdownOpen(false); if (isInside) { event.stopPropagation(); return; } }} renderContent={() => (
{ event.stopPropagation(); }} className="sp-smart-post-icon-picker-popup" >
Select an Icon Choose an icon for this collection.
{/* Search Input */} setSearchQuery(e.target.value)} className="sp-smart-post-icon-picker-search" /> {/* Category Dropdown */}
{filteredIcons?.map((icon, index) => ( handleChange(icon)} > ))}
)} />
); }; export default SPIconPicker;