|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { FaEllipsisV, FaEye, FaEdit, FaTrashAlt } from 'react-icons/fa'; |
| 3 | + |
| 4 | +interface ActionDropdownProps { |
| 5 | + onView: () => void; |
| 6 | + onEdit?: () => void; // Optional to allow conditional rendering |
| 7 | + onDelete?: () => void; // Optional to allow conditional rendering |
| 8 | + canEditDelete: boolean; // Prop to control visibility of Edit and Delete |
| 9 | +} |
| 10 | + |
| 11 | +function ActionDropdown({ |
| 12 | + onView, |
| 13 | + onEdit, |
| 14 | + onDelete, |
| 15 | + canEditDelete, |
| 16 | +}: ActionDropdownProps) { |
| 17 | + const [isOpen, setIsOpen] = useState(false); |
| 18 | + |
| 19 | + const toggleDropdown = () => setIsOpen(!isOpen); |
| 20 | + |
| 21 | + return ( |
| 22 | + <div className="relative"> |
| 23 | + <button |
| 24 | + type="button" |
| 25 | + aria-label="Open actions menu" |
| 26 | + className="text-gray-500 hover:text-gray-700" |
| 27 | + onClick={toggleDropdown} |
| 28 | + > |
| 29 | + <FaEllipsisV className="text-2xl" /> |
| 30 | + </button> |
| 31 | + {isOpen && ( |
| 32 | + <div className="absolute right-0 z-10 w-48 mt-2 bg-white border border-gray-200 rounded-md shadow-lg"> |
| 33 | + <button |
| 34 | + type="button" |
| 35 | + aria-label="View item" |
| 36 | + onClick={() => { |
| 37 | + onView(); |
| 38 | + setIsOpen(false); |
| 39 | + }} |
| 40 | + className="flex items-center w-full px-4 py-2 text-sm text-blue-600 hover:bg-blue-100" |
| 41 | + > |
| 42 | + <FaEye className="mr-2" /> |
| 43 | + View |
| 44 | + </button> |
| 45 | + {canEditDelete && ( |
| 46 | + <> |
| 47 | + <button |
| 48 | + type="button" |
| 49 | + aria-label="Edit item" |
| 50 | + onClick={() => { |
| 51 | + onEdit?.(); |
| 52 | + setIsOpen(false); |
| 53 | + }} |
| 54 | + className="flex items-center w-full px-4 py-2 text-sm text-yellow-600 hover:bg-yellow-100" |
| 55 | + > |
| 56 | + <FaEdit className="mr-2" /> |
| 57 | + Edit |
| 58 | + </button> |
| 59 | + <button |
| 60 | + type="button" |
| 61 | + aria-label="Delete item" |
| 62 | + onClick={() => { |
| 63 | + onDelete?.(); |
| 64 | + setIsOpen(false); |
| 65 | + }} |
| 66 | + className="flex items-center w-full px-4 py-2 text-sm text-red-600 hover:bg-red-100" |
| 67 | + > |
| 68 | + <FaTrashAlt className="mr-2" /> |
| 69 | + Delete |
| 70 | + </button> |
| 71 | + </> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + )} |
| 75 | + </div> |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +export default ActionDropdown; |
0 commit comments