|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | +import { |
| 3 | + FaEllipsisV, |
| 4 | + FaEye, |
| 5 | + FaEdit, |
| 6 | + FaTrashAlt, |
| 7 | + FaClipboard, |
| 8 | +} from 'react-icons/fa'; |
| 9 | +import { toast } from 'react-toastify'; |
| 10 | + |
| 11 | +interface ActionDropdownProps { |
| 12 | + onView: () => void; |
| 13 | + onEdit?: () => void; |
| 14 | + onDelete?: () => void; |
| 15 | + canEditDelete: boolean; |
| 16 | + id?: string; |
| 17 | +} |
| 18 | + |
| 19 | +function ActionDropdown({ |
| 20 | + onView, |
| 21 | + onEdit, |
| 22 | + onDelete, |
| 23 | + canEditDelete, |
| 24 | + id, |
| 25 | +}: ActionDropdownProps) { |
| 26 | + const [isOpen, setIsOpen] = useState(false); |
| 27 | + const dropdownRef = useRef<HTMLDivElement>(null); |
| 28 | + |
| 29 | + const toggleDropdown = () => setIsOpen(!isOpen); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + const handleClickOutside = (event: MouseEvent) => { |
| 33 | + if ( |
| 34 | + dropdownRef.current && |
| 35 | + !dropdownRef.current.contains(event.target as Node) |
| 36 | + ) { |
| 37 | + setIsOpen(false); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + if (isOpen) { |
| 42 | + document.addEventListener('mousedown', handleClickOutside); |
| 43 | + } else { |
| 44 | + document.removeEventListener('mousedown', handleClickOutside); |
| 45 | + } |
| 46 | + |
| 47 | + return () => { |
| 48 | + document.removeEventListener('mousedown', handleClickOutside); |
| 49 | + }; |
| 50 | + }, [isOpen]); |
| 51 | + |
| 52 | + const copyToClipboard = (text: any) => { |
| 53 | + navigator.clipboard |
| 54 | + .writeText(text) |
| 55 | + .then(() => { |
| 56 | + toast.success('ID copied to clipboard!'); |
| 57 | + }) |
| 58 | + .catch((err) => { |
| 59 | + toast.error(`Failed to copy: ${err}`); |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="relative" ref={dropdownRef}> |
| 65 | + {/* Hide ellipsis icon if the dropdown is open */} |
| 66 | + {!isOpen && ( |
| 67 | + <button |
| 68 | + type="button" |
| 69 | + aria-label="Open actions menu" |
| 70 | + className="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300" |
| 71 | + onClick={toggleDropdown} |
| 72 | + > |
| 73 | + <FaEllipsisV className="text-lg" /> |
| 74 | + </button> |
| 75 | + )} |
| 76 | + |
| 77 | + {isOpen && ( |
| 78 | + <div className="absolute right-0 z-10 w-48 mt-2 bg-white border border-gray-200 rounded-md shadow-lg"> |
| 79 | + <button |
| 80 | + type="button" |
| 81 | + aria-label="View item" |
| 82 | + onClick={() => { |
| 83 | + onView(); |
| 84 | + setIsOpen(false); |
| 85 | + }} |
| 86 | + className="flex items-center w-full px-4 py-2 text-sm text-blue-600 hover:bg-blue-100 dark:text-blue-400 dark:hover:bg-blue-700" |
| 87 | + > |
| 88 | + <FaEye className="mr-2" /> |
| 89 | + View |
| 90 | + </button> |
| 91 | + {canEditDelete && ( |
| 92 | + <> |
| 93 | + <button |
| 94 | + type="button" |
| 95 | + aria-label="Edit item" |
| 96 | + onClick={() => { |
| 97 | + onEdit?.(); |
| 98 | + setIsOpen(false); |
| 99 | + }} |
| 100 | + className="flex items-center w-full px-4 py-2 text-sm text-yellow-600 hover:bg-yellow-100 dark:text-yellow-400 dark:hover:bg-yellow-700" |
| 101 | + > |
| 102 | + <FaEdit className="mr-2" /> |
| 103 | + Edit |
| 104 | + </button> |
| 105 | + <button |
| 106 | + type="button" |
| 107 | + aria-label="Delete item" |
| 108 | + onClick={() => { |
| 109 | + onDelete?.(); |
| 110 | + setIsOpen(false); |
| 111 | + }} |
| 112 | + className="flex items-center w-full px-4 py-2 text-sm text-red-600 hover:bg-red-100 dark:text-red-400 dark:hover:bg-red-700" |
| 113 | + > |
| 114 | + <FaTrashAlt className="mr-2" /> |
| 115 | + Delete |
| 116 | + </button> |
| 117 | + </> |
| 118 | + )} |
| 119 | + <button |
| 120 | + type="button" |
| 121 | + aria-label="Copy ID" |
| 122 | + onClick={() => { |
| 123 | + copyToClipboard(id); |
| 124 | + setIsOpen(false); |
| 125 | + }} |
| 126 | + className="flex items-center w-full px-4 py-2 text-sm text-gray-600 hover:bg-gray-100 dark:text-gray-400 dark:hover:bg-gray-700" |
| 127 | + > |
| 128 | + <FaClipboard className="mr-2" /> |
| 129 | + Copy ID |
| 130 | + </button> |
| 131 | + </div> |
| 132 | + )} |
| 133 | + </div> |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +export default ActionDropdown; |
0 commit comments