import { useEffect } from 'react'; import { Form as RemixForm } from "@remix-run/react"; import { Input } from "~/components/ui/Input"; import { Textarea } from "~/components/ui/Textarea"; import { Button } from "~/components/ui/Button"; import { FormField } from "~/components/ui/FormField"; import { Form, FormActions, FormSection, FormGrid } from "~/components/ui/Form"; import { useFormValidation } from "~/hooks/useFormValidation"; import { customerSchema } from "~/lib/form-validation"; import type { Customer } from "~/types/database"; interface EnhancedCustomerFormProps { customer?: Customer; onCancel: () => void; errors?: Record; isLoading: boolean; onSubmit?: (data: any) => void; } export function EnhancedCustomerForm({ customer, onCancel, errors = {}, isLoading, onSubmit, }: EnhancedCustomerFormProps) { const { values, errors: validationErrors, touched, isValid, setValue, setTouched, reset, validate, getFieldProps, } = useFormValidation({ schema: customerSchema, initialValues: { name: customer?.name || "", phone: customer?.phone || "", email: customer?.email || "", address: customer?.address || "", }, validateOnChange: true, validateOnBlur: true, }); // Reset form when customer changes useEffect(() => { if (customer) { reset({ name: customer.name || "", phone: customer.phone || "", email: customer.email || "", address: customer.address || "", }); } else { reset({ name: "", phone: "", email: "", address: "", }); } }, [customer, reset]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const { isValid: formIsValid } = validate(); if (formIsValid && onSubmit) { onSubmit(values); } }; const isEditing = !!customer; const combinedErrors = { ...validationErrors, ...errors }; return (
{isEditing && ( )} {/* Customer Name */} {/* Phone Number */} {/* Email */} {/* Address */}