"use client"; import React, { type ReactNode, useState } from "react"; import { cx } from "../lib/utils"; import { OnboardingButton } from "../primitives/onboarding-button"; import { OnboardingCard } from "../primitives/onboarding-card"; import { OnboardingCheckbox } from "../primitives/onboarding-checkbox"; import { OnboardingLabel } from "../primitives/onboarding-label"; import { OnboardingRadioCardGroup, OnboardingRadioCardItem, } from "../primitives/onboarding-radio-card"; export interface BooleanField { /** Unique identifier for the field */ id: string; /** Label/question text */ label: string; /** Optional description/helper text */ description?: string; /** Display mode: 'toggle' for checkbox, 'yesno' for Yes/No radio buttons */ mode?: "toggle" | "yesno"; /** Default value */ defaultValue?: boolean; /** Whether this field is required to be true (e.g., terms acceptance) */ requiredTrue?: boolean; /** Custom yes/no labels */ yesLabel?: string; noLabel?: string; } export interface BooleanStepProps { /** Title displayed at the top of the step */ title?: string; /** Description text below the title */ description?: string | ReactNode; /** Array of boolean fields */ fields: BooleanField[]; /** Called when any value changes */ onValuesChange?: (values: Record) => void; /** Called when the user submits the form */ onSubmit: (values: Record) => void | Promise; /** Text for the submit button */ submitText?: string; /** Text shown while submitting */ loadingText?: string; /** Optional back button config */ backButton?: { text: string; onClick: () => void; }; } export function BooleanStep({ title = "Confirm your preferences", description = "Please review and confirm the options below.", fields, onValuesChange, onSubmit, submitText = "Continue", loadingText = "Submitting...", backButton, }: BooleanStepProps) { const [values, setValues] = useState>(() => { const initial: Record = {}; fields.forEach((field) => { initial[field.id] = field.defaultValue ?? false; }); return initial; }); const [loading, setLoading] = useState(false); const handleChange = (fieldId: string, value: boolean) => { const newValues = { ...values, [fieldId]: value }; setValues(newValues); onValuesChange?.(newValues); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { await onSubmit(values); } finally { setLoading(false); } }; const isValid = fields.every((field) => { if (field.requiredTrue && !values[field.id]) return false; return true; }); return (

{title}

{description}

{fields.map((field, index) => (
{field.mode === "yesno" ? (
{field.label} {field.requiredTrue && ( * )} {field.description && (

{field.description}

)} handleChange(field.id, value === "yes") } className="grid grid-cols-2 gap-3" > {field.yesLabel || "Yes"} {field.noLabel || "No"}
) : ( handleChange(field.id, checked === true) } className="mt-0.5" />
{field.label} {field.requiredTrue && ( * )} {field.description && (

{field.description}

)}
)}
))}
{backButton && ( {backButton.text} )} {loading ? loadingText : submitText}
); }