'use client'; import { Button } from '@/components/ui/Button'; import { Checkbox } from '@/components/ui/Checkbox'; import { Heading, Label, Text } from '@/components/ui/Typography'; import { useState } from 'react'; interface ContactFormProps { title?: string; subtitle?: string; } export function ContactForm({ title = 'Kontaktiraj nas', subtitle = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' }: ContactFormProps) { // Form state const [formData, setFormData] = useState({ name: '', email: '', message: '', acceptTerms: false }); // Validation errors const [errors, setErrors] = useState>({}); // Form submission status const [isSubmitting, setIsSubmitting] = useState(false); const [isSubmitted, setIsSubmitted] = useState(false); // Handle input changes const handleChange = (e: React.ChangeEvent) => { const { name, value, type } = e.target; // Handle checkbox if (type === 'checkbox') { const checked = (e.target as HTMLInputElement).checked; setFormData(prev => ({ ...prev, [name]: checked })); } else { setFormData(prev => ({ ...prev, [name]: value })); } // Clear error when user starts typing if (errors[name]) { setErrors(prev => { const newErrors = { ...prev }; delete newErrors[name]; return newErrors; }); } }; // Handle checkbox change specifically const handleCheckboxChange = (e: React.ChangeEvent) => { const { name, checked } = e.target; setFormData(prev => ({ ...prev, [name]: checked })); // Clear error when user changes checkbox if (errors[name]) { setErrors(prev => { const newErrors = { ...prev }; delete newErrors[name]; return newErrors; }); } }; // Validate form const validateForm = () => { const newErrors: Record = {}; // Name validation if (!formData.name.trim()) { newErrors.name = 'Molimo unesite vaše ime i prezime'; } // Email validation if (!formData.email.trim()) { newErrors.email = 'Molimo unesite vašu email adresu'; } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) { newErrors.email = 'Molimo unesite ispravnu email adresu'; } // Message validation if (!formData.message.trim()) { newErrors.message = 'Molimo unesite vašu poruku'; } else if (formData.message.trim().length < 10) { newErrors.message = 'Vaša poruka mora sadržavati barem 10 znakova'; } // Terms validation if (!formData.acceptTerms) { newErrors.acceptTerms = 'Molimo prihvatite uvjete korištenja'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; // Handle form submission const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (validateForm()) { setIsSubmitting(true); // Simulate API call setTimeout(() => { setIsSubmitting(false); setIsSubmitted(true); // Reset form after submission setFormData({ name: '', email: '', message: '', acceptTerms: false }); // Hide success message after 5 seconds setTimeout(() => { setIsSubmitted(false); }, 5000); }, 1000); } }; return (
{title} {subtitle} {isSubmitted ? (
Vaša poruka je uspješno poslana! Hvala vam na vašem upitu. Odgovorit ćemo vam u najkraćem mogućem roku.
) : null}
{errors.name && {errors.name}}
{errors.email && {errors.email}}