"use client"; import * as React from "react"; import { Check, ChevronsUpDown } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; interface CategoryAutocompleteProps { value: string; onChange: (value: string) => void; suggestions: string[]; placeholder?: string; className?: string; disabled?: boolean; "data-testid"?: string; } export function CategoryAutocomplete({ value, onChange, suggestions, placeholder = "Select or type a category...", className, disabled = false, "data-testid": testId, }: CategoryAutocompleteProps) { const [open, setOpen] = React.useState(false); const [triggerWidth, setTriggerWidth] = React.useState(0); const triggerRef = React.useRef(null); // Update trigger width when component mounts or value changes React.useEffect(() => { if (triggerRef.current) { const updateWidth = () => { setTriggerWidth(triggerRef.current?.offsetWidth || 0); }; updateWidth(); // Listen for resize events to handle responsive behavior const resizeObserver = new ResizeObserver(updateWidth); resizeObserver.observe(triggerRef.current); return () => { resizeObserver.disconnect(); }; } }, [value]); return ( No category found. {suggestions.map((suggestion) => ( { onChange(currentValue === value ? "" : currentValue); setOpen(false); }} data-testid={`category-option-${suggestion.toLowerCase().replace(/\s+/g, "-")}`} > {suggestion} ))} ); }