"use client"; import { MapPin } from "lucide-react"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { SearchableSelect } from "~/components/ui/select"; import { US_STATES, ALL_COUNTRIES, POPULAR_COUNTRIES, formatPostalCode, PLACEHOLDERS, } from "~/lib/form-constants"; interface AddressFormProps { addressLine1: string; addressLine2: string; city: string; state: string; postalCode: string; country: string; onChange: (field: string, value: string) => void; errors?: { addressLine1?: string; addressLine2?: string; city?: string; state?: string; postalCode?: string; country?: string; }; required?: boolean; className?: string; } export function AddressForm({ addressLine1, addressLine2, city, state, postalCode, country, onChange, errors = {}, required = false, className = "", }: AddressFormProps) { const handlePostalCodeChange = (value: string) => { const formatted = formatPostalCode(value, country || "US"); onChange("postalCode", formatted); }; // Combine popular and all countries, removing duplicates const countryOptions = [ { value: "__placeholder__", label: "Select a country", disabled: true }, { value: "divider-popular", label: "Popular Countries", disabled: true }, ...POPULAR_COUNTRIES, { value: "divider-all", label: "All Countries", disabled: true }, ...ALL_COUNTRIES.filter( (c) => !POPULAR_COUNTRIES.some((p) => p.value === c.value), ), ]; const stateOptions = [ { value: "__placeholder__", label: "Select a state", disabled: true }, ...US_STATES, ]; return (
Address Information
{/* Address Line 1 */}
onChange("addressLine1", e.target.value)} placeholder={PLACEHOLDERS.addressLine1} className={errors.addressLine1 ? "border-destructive" : ""} /> {errors.addressLine1 && (

{errors.addressLine1}

)}
{/* Address Line 2 */}
onChange("addressLine2", e.target.value)} placeholder={PLACEHOLDERS.addressLine2} />
{/* City and State/Province */}
onChange("city", e.target.value)} placeholder={PLACEHOLDERS.city} className={errors.city ? "border-destructive" : ""} /> {errors.city && (

{errors.city}

)}
{country === "United States" ? ( onChange("state", value)} placeholder="Select a state" className={errors.state ? "border-destructive" : ""} /> ) : ( onChange("state", e.target.value)} placeholder="State/Province" className={errors.state ? "border-destructive" : ""} /> )} {errors.state && (

{errors.state}

)}
{/* Postal Code and Country */}
handlePostalCodeChange(e.target.value)} placeholder={ country === "United States" ? "12345" : PLACEHOLDERS.postalCode } className={errors.postalCode ? "border-destructive" : ""} maxLength={ country === "United States" ? 10 : country === "Canada" ? 7 : undefined } /> {errors.postalCode && (

{errors.postalCode}

)}
{ // Don't save the placeholder value if (value !== "__placeholder__") { onChange("country", value); // Reset state when country changes from United States if (value !== "United States" && state.length === 2) { onChange("state", ""); } } }} placeholder="Select a country" className={errors.country ? "border-destructive" : ""} renderOption={(option) => { if (option.value?.startsWith("divider-")) { return (
{option.label}
); } return option.label; }} isOptionDisabled={(option) => option.disabled || option.value?.startsWith("divider-") } /> {errors.country && (

{errors.country}

)}
); }