|
| 1 | +import * as React from 'react' |
| 2 | +import { createRoot } from 'react-dom/client' |
| 3 | +import { TanStackDevtools } from '@tanstack/react-devtools' |
| 4 | +import { formDevtoolsPlugin } from '@tanstack/react-form-devtools' |
| 5 | +import { useForm } from '@tanstack/react-form'; |
| 6 | +import { z } from 'zod'; |
| 7 | +import type { AnyFieldApi, DeepKeys } from '@tanstack/react-form'; |
| 8 | + |
| 9 | +const userSchema = z.object({ |
| 10 | + firstName: z.string().min(2, 'Too short'), |
| 11 | + email: z.string().email('Invalid email'), |
| 12 | +}); |
| 13 | + |
| 14 | +type FormData = z.infer<typeof userSchema>; |
| 15 | + |
| 16 | +function FieldUI({ field, label }: { field: AnyFieldApi; label: string }) { |
| 17 | + return ( |
| 18 | + <div> |
| 19 | + <input |
| 20 | + key={label} |
| 21 | + value={field.state.value} |
| 22 | + onBlur={field.handleBlur} |
| 23 | + onChange={(e) => field.handleChange(e.target.value)} |
| 24 | + placeholder={label} |
| 25 | + /> |
| 26 | + {field.state.meta.errors.length > 0 && ( |
| 27 | + <em style={{ color: 'red' }}> |
| 28 | + {field.state.meta.errors |
| 29 | + .map((e) => (typeof e === 'string' ? e : e.message)) |
| 30 | + .join(', ')} |
| 31 | + </em> |
| 32 | + )} |
| 33 | + </div> |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +export default function App() { |
| 38 | + const [step, setStep] = React.useState(0); |
| 39 | + |
| 40 | + const form = useForm({ |
| 41 | + defaultValues: { |
| 42 | + firstName: '', |
| 43 | + email: '', |
| 44 | + } as FormData, |
| 45 | + validators: { onSubmit: userSchema }, |
| 46 | + onSubmit: async ({ value }) => console.log('Final submit:', value), |
| 47 | + }); |
| 48 | + |
| 49 | + const steps = [ |
| 50 | + { |
| 51 | + fields: ['firstName'] as const, |
| 52 | + component: () => ( |
| 53 | + <form.Field |
| 54 | + name="firstName" |
| 55 | + validators={{ onBlur: userSchema.shape.firstName }} |
| 56 | + children={(f) => <FieldUI field={f} label="First Name" />} |
| 57 | + /> |
| 58 | + ), |
| 59 | + }, |
| 60 | + { |
| 61 | + fields: ['email'] as const, |
| 62 | + component: () => ( |
| 63 | + <form.Field |
| 64 | + name="email" |
| 65 | + validators={{ onBlur: userSchema.shape.email }} |
| 66 | + children={(f) => <FieldUI field={f} label="Email" />} |
| 67 | + /> |
| 68 | + ), |
| 69 | + }, |
| 70 | + ]; |
| 71 | + |
| 72 | + type FormFieldName = DeepKeys<FormData>; |
| 73 | + |
| 74 | + const validateFieldGroup = async (fields: readonly FormFieldName[]) => { |
| 75 | + await Promise.all(fields.map((field) => form.validateField(field, 'blur'))); |
| 76 | + return fields.every( |
| 77 | + (field) => (form.getFieldMeta(field)?.errors.length ?? 0) === 0 |
| 78 | + ); |
| 79 | + }; |
| 80 | + |
| 81 | + const next = async () => { |
| 82 | + const result = await validateFieldGroup(steps[step].fields); |
| 83 | + if (result) { |
| 84 | + setStep((s) => s + 1); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <form |
| 90 | + onSubmit={(e) => { |
| 91 | + e.preventDefault(); |
| 92 | + form.handleSubmit(); |
| 93 | + }} |
| 94 | + > |
| 95 | + {steps[step].component()} |
| 96 | + <div style={{ marginTop: 20 }}> |
| 97 | + {step > 0 && ( |
| 98 | + <button |
| 99 | + key="back" |
| 100 | + type="button" |
| 101 | + onClick={() => setStep((s) => s - 1)} |
| 102 | + > |
| 103 | + Back |
| 104 | + </button> |
| 105 | + )} |
| 106 | + {step < steps.length - 1 ? ( |
| 107 | + <button key="next" type="button" onClick={next}> |
| 108 | + Next |
| 109 | + </button> |
| 110 | + ) : ( |
| 111 | + <button style={{ marginLeft: 20 }} type="submit"> |
| 112 | + Submit |
| 113 | + </button> |
| 114 | + )} |
| 115 | + </div> |
| 116 | + </form> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +const rootElement = document.getElementById('root')! |
| 123 | + |
| 124 | +createRoot(rootElement).render( |
| 125 | + <React.StrictMode> |
| 126 | + <App /> |
| 127 | + |
| 128 | + <TanStackDevtools |
| 129 | + config={{ hideUntilHover: true }} |
| 130 | + plugins={[formDevtoolsPlugin()]} |
| 131 | + /> |
| 132 | + </React.StrictMode>, |
| 133 | +) |
0 commit comments