| 1 |
import { Dropdown, Tooltip } from "@wordpress/components"; |
| 2 |
import { useEffect, useRef, useState } from "@wordpress/element"; |
| 3 |
import icons from "./icon-lists"; |
| 4 |
import "./editor.scss"; |
| 5 |
|
| 6 |
const SPIconPicker = ({ |
| 7 |
attributes, |
| 8 |
attributesKey, |
| 9 |
setAttributes, |
| 10 |
label, |
| 11 |
closeBtn = true, |
| 12 |
defaultCategory = "All", |
| 13 |
}) => { |
| 14 |
const activeIconRef = useRef(null); |
| 15 |
const [searchQuery, setSearchQuery] = useState(""); |
| 16 |
const [selectedCategory, setSelectedCategory] = useState(defaultCategory); |
| 17 |
const [isDropdownOpen, setIsDropdownOpen] = useState(false); |
| 18 |
const handleChange = (newIcon) => { |
| 19 |
// const icon = newIcon.split( ' ' )[ 1 ]; |
| 20 |
const icon = newIcon; |
| 21 |
setAttributes({ [attributesKey]: icon }); |
| 22 |
}; |
| 23 |
const handleClose = () => { |
| 24 |
setAttributes({ [attributesKey]: "" }); |
| 25 |
}; |
| 26 |
const flatIcons = selectedCategory === "All" ? Object.values(icons).flat() : icons[selectedCategory]; |
| 27 |
// const activeIcon = 'fa ' + attributes; |
| 28 |
const activeIcon = attributes; |
| 29 |
const categories = Object.keys(icons); |
| 30 |
// Filter icons based on search query |
| 31 |
const filteredIcons = flatIcons.filter((icon) => icon.toLowerCase().includes(searchQuery.toLowerCase())); |
| 32 |
|
| 33 |
const iconNameHandle = (icon) => { |
| 34 |
const iconName = icon?.replace("fa fa-", "")?.replaceAll("-", " "); |
| 35 |
const capitalizedName = iconName?.replace(/\b\w/g, (char) => char.toUpperCase()); |
| 36 |
return capitalizedName; |
| 37 |
}; |
| 38 |
|
| 39 |
useEffect(() => { |
| 40 |
if (isDropdownOpen && activeIconRef.current) { |
| 41 |
setTimeout(() => { |
| 42 |
activeIconRef.current.scrollIntoView({ |
| 43 |
behavior: "smooth", |
| 44 |
block: "center", |
| 45 |
}); |
| 46 |
}, 100); |
| 47 |
} |
| 48 |
}, [isDropdownOpen]); |
| 49 |
|
| 50 |
return ( |
| 51 |
<div className="sp-smart-post-icon-picker-area sp-smart-post-component-mb"> |
| 52 |
<span className="sp-smart-post-component-title">{label}</span> |
| 53 |
{/* modal */} |
| 54 |
<Dropdown |
| 55 |
position="top right" |
| 56 |
renderToggle={({ isOpen, onToggle }) => ( |
| 57 |
<div |
| 58 |
onClick={() => { |
| 59 |
setIsDropdownOpen((prev) => !prev); |
| 60 |
onToggle(); |
| 61 |
}} |
| 62 |
aria-expanded={isOpen} |
| 63 |
> |
| 64 |
<div className="sp-smart-post-media-control__wrapper sp-smart-post-icon-picker"> |
| 65 |
{attributes ? ( |
| 66 |
<> |
| 67 |
<div className="sp-smart-post-icon-picker-active-icon"> |
| 68 |
<i className={activeIcon}></i> |
| 69 |
</div> |
| 70 |
{closeBtn && ( |
| 71 |
<button |
| 72 |
className="sp-smart-post-icon-picker-close-btn sp-smart-post-icon-picker-btn" |
| 73 |
onClick={handleClose} |
| 74 |
> |
| 75 |
<i className="fa fa-times" /> |
| 76 |
</button> |
| 77 |
)} |
| 78 |
</> |
| 79 |
) : ( |
| 80 |
<button className="sp-smart-post-icon-picker-add-btn sp-smart-post-icon-picker-btn"> |
| 81 |
<i className="fa fa-plus" /> |
| 82 |
</button> |
| 83 |
)} |
| 84 |
<div className="sp-smart-post-media-picker-tooltip"> |
| 85 |
<span>{attributes ? "Change Icon" : "Choose Icon"}</span> |
| 86 |
</div> |
| 87 |
</div> |
| 88 |
</div> |
| 89 |
)} |
| 90 |
onClose={(event, isInside) => { |
| 91 |
setIsDropdownOpen(false); |
| 92 |
if (isInside) { |
| 93 |
event.stopPropagation(); |
| 94 |
return; |
| 95 |
} |
| 96 |
}} |
| 97 |
renderContent={() => ( |
| 98 |
<div |
| 99 |
onMouseDown={(event) => { |
| 100 |
event.stopPropagation(); |
| 101 |
}} |
| 102 |
className="sp-smart-post-icon-picker-popup" |
| 103 |
> |
| 104 |
<div className="sp-smart-post-icon-picker-popup-top-section"> |
| 105 |
<span className="sp-smart-post-icon-search-heading">Select an Icon</span> |
| 106 |
<span className="sp-smart-post-icon-search-sub-heading"> |
| 107 |
Choose an icon for this collection. |
| 108 |
</span> |
| 109 |
<div className="sp-smart-post-icon-picker-search-and-category-wrapper"> |
| 110 |
{/* Search Input */} |
| 111 |
<input |
| 112 |
type="text" |
| 113 |
placeholder="Search..." |
| 114 |
value={searchQuery} |
| 115 |
onChange={(e) => setSearchQuery(e.target.value)} |
| 116 |
className="sp-smart-post-icon-picker-search" |
| 117 |
/> |
| 118 |
{/* Category Dropdown */} |
| 119 |
<select |
| 120 |
className="sp-smart-post-icon-picker-category" |
| 121 |
value={selectedCategory} |
| 122 |
onChange={(e) => setSelectedCategory(e.target.value)} |
| 123 |
> |
| 124 |
<option value="All">All</option> |
| 125 |
{categories.map((category, index) => ( |
| 126 |
<option key={index} value={category}> |
| 127 |
{category} |
| 128 |
</option> |
| 129 |
))} |
| 130 |
</select> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
<div className="sp-smart-post-icon-picker-list"> |
| 134 |
{filteredIcons?.map((icon, index) => ( |
| 135 |
<Tooltip |
| 136 |
key={index} |
| 137 |
text={iconNameHandle(icon)} |
| 138 |
className="sp-smart-post-component-tooltip" |
| 139 |
position="top" |
| 140 |
> |
| 141 |
<span |
| 142 |
key={index} |
| 143 |
ref={activeIcon === icon ? activeIconRef : null} |
| 144 |
className={`sp-smart-post-icon-picker-item ${ |
| 145 |
activeIcon === icon ? "active" : "" |
| 146 |
}`} |
| 147 |
onClick={() => handleChange(icon)} |
| 148 |
> |
| 149 |
<i className={icon}></i> |
| 150 |
</span> |
| 151 |
</Tooltip> |
| 152 |
))} |
| 153 |
</div> |
| 154 |
</div> |
| 155 |
)} |
| 156 |
/> |
| 157 |
</div> |
| 158 |
); |
| 159 |
}; |
| 160 |
|
| 161 |
export default SPIconPicker; |
| 162 |
|