({
value,
options,
onChange,
className = "",
disabled = false,
placeholder = "",
iconDropdown = false,
direction = "down",
}: DropdownProps)
| 25 | } |
| 26 | |
| 27 | export const Dropdown = ({ |
| 28 | value, |
| 29 | options, |
| 30 | onChange, |
| 31 | className = "", |
| 32 | disabled = false, |
| 33 | placeholder = "", |
| 34 | iconDropdown = false, |
| 35 | direction = "down", |
| 36 | }: DropdownProps) => { |
| 37 | const [isOpen, setIsOpen] = useState(false); |
| 38 | const dropdownRef = useRef<HTMLDivElement>(null); |
| 39 | |
| 40 | const selectedOption = options.find((opt) => opt.id === value); |
| 41 | |
| 42 | useEffect(() => { |
| 43 | function handleClickOutside(event: MouseEvent) { |
| 44 | if ( |
| 45 | dropdownRef.current && |
| 46 | !dropdownRef.current.contains(event.target as Node) |
| 47 | ) { |
| 48 | setIsOpen(false); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | document.addEventListener("mousedown", handleClickOutside); |
| 53 | return () => document.removeEventListener("mousedown", handleClickOutside); |
| 54 | }, []); |
| 55 | |
| 56 | const handleSelect = (e: React.MouseEvent, optionId: string) => { |
| 57 | e.preventDefault(); |
| 58 | e.stopPropagation(); |
| 59 | onChange(optionId); |
| 60 | setIsOpen(false); |
| 61 | }; |
| 62 | |
| 63 | return ( |
| 64 | <div className={`jotty-dropdown relative ${className}`} ref={dropdownRef}> |
| 65 | {iconDropdown ? ( |
| 66 | <div |
| 67 | className="jotty-dropdown-icon flex items-center gap-2 cursor-pointer" |
| 68 | onClick={() => setIsOpen(!isOpen)} |
| 69 | > |
| 70 | <MoreHorizontalIcon |
| 71 | className={cn( |
| 72 | `h-4 w-4 transition-transform text-muted-foreground`, |
| 73 | isOpen ? "rotate-180" : "", |
| 74 | disabled && "opacity-50 text-muted-foreground" |
| 75 | )} |
| 76 | /> |
| 77 | </div> |
| 78 | ) : ( |
| 79 | <button |
| 80 | type="button" |
| 81 | onClick={(e) => { |
| 82 | e.preventDefault(); |
| 83 | if (!disabled) { |
| 84 | setIsOpen(!isOpen); |
nothing calls this directly
no test coverage detected