"use client"; import "./components/ui/tidal.css"; import { useEffect, useRef, useState, type Dispatch, type SetStateAction } from "react"; import { TidalAddressForm, type TidalAddressField, type TidalAddressOption, type TidalAddressValue } from "./components/ui/address-form"; import "./components/ui/address-form.css"; const tidalExampleStyles = ".tidal-scope .address-form-demo,\n.tidal-scope .address-form-demo * {\n box-sizing: border-box;\n}\n\n.tidal-scope .address-form-demo {\n display: grid;\n width: min(100%, 1040px);\n min-width: 0;\n gap: 22px;\n margin-inline: auto;\n padding: clamp(16px, 4vw, 42px);\n}\n\n@container tidal-preview (max-width: 390px) {\n .tidal-scope .address-form-demo {\n gap: 16px;\n padding: 12px;\n }\n}"; const COUNTRIES: readonly TidalAddressOption[] = [ { value: "US", label: "United States" }, { value: "CA", label: "Canada" }, ]; const REGIONS: Readonly> = { US: [ { value: "IL", label: "Illinois" }, { value: "IN", label: "Indiana" }, { value: "MI", label: "Michigan" }, ], CA: [ { value: "ON", label: "Ontario" }, { value: "QC", label: "Quebec" }, { value: "BC", label: "British Columbia" }, ], }; const INITIAL_SHIPPING: TidalAddressValue = { name: "Avery Morgan", company: "Northstar Studio", line1: "1440 Market Street", line2: "Suite 210", city: "Chicago", region: "IL", postalCode: "60607", country: "US", }; const INITIAL_BILLING: TidalAddressValue = { name: "Avery Morgan", company: "Northstar Studio", line1: "28 Wellington Street", line2: "", city: "Toronto", region: "ON", postalCode: "M5E 1C7", country: "CA", }; export function AddressFormDemo() { const [shipping, setShipping] = useState({ ...INITIAL_SHIPPING, }); const [billing, setBilling] = useState({ ...INITIAL_BILLING, }); const [sameAsShipping, setSameAsShipping] = useState(true); const [busy, setBusy] = useState(false); const [status, setStatus] = useState("No unsaved changes."); const timerRef = useRef(null); useEffect( () => () => { if (timerRef.current !== null) window.clearTimeout(timerRef.current); }, [], ); function cancelPending() { if (timerRef.current !== null) { window.clearTimeout(timerRef.current); timerRef.current = null; } setBusy(false); } function updateAddress( setter: Dispatch>, field: TidalAddressField, value: string, ) { cancelPending(); setter((current) => ({ ...current, [field]: value, ...(field === "country" ? { region: "" } : null), })); setStatus("Unsaved changes."); } return (
updateAddress(setShipping, field, value), countryOptions: COUNTRIES, regionOptions: REGIONS[shipping.country] ?? [], legend: "Shipping address", namePrefix: "shipping", fields: { name: { inputProps: { required: true, autoCapitalize: "words" }, }, line1: { inputProps: { required: true } }, city: { inputProps: { required: true } }, region: { selectProps: { required: true } }, postalCode: { inputProps: { required: true } }, country: { selectProps: { required: true } }, }, }} billing={{ value: billing, onValueChange: (field, value) => updateAddress(setBilling, field, value), countryOptions: COUNTRIES, regionOptions: REGIONS[billing.country] ?? [], legend: "Billing address", namePrefix: "billing", sameAsShipping, sameAsLabel: "Billing address matches shipping", onSameAsShippingChange: (checked) => { cancelPending(); setSameAsShipping(checked); setStatus( checked ? "Billing matches the shipping address." : "Enter a separate billing address.", ); }, fields: { name: { inputProps: { required: true } }, line1: { inputProps: { required: true } }, city: { inputProps: { required: true } }, region: { selectProps: { required: true } }, postalCode: { inputProps: { required: true } }, country: { selectProps: { required: true } }, }, }} busy={busy} onSubmit={(event) => { event.preventDefault(); if (busy) return; const submittedFields = Array.from( new FormData(event.currentTarget).keys(), ).length; setBusy(true); setStatus("Saving address…"); timerRef.current = window.setTimeout(() => { timerRef.current = null; setBusy(false); setStatus( `Address updated in this preview. ${submittedFields} fields checked.`, ); }, 600); }} onResetRequest={() => { cancelPending(); setShipping({ ...INITIAL_SHIPPING }); setBilling({ ...INITIAL_BILLING }); setSameAsShipping(true); setStatus("Address reset."); }} action="/local-address-preview" method="post" status={ {status} } submitButtonProps={{ formAction: "/local-address-preview", formMethod: "post", }} footer="Sample data. Changes stay in this preview." />
); } export default function AgentExample() { return (
); }