"use client"; import { RiSearchLine } from "@remixicon/react"; import * as React from "react"; import { Command, CommandEmpty, CommandGroup, CommandItem, CommandList, } from "@/components/ui/command"; import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; export interface EstabelecimentoInputProps { id?: string; value: string; onChange: (value: string) => void; estabelecimentos: string[]; placeholder?: string; required?: boolean; maxLength?: number; } export function EstabelecimentoInput({ id, value, onChange, estabelecimentos = [], placeholder = "Ex.: Padaria, TransferĂȘncia, Saldo inicial", required = false, maxLength = 20, }: EstabelecimentoInputProps) { const [open, setOpen] = React.useState(false); const [searchValue, setSearchValue] = React.useState(""); const handleSelect = (selectedValue: string) => { onChange(selectedValue); setOpen(false); setSearchValue(""); }; const handleInputChange = (e: React.ChangeEvent) => { const newValue = e.target.value; onChange(newValue); setSearchValue(newValue); // Open popover when user types and there are suggestions if (newValue.length > 0 && estabelecimentos.length > 0) { setOpen(true); } }; const filteredEstabelecimentos = React.useMemo(() => { if (!searchValue) return estabelecimentos; const lowerSearch = searchValue.toLowerCase(); return estabelecimentos.filter((item) => item.toLowerCase().includes(lowerSearch), ); }, [estabelecimentos, searchValue]); return (
{estabelecimentos.length > 0 && ( )}
{estabelecimentos.length > 0 && ( e.preventDefault()} > Nenhum estabelecimento encontrado. {filteredEstabelecimentos.map((item) => ( handleSelect(item)} className="cursor-pointer" > {item} ))} )}
); }