1import React, { useState } from "react";
2import { Formik, Form, Field, ErrorMessage } from "formik";
3import * as Yup from "yup";
4
5// Define individual schemas for each step
6const stepSchemas = [
7 // Step 1 Schema: Personal Details
8 Yup.object({
9 firstName: Yup.string().required("First name is required"),
10 lastName: Yup.string().required("Last name is required"),
11 email: Yup.string().email("Invalid email").required("Email is required"),
12 }),
13 // Step 2 Schema: Organization Details with conditional field
14 Yup.object({
15 companyName: Yup.string().required("Company name is required"),
16 hasTaxId: Yup.boolean(),
17 taxId: Yup.string().when("hasTaxId", {
18 is: true,
19 then: (schema) => schema.required("Tax ID is required when registered"),
20 otherwise: (schema) => schema.optional(),
21 }),
22 }),
23 // Step 3 Schema: Plan Selection
24 Yup.object({
25 planTier: Yup.string().oneOf(["starter", "growth", "enterprise"]).required("Select a tier"),
26 }),
27];
28
29export function MultiStepOnboardingWizard() {
30 const [stepIndex, setStepIndex] = useState(0);
31 const isLastStep = stepIndex === stepSchemas.length - 1;
32
33 const currentValidationSchema = stepSchemas[stepIndex];
34
35 return (
36 <div className="max-w-xl mx-auto p-8 bg-slate-900 border border-slate-800 rounded-2xl text-white">
37 {/* Step Indicator */}
38 <div className="flex justify-between items-center mb-8 border-b border-slate-800 pb-4">
39 {["Identity", "Organization", "Subscription"].map((label, idx) => (
40 <div key={label} className="flex items-center gap-2">
41 <span
42 className={`h-7 w-7 rounded-full flex items-center justify-center text-xs font-bold ${
43 idx === stepIndex
44 ? "bg-cyan-500 text-slate-950"
45 : idx < stepIndex
46 ? "bg-emerald-500/20 text-emerald-400 border border-emerald-500"
47 : "bg-slate-800 text-slate-500"
48 }`}
49 >
50 {idx + 1}
51 </span>
52 <span className={`text-xs font-medium ${idx === stepIndex ? "text-cyan-400" : "text-slate-500"}`}>
53 {label}
54 </span>
55 </div>
56 ))}
57 </div>
58
59 <Formik
60 initialValues={{
61 firstName: "",
62 lastName: "",
63 email: "",
64 companyName: "",
65 hasTaxId: false,
66 taxId: "",
67 planTier: "growth",
68 }}
69 validationSchema={currentValidationSchema}
70 onSubmit={async (values, { setSubmitting }) => {
71 if (!isLastStep) {
72 // Advance to next step; Formik preserves all existing values in memory!
73 setStepIndex((curr) => curr + 1);
74 setSubmitting(false);
75 } else {
76 // Final submission
77 console.log("Final Wizard Submission:", values);
78 await new Promise((r) => setTimeout(r, 1200));
79 setSubmitting(false);
80 alert("Account provisioned successfully!");
81 }
82 }}
83 >
84 {({ values, isSubmitting }) => (
85 <Form className="space-y-4">
86 {/* Step 1 Content */}
87 {stepIndex === 0 && (
88 <div className="space-y-4">
89 <div>
90 <label className="block text-xs text-slate-400 mb-1">First Name</label>
91 <Field name="firstName" className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm text-white" />
92 <ErrorMessage name="firstName" component="div" className="text-xs text-rose-400 mt-1" />
93 </div>
94 <div>
95 <label className="block text-xs text-slate-400 mb-1">Last Name</label>
96 <Field name="lastName" className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm text-white" />
97 <ErrorMessage name="lastName" component="div" className="text-xs text-rose-400 mt-1" />
98 </div>
99 <div>
100 <label className="block text-xs text-slate-400 mb-1">Email Address</label>
101 <Field name="email" type="email" className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm text-white" />
102 <ErrorMessage name="email" component="div" className="text-xs text-rose-400 mt-1" />
103 </div>
104 </div>
105 )}
106
107 {/* Step 2 Content */}
108 {stepIndex === 1 && (
109 <div className="space-y-4">
110 <div>
111 <label className="block text-xs text-slate-400 mb-1">Company Legal Name</label>
112 <Field name="companyName" className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm text-white" />
113 <ErrorMessage name="companyName" component="div" className="text-xs text-rose-400 mt-1" />
114 </div>
115
116 <div className="flex items-center gap-2 pt-2">
117 <Field type="checkbox" name="hasTaxId" id="hasTaxId" className="h-4 w-4 rounded bg-slate-800 border-slate-700" />
118 <label htmlFor="hasTaxId" className="text-xs text-slate-300">Company is tax registered (VAT/EIN)</label>
119 </div>
120
121 {values.hasTaxId && (
122 <div>
123 <label className="block text-xs text-slate-400 mb-1">Tax ID / EIN</label>
124 <Field name="taxId" className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm text-white" />
125 <ErrorMessage name="taxId" component="div" className="text-xs text-rose-400 mt-1" />
126 </div>
127 )}
128 </div>
129 )}
130
131 {/* Step 3 Content */}
132 {stepIndex === 2 && (
133 <div className="space-y-4">
134 <label className="block text-xs text-slate-400">Choose Deployment Plan</label>
135 <div className="grid grid-cols-3 gap-3">
136 {["starter", "growth", "enterprise"].map((tier) => (
137 <label
138 key={tier}
139 className={`p-4 border rounded-xl cursor-pointer flex flex-col items-center capitalize text-sm font-semibold transition ${
140 values.planTier === tier
141 ? "border-cyan-500 bg-cyan-950/40 text-cyan-300"
142 : "border-slate-800 bg-slate-800/40 text-slate-400"
143 }`}
144 >
145 <Field type="radio" name="planTier" value={tier} className="sr-only" />
146 {tier}
147 </label>
148 ))}
149 </div>
150 </div>
151 )}
152
153 {/* Navigation Buttons */}
154 <div className="flex justify-between pt-6 border-t border-slate-800 mt-6">
155 {stepIndex > 0 ? (
156 <button
157 type="button"
158 onClick={() => setStepIndex((curr) => curr - 1)}
159 className="px-4 py-2 bg-slate-800 hover:bg-slate-700 text-slate-300 text-sm font-semibold rounded-lg"
160 >
161 Previous
162 </button>
163 ) : (
164 <div />
165 )}
166
167 <button
168 type="submit"
169 disabled={isSubmitting}
170 className="px-6 py-2 bg-cyan-600 hover:bg-cyan-500 text-white text-sm font-semibold rounded-lg disabled:opacity-50"
171 >
172 {isSubmitting ? "Processing..." : isLastStep ? "Complete Registration" : "Continue"}
173 </button>
174 </div>
175 </Form>
176 )}
177 </Formik>
178 </div>
179 );
180}