Skip to content

Commit b2b9b0a

Browse files
docs: form-api-docs updated and removed some old docs (#433)
* docs: form-api-docs updated and removed some old docs * chore: updated docs removed mentioning the types and tried to explain what they do instead --------- Co-authored-by: Corbin Crutchley <git@crutchcorn.dev>
1 parent a721443 commit b2b9b0a

File tree

1 file changed

+59
-39
lines changed

1 file changed

+59
-39
lines changed

docs/reference/formApi.md

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Form API
55

66
### Creating a new FormApi Instance
77

8-
> Some of these docs may be inaccurate due to an API shift in `0.11.0`. If you're interested in helping us fix these issues, please [join our Discord](https://tlinz.com/discord) and reach out in the `#form` channel.
8+
99

1010
Normally, you will not need to create a new `FormApi` instance directly. Instead, you will use a framework hook/function like `useForm` or `createForm` to create a new instance for you that utilizes your frameworks reactivity model. However, if you need to create a new instance manually, you can do so by calling the `new FormApi` constructor.
1111

@@ -20,60 +20,91 @@ An object representing the options for a form.
2020
- ```tsx
2121
defaultValues?: TData
2222
```
23-
- The default values for the form fields.
23+
- Set initial values for you form.
2424
- ```tsx
2525
defaultState?: Partial<FormState<TData>>
2626
```
2727
- The default state for the form.
28+
29+
- ```tsx
30+
asyncDebounceMs?: number
31+
```
32+
- Optional time in milliseconds if you want to introduce a delay before firing off an async action.
33+
34+
- ```tsx
35+
onMount?: (values: TData, formApi: FormApi<TData>) => ValidationError
36+
```
37+
- Optional function that fires as soon as the component mounts.
38+
- ```tsx
39+
onMountAsync?: ( values: TData, formApi: FormApi<TData>) => ValidationError | Promise<ValidationError>
40+
```
41+
- Optional async function that fires when the component mounts
2842
- ```tsx
29-
onSubmit?: (values: TData, formApi: FormApi<TData>) => Promise<any>
43+
onMountAsyncDebounceMs?: number
3044
```
31-
- A function to be called when the form is submitted and valid.
45+
- The default time in milliseconds that if set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds.
46+
3247
- ```tsx
33-
onInvalidSubmit?: (values: TData, formApi: FormApi<TData>) => void
48+
onChange?: (values: TData, formApi: FormApi<TData>) => ValidationError
3449
```
35-
- A function to be called when the form is submitted but invalid.
50+
- Optional function that checks the validity of your data whenever a value changes
51+
3652
- ```tsx
37-
validate?: (values: TData, formApi: FormApi<TData>) => Promise<any>
53+
onChangeAsync?: (values: TData, formApi: FormApi<TData>) => ValidationError | Promise<ValidationError>
3854
```
39-
- A function for custom validation logic for the form.
55+
- Optional onChange asynchronous counterpart to onChange. Useful for more complex validation logic that might involve server requests.
56+
4057
- ```tsx
41-
defaultValidatePristine?: boolean
58+
onChangeAsyncDebounceMs?: number
4259
```
43-
- A boolean flag to enable or disable validation for pristine fields.
60+
- The default time in milliseconds that if set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds.
61+
4462
- ```tsx
45-
defaultValidateOn?: ValidationCause
63+
onBlur?: (values: TData, formApi: FormApi<TData>) => ValidationError
4664
```
47-
- The default minimum cause for a field to be synchronously validated
65+
- Optional function that validates the form data when a field loses focus, returns a `ValidationError`
66+
4867
- ```tsx
49-
defaultValidateAsyncOn?: ValidationCause
68+
onBlurAsync?: (values: TData,formApi: FormApi<TData>) => ValidationError | Promise<ValidationError>
5069
```
51-
- The default minimum cause for a field to be asynchronously validated
70+
- Optional onBlur asynchronous validation method for when a field loses focus return a `ValidationError` or a promise of `Promise<ValidationError>`
71+
5272
- ```tsx
53-
defaultValidateAsyncDebounceMs?: number
73+
onBlurAsyncDebounceMs?: number
5474
```
5575
- The default time in milliseconds that if set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds.
5676

77+
- ```tsx
78+
onSubmit?: (values: TData, formApi: FormApi<TData>) => any | Promise<any>
79+
```
80+
- A function to be called when the form is submitted, what should happen once the user submits a valid form returns `any` or a promise `Promise<any>`
81+
82+
- ```tsx
83+
onSubmitInvalid?: (values: TData, formApi: FormApi<TData>) => void
84+
```
85+
- Specify an action for scenarios where the user tries to submit an invalid form.
86+
87+
5788
### `FormApi<TFormData>`
5889

5990
A class representing the Form API. It handles the logic and interactions with the form state.
6091

6192
#### Properties
6293

6394
- ```tsx
64-
options: FormOptions<TFormData>
95+
options: FormOptions<TFormData> = {}
6596
```
6697
- The options for the form.
6798
- ```tsx
6899
store: Store<FormState<TFormData>>
69100
```
70-
- The internal store for the form state.
101+
- An internal mechanism that keeps track of the form's state.
71102
- ```tsx
72103
state: FormState<TFormData>
73104
```
74105
- The current state of the form.
75106
- ```tsx
76-
fieldInfo: Record<DeepKeys<TFormData>, FieldInfo<TFormData>>
107+
fieldInfo: Record<DeepKeys<TFormData>, FieldInfo<TFormData>> ={} as any
77108
```
78109
- A record of field information for each field in the form.
79110
- ```tsx
@@ -103,12 +134,9 @@ A class representing the Form API. It handles the logic and interactions with th
103134
validateAllFields()
104135
```
105136
- Validates all fields in the form.
137+
106138
- ```tsx
107-
validateForm()
108-
```
109-
- Validates the form itself.
110-
- ```tsx
111-
handleSubmit(e: FormEvent & { __handled?: boolean })
139+
handleSubmit(e: FormSubmitEvent)
112140
```
113141
- Handles the form submission event, performs validation, and calls the appropriate onSubmit or onInvalidSubmit callbacks.
114142
- ```tsx
@@ -221,22 +249,9 @@ An object representing the field information for a specific field within the for
221249
instances: Record<string, FieldApi<any, TFormData>>
222250
```
223251
- A record of field instances with unique identifiers as keys.
224-
- ```tsx
225-
validationCount?: number
226-
```
227-
- A counter for tracking the number of validations performed on the field.
228-
- ```tsx
229-
validationPromise?: Promise<ValidationError>
230-
```
231-
- A promise representing the current validation state of the field.
232-
- ```tsx
233-
validationResolve?: (error: ValidationError) => void
234-
```
235-
- A function to resolve the validation promise with a possible validation error.
236-
- ```tsx
237-
validationReject?: (error: unknown) => void
238-
```
239-
- A function to reject the validation promise with an error.
252+
- Check below for `ValidationMeta`
253+
254+
240255

241256
### `ValidationMeta`
242257

@@ -246,6 +261,11 @@ An object representing the validation metadata for a field.
246261
validationCount?: number
247262
```
248263
- A counter for tracking the number of validations performed on the field.
264+
265+
- ```tsx
266+
validationAsyncCount?: number
267+
```
268+
- A counter for tracking the number of validations performed on the field async.
249269
- ```tsx
250270
validationPromise?: Promise<ValidationError>
251271
```

0 commit comments

Comments
 (0)