import * as React from "react" import { Command, CommandInput } from "./ui/command" import { useEffect } from "react" type CommandPopoverProps = React.PropsWithChildren<{ placeholder?: string value?: string selectedValue?: unknown onValueChange?: (value: string) => void }> export function CommandPopover({ placeholder, value, onValueChange, selectedValue, children, }: CommandPopoverProps) { const [open, setOpen] = React.useState(false) const inputRef = React.useRef(null) const commandRef = React.useRef(null) const handleInputChange = (value: string) => { onValueChange?.(value) if (!open) setOpen(true) } // Handle click outside useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if ( commandRef.current && !commandRef.current.contains(event.target as Node) && inputRef.current && !inputRef.current.contains(event.target as Node) ) { setOpen(false) } } document.addEventListener("mousedown", handleClickOutside) return () => { document.removeEventListener("mousedown", handleClickOutside) } }, []) useEffect(() => { setOpen(false) onValueChange?.("") // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedValue]) // Handle keyboard events for showing/hiding the command menu const handleKeyDown = (e: React.KeyboardEvent) => { // Show the command menu on arrow down when it's closed if (e.key === "ArrowDown" && !open) { e.preventDefault() setOpen(true) } // Hide the command menu on escape if (e.key === "Escape" && open) { e.preventDefault() setOpen(false) } } return (
setOpen(true)} placeholder={placeholder} className="border-none focus:ring-0" /> {open && (
{children}
)}
) }