import { Modal, Button } from "@wordpress/components"; import { useState } from "@wordpress/element"; import "./editor.scss"; import Divider from "../divider/divider"; import useIconList from "./useIconList"; import { CloseIcon } from "../../prebuild-library/icons"; const IconsLibrary = ({ attributes, attributesKey, setAttributes, onChange = false, pro = false }) => { let iconList = useIconList(); const activeIcon = iconList[attributes]; const [styles, setStyle] = useState([]); const [isOpen, setOpen] = useState(false); const [iconName, setIconName] = useState(""); const [searchValue, setSearchValue] = useState(""); const [iconCat, setIconCat] = useState("all-icons"); const openModal = () => setOpen(true); const closeModal = () => setOpen(false); function handleRemoveIcon() { setAttributes({ [attributesKey]: "", }); setOpen(true); } function shortenLabel(text) { if (text.includes("-")) { return text.split("-")[0]; } const parts = text.trim().split(/\s+/); return parts.length > 1 ? parts[0] + " …" : parts[0]; } const stylesType = ["Solid", "Regular", "Light"]; // const catList = ["All Icons", "Accessibility", "Alert", "Animals", "Arrows", "Media", "Business", "Charity"]; const catList = ["All Icons", "Accessibility", "Alert", "Animals", "Arrows", "Media"]; const handleStyleChange = (e) => { const { value, checked } = e.target; setStyle( (prev) => (checked ? [...prev, value] : prev.filter((item) => item !== value)) // remove unselected ); }; if (styles.length > 0) { iconList = Object.fromEntries( Object.entries(iconList)?.filter(([_, icon]) => icon?.style?.some((style) => styles.includes(style))) ); } if ("all-icons" !== iconCat) { iconList = Object.fromEntries( Object.entries(iconList)?.filter(([_, icon]) => icon?.category?.some((cat) => iconCat === cat)) ); } if ("" !== searchValue) { iconList = Object.fromEntries( Object.entries(iconList).filter(([_, icon]) => icon.label.toLowerCase().includes(searchValue.toLowerCase())) ); } function handleIconChange() { if (onChange) { onChange(iconName); } else { setAttributes({ [attributesKey]: iconName, }); } closeModal(); } return ( <>