Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

step1.component.ts

69 lines1.9 KB

1import { Component, input, output } from '@angular/core'
2import {
3 TanStackAppField,
4 TanStackField,
5 TanStackFormGroup,
6 injectWithForm,
7} from '@tanstack/angular-form'
8import { TextFieldComponent } from './text-field.component'
9import { step1Schema, wizardFormOpts } from './shared-form'
10​
11@Component({
12 selector: 'app-step1',
13 standalone: true,
14 imports: [
15 TanStackField,
16 TanStackAppField,
17 TanStackFormGroup,
18 TextFieldComponent,
19 ],
20 template: `
21 <ng-container
22 [tanstackFormGroup]="withForm.form"
23 name="step1"
24 [validators]="{ onDynamic: step1Schema }"
25 [onGroupSubmit]="onGroupSubmit"
26 [onGroupSubmitInvalid]="onGroupSubmitInvalid"
27 #group="formGroup"
28 >
29 <form
30 (submit)="
31 $event.preventDefault();
32 $event.stopPropagation();
33 group.api.handleSubmit()
34 "
35 >
36 <app-text-field
37 label="Step 1 Name"
38 tanstack-app-field
39 [tanstackField]="withForm.form"
40 name="step1.name"
41 />
42​
43 <button type="submit" [disabled]="isSubmitting()">Submit</button>
44​
45 <!-- formGroup contains errorMaps and errors, just like forms and fields -->
46 <pre>{{ stringify(group.api.state.meta.errorMap) }}</pre>
47 </form>
48 </ng-container>
49 `,
50})
51export class Step1Component {
52 withForm = injectWithForm({ ...wizardFormOpts })
53 step = input.required<number>()
54 isSubmitting = input.required<boolean>()
55 stepChange = output<number>()
56​
57 step1Schema = step1Schema
58 stringify = (value: unknown) => JSON.stringify(value, null, 2)
59​
60 onGroupSubmit = () => {
61 this.stepChange.emit(this.step() + 1)
62 }
63​
64 onGroupSubmitInvalid = () => {
65 // Just like a form, you can also handle invalid submits at the group level,
66 // which is useful for multi-step wizards to prevent going to the next step
67 // if the current step is invalid
68 }