Open History to see the last commit
1import React from 'react'
2import { useForm } from '../src'
3import type { AnyFieldApi, AnyFormApi, ValidationIssue } from '../src'
4β
5interface FormValidationIssue extends ValidationIssue {
6 source: 'form'
7}
8β
9interface FormFieldValidationIssue extends ValidationIssue {
10 source: 'form-field'
11}
12β
13interface GroupValidationIssue extends ValidationIssue {
14 source: 'group'
15}
16β
17interface GroupFieldValidationIssue extends ValidationIssue {
18 source: 'group-field'
19}
20β
21interface FieldValidationIssue extends ValidationIssue {
22 source: 'field'
23}
24β
25const defaultValues = {
26 section: {
27 name: '',
28 },
29}
30β
31const formValidator = {
32 triggers: [],
33 run: () => ({
34 form: {
35 message: 'Form error',
36 source: 'form' as const,
37 } satisfies FormValidationIssue,
38 fields: {
39 'section.name': {
40 message: 'Form field error',
41 source: 'form-field' as const,
42 } satisfies FormFieldValidationIssue,
43 },
44 }),
45}
46β
47const formOnlyValidator = {
48 triggers: [],
49 run: () => ({
50 form: {
51 message: 'Form error',
52 source: 'form' as const,
53 } satisfies FormValidationIssue,
54 fields: {},
55 }),
56}
57β
58const formFieldOnlyValidator = {
59 triggers: [],
60 run: () => ({
61 fields: {
62 'section.name': {
63 message: 'Form field error',
64 source: 'form-field' as const,
65 } satisfies FormFieldValidationIssue,
66 },
67 }),
68}
69β
70const groupValidator = {
71 triggers: [],
72 run: () => ({
73 form: {
74 message: 'Group error',
75 source: 'group' as const,
76 } satisfies GroupValidationIssue,
77 fields: {
78 name: {
79 message: 'Group field error',
80 source: 'group-field' as const,
81 } satisfies GroupFieldValidationIssue,
82 },
83 }),
84}
85β
86const fieldValidator = {
87 triggers: [],
88 run: () =>
89 ({
90 message: 'Field error',
91 source: 'field' as const,
92 }) satisfies FieldValidationIssue,
93}
94β
95declare function acceptAnyFormApi(form: AnyFormApi): void
96declare function acceptAnyFieldApi(field: AnyFieldApi): void
97β
98function AnyApiAssignabilityMatrix() {
99 const formWithFormAndFieldErrors = useForm({
100 defaultValues,
101 validators: [formValidator],
102 onSubmit: () => undefined,
103 })
104 const formWithOnlyFormErrors = useForm({
105 defaultValues,
106 validators: [formOnlyValidator],
107 onSubmit: () => undefined,
108 })
109 const formWithOnlyFieldErrors = useForm({
110 defaultValues,
111 validators: [formFieldOnlyValidator],
112 onSubmit: () => undefined,
113 })
114 const formWithoutErrors = useForm({
115 defaultValues,
116 validators: [],
117 onSubmit: () => undefined,
118 })
119β
120 acceptAnyFormApi(formWithFormAndFieldErrors)
121 acceptAnyFormApi(formWithOnlyFormErrors)
122 acceptAnyFormApi(formWithOnlyFieldErrors)
123 acceptAnyFormApi(formWithoutErrors)
124β
125 return (
126 <>
127 <formWithFormAndFieldErrors.FormGroup
128 name="section"
129 validators={[groupValidator]}
130 >
131 {(group) => (
132 <>
133 {/* Form, group, and field validators produce errors. */}
134 <group.Field name="name" validators={[fieldValidator]}>
135 {(field) => {
136 acceptAnyFieldApi(field)
137 return null
138 }}
139 </group.Field>
140 {/* The field validator omits its error. */}
141 <group.Field name="name">
142 {(field) => {
143 acceptAnyFieldApi(field)
144 return null
145 }}
146 </group.Field>
147 </>
148 )}
149 </formWithFormAndFieldErrors.FormGroup>
150β
151 <formWithFormAndFieldErrors.FormGroup name="section">
152 {(group) => (
153 <>
154 {/* The group validator omits its error. */}
155 <group.Field name="name" validators={[fieldValidator]}>
156 {(field) => {
157 acceptAnyFieldApi(field)
158 return null
159 }}
160 </group.Field>
161 {/* The group and field validators omit their errors. */}
162 <group.Field name="name">
163 {(field) => {
164 acceptAnyFieldApi(field)
165 return null
166 }}
167 </group.Field>
168 </>
169 )}
170 </formWithFormAndFieldErrors.FormGroup>
171β
172 <formWithoutErrors.FormGroup name="section" validators={[groupValidator]}>
173 {(group) => (
174 <>
175 {/* The form validator omits its error. */}
176 <group.Field name="name" validators={[fieldValidator]}>
177 {(field) => {
178 acceptAnyFieldApi(field)
179 return null
180 }}
181 </group.Field>
182 {/* The form and field validators omit their errors. */}
183 <group.Field name="name">
184 {(field) => {
185 acceptAnyFieldApi(field)
186 return null
187 }}
188 </group.Field>
189 </>
190 )}
191 </formWithoutErrors.FormGroup>
192β
193 <formWithoutErrors.FormGroup name="section">
194 {(group) => (
195 <>
196 {/* The form and group validators omit their errors. */}
197 <group.Field name="name" validators={[fieldValidator]}>
198 {(field) => {
199 acceptAnyFieldApi(field)
200 return null
201 }}
202 </group.Field>
203 {/* All validator levels omit their errors. */}
204 <group.Field name="name">
205 {(field) => {
206 acceptAnyFieldApi(field)
207 return null
208 }}
209 </group.Field>
210 </>
211 )}
212 </formWithoutErrors.FormGroup>
213 </>
214 )
215}
216β
217void AnyApiAssignabilityMatrix