"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Search, ScanLine, AlertCircle } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; interface SearchBarProps { variant?: "hero" | "compact" | "floating"; placeholder?: string; className?: string; } export function SearchBar({ variant = "hero", placeholder = "Digite o código de barras (ex: 7896004713588)", className = "", }: SearchBarProps) { const [barcode, setBarcode] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const router = useRouter(); const validateBarcode = (code: string) => { const cleanCode = code.replace(/\D/g, ""); return cleanCode.length >= 8 && cleanCode.length <= 13; }; const handleSearch = async (e: React.FormEvent) => { e.preventDefault(); setError(""); if (!barcode.trim()) { setError("Digite um código de barras válido"); return; } if (!validateBarcode(barcode)) { setError("Código de barras deve ter entre 8 e 13 dígitos"); return; } setLoading(true); // Simula busca (futuramente será integração com API) setTimeout(() => { router.push(`/produto/${barcode.trim()}`); }, 1000); }; const getVariantStyles = () => { switch (variant) { case "compact": return { container: "max-w-md mx-auto", input: "text-base py-3", button: "px-6 py-3", }; case "floating": return { container: "fixed bottom-6 left-1/2 transform -translate-x-1/2 z-50 max-w-sm w-full mx-4", input: "text-sm py-3", button: "px-4 py-3", }; default: // hero return { container: "max-w-2xl mx-auto", input: "text-lg py-4", button: "px-8 py-4", }; } }; const styles = getVariantStyles(); if (variant === "floating") { return (
setBarcode(e.target.value)} className={`pl-12 pr-4 ${styles.input} border-0 bg-transparent focus:ring-0 placeholder:text-gray-400`} />
{error && (
{error}
)}
); } return (
setBarcode(e.target.value)} className={`pl-12 pr-4 ${styles.input} border-0 bg-transparent focus:ring-0 placeholder:text-gray-400`} />
{error && (
{error}
)}
); }