Open History to see the last commit
1import { Component, signal } from '@angular/core'
2import {
3 TanStackWithForm,
4 injectForm,
5 injectStore,
6 revalidateLogic,
7} from '@tanstack/angular-form'
8import { z } from 'zod'
9import { Step1Component } from './step1.component'
10import { Step2Component } from './step2.component'
11import { step1Schema, step2Schema, wizardFormOpts } from './shared-form'
12β
13@Component({
14 selector: 'app-root',
15 standalone: true,
16 imports: [TanStackWithForm, Step1Component, Step2Component],
17 template: `
18 @if (step() === 0) {
19 <app-step1
20 tanstack-with-form
21 [form]="form"
22 [step]="step()"
23 [isSubmitting]="isSubmitting()"
24 (stepChange)="step.set($event)"
25 />
26 }
27 @if (step() === 1) {
28 <app-step2
29 tanstack-with-form
30 [form]="form"
31 [step]="step()"
32 [isSubmitting]="isSubmitting()"
33 (stepChange)="step.set($event)"
34 />
35 }
36 `,
37})
38export class AppComponent {
39 step = signal(0)
40β
41 form = injectForm({
42 ...wizardFormOpts,
43 validationLogic: revalidateLogic(),
44 // onDynamic is only used when `form.handleSubmit` is called itself.
45 // When the FormGroup's `handleSubmit` is called, it will only validate the
46 // current step's schema. This means that this schema will not be called
47 // when the user submits the form group, but instead when they submit the
48 // entire form.
49 validators: {
50 onDynamic: z.object({
51 step1: step1Schema,
52 step2: step2Schema,
53 }),
54 },
55 onSubmit: ({ value }) => {
56 alert(`Form submitted: ${JSON.stringify(value)}`)
57 },
58 })
59β
60 isSubmitting = injectStore(this.form, (state) => state.isSubmitting)
61}