'use client' import { startTransition, type FormEvent, type ReactNode } from 'react' import { Button } from '@/components/ui/button' import { Spinner } from '@/components/ui/Spinner' import { cn } from '@/lib/utils' type WizardStepFormChromeCommonProps = { isPending: boolean /** sr-only announcement read out while the step is saving. */ pendingAnnouncement: string ctaLabel: string children: ReactNode leadingSubmit?: ReactNode submitBarClassName?: string ctaClassName?: string } /** * Shared chrome of the wizard final steps (D11): pending attributes, submit * bar with Spinner + "Salvando…" and the sr-only pending announcement. Two * modes — `action` renders a `
` with a submit CTA (the three * `useActionState` steps); `onCtaClick` renders a plain `
` with a * button CTA (the votes step, which saves via `useTransition` + fetch). */ export const WizardStepFormChrome = ( props: | ({ action: (formData: FormData) => void | Promise } & WizardStepFormChromeCommonProps) | ({ onCtaClick: () => void } & WizardStepFormChromeCommonProps), ) => { const { isPending, pendingAnnouncement, ctaLabel, children, leadingSubmit, submitBarClassName, ctaClassName, } = props const submitButton = ( <> {leadingSubmit} ) const pendingAttrs = { 'aria-busy': isPending || undefined, 'data-pending': isPending ? '' : undefined, } const content = ( <> {children}
{submitButton}
{isPending ? pendingAnnouncement : null}
) // C140 — manual dispatch (no `action={props.action}`): React 19 resets // uncontrolled fields after any settled form action, wiping typed values // on a validation error — the step stays visible, so the wipe showed. // `startTransition` keeps `isPending` correct for the imperative dispatch. const handleSubmit = 'action' in props ? (event: FormEvent) => { event.preventDefault() startTransition(() => props.action(new FormData(event.currentTarget))) } : undefined return 'action' in props ? ( {content} ) : (
{content}
) }