Skip to content

Commit 008b1d4

Browse files
authored
feat(FieldApi): allow debounce of onChange and onBlur listener (#1375)
* feat(FieldApi): allow debounce of onChange and onBlur listener * ci: apply automated fixes and generate docs * feat: add ListenerCause type * ci: apply automated fixes and generate docs * docs: add debounce info to react listeners guide
1 parent 8f69ec4 commit 008b1d4

20 files changed

+282
-127
lines changed

docs/framework/react/guides/listeners.md

+22
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,25 @@ function App() {
6767
)
6868
}
6969
```
70+
71+
### Built-in Debouncing
72+
73+
If you are making an API request inside a listener, you may want to debounce the calls as it can lead to performance issues.
74+
We enable an easy method for debouncing your listeners by adding a `onChangeDebounceMs` or `onBlurDebounceMs`.
75+
76+
```tsx
77+
<form.Field
78+
name="country"
79+
listeners={{
80+
onChangeDebounceMs: 500, // 500ms debounce
81+
onChange: ({ value }) => {
82+
console.log(`Country changed to: ${value} without a change within 500ms, resetting province`)
83+
form.setFieldValue('province', '')
84+
},
85+
}}
86+
>
87+
{(field) => (
88+
/* ... */
89+
)}
90+
</form.Field>
91+
```

docs/reference/classes/fieldapi.md

+37-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: FieldApi
77

88
# Class: FieldApi\<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer, TParentSubmitMeta\>
99

10-
Defined in: [packages/form-core/src/FieldApi.ts:842](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L842)
10+
Defined in: [packages/form-core/src/FieldApi.ts:845](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L845)
1111

1212
A class representing the API for managing a form field.
1313

@@ -65,7 +65,7 @@ the `new FieldApi` constructor.
6565
new FieldApi<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer, TParentSubmitMeta>(opts): FieldApi<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer, TParentSubmitMeta>
6666
```
6767

68-
Defined in: [packages/form-core/src/FieldApi.ts:970](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L970)
68+
Defined in: [packages/form-core/src/FieldApi.ts:976](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L976)
6969

7070
Initializes a new `FieldApi` instance.
7171

@@ -87,7 +87,7 @@ Initializes a new `FieldApi` instance.
8787
form: FormApi<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer, TParentSubmitMeta>;
8888
```
8989

90-
Defined in: [packages/form-core/src/FieldApi.ts:886](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L886)
90+
Defined in: [packages/form-core/src/FieldApi.ts:889](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L889)
9191

9292
A reference to the form API instance.
9393

@@ -99,7 +99,7 @@ A reference to the form API instance.
9999
name: DeepKeys<TParentData>;
100100
```
101101

102-
Defined in: [packages/form-core/src/FieldApi.ts:910](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L910)
102+
Defined in: [packages/form-core/src/FieldApi.ts:913](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L913)
103103

104104
The field name.
105105

@@ -111,7 +111,7 @@ The field name.
111111
options: FieldApiOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer, TParentSubmitMeta>;
112112
```
113113

114-
Defined in: [packages/form-core/src/FieldApi.ts:914](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L914)
114+
Defined in: [packages/form-core/src/FieldApi.ts:917](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L917)
115115

116116
The field options.
117117

@@ -123,7 +123,7 @@ The field options.
123123
store: Derived<FieldState<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>>;
124124
```
125125

126-
Defined in: [packages/form-core/src/FieldApi.ts:938](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L938)
126+
Defined in: [packages/form-core/src/FieldApi.ts:941](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L941)
127127

128128
The field state store.
129129

@@ -132,10 +132,22 @@ The field state store.
132132
### timeoutIds
133133

134134
```ts
135-
timeoutIds: Record<ValidationCause, null | Timeout>;
135+
timeoutIds: object;
136136
```
137137

138-
Defined in: [packages/form-core/src/FieldApi.ts:965](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L965)
138+
Defined in: [packages/form-core/src/FieldApi.ts:968](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L968)
139+
140+
#### listeners
141+
142+
```ts
143+
listeners: Record<ListenerCause, null | Timeout>;
144+
```
145+
146+
#### validations
147+
148+
```ts
149+
validations: Record<ValidationCause, null | Timeout>;
150+
```
139151

140152
## Accessors
141153

@@ -147,7 +159,7 @@ Defined in: [packages/form-core/src/FieldApi.ts:965](https://github.com/TanStack
147159
get state(): FieldState<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>
148160
```
149161

150-
Defined in: [packages/form-core/src/FieldApi.ts:962](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L962)
162+
Defined in: [packages/form-core/src/FieldApi.ts:965](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L965)
151163

152164
The current field state.
153165

@@ -163,7 +175,7 @@ The current field state.
163175
getInfo(): FieldInfo<TParentData>
164176
```
165177

166-
Defined in: [packages/form-core/src/FieldApi.ts:1218](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1218)
178+
Defined in: [packages/form-core/src/FieldApi.ts:1224](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1224)
167179

168180
Gets the field information object.
169181

@@ -179,7 +191,7 @@ Gets the field information object.
179191
getMeta(): FieldMeta<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>
180192
```
181193

182-
Defined in: [packages/form-core/src/FieldApi.ts:1186](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1186)
194+
Defined in: [packages/form-core/src/FieldApi.ts:1192](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1192)
183195

184196
#### Returns
185197

@@ -193,7 +205,7 @@ Defined in: [packages/form-core/src/FieldApi.ts:1186](https://github.com/TanStac
193205
getValue(): TData
194206
```
195207

196-
Defined in: [packages/form-core/src/FieldApi.ts:1168](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1168)
208+
Defined in: [packages/form-core/src/FieldApi.ts:1177](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1177)
197209

198210
Gets the current field value.
199211

@@ -213,7 +225,7 @@ Use `field.state.value` instead.
213225
handleBlur(): void
214226
```
215227

216-
Defined in: [packages/form-core/src/FieldApi.ts:1625](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1625)
228+
Defined in: [packages/form-core/src/FieldApi.ts:1616](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1616)
217229

218230
Handles the blur event.
219231

@@ -229,7 +241,7 @@ Handles the blur event.
229241
handleChange(updater): void
230242
```
231243

232-
Defined in: [packages/form-core/src/FieldApi.ts:1618](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1618)
244+
Defined in: [packages/form-core/src/FieldApi.ts:1609](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1609)
233245

234246
Handles the change event.
235247

@@ -254,7 +266,7 @@ insertValue(
254266
opts?): void
255267
```
256268
257-
Defined in: [packages/form-core/src/FieldApi.ts:1238](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1238)
269+
Defined in: [packages/form-core/src/FieldApi.ts:1241](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1241)
258270
259271
Inserts a value at the specified index, shifting the subsequent values to the right.
260272
@@ -284,7 +296,7 @@ Inserts a value at the specified index, shifting the subsequent values to the ri
284296
mount(): () => void
285297
```
286298
287-
Defined in: [packages/form-core/src/FieldApi.ts:1063](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1063)
299+
Defined in: [packages/form-core/src/FieldApi.ts:1072](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1072)
288300
289301
Mounts the field instance to the form.
290302
@@ -307,7 +319,7 @@ moveValue(
307319
opts?): void
308320
```
309321
310-
Defined in: [packages/form-core/src/FieldApi.ts:1294](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1294)
322+
Defined in: [packages/form-core/src/FieldApi.ts:1285](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1285)
311323
312324
Moves the value at the first specified index to the second specified index.
313325
@@ -337,7 +349,7 @@ Moves the value at the first specified index to the second specified index.
337349
pushValue(value, opts?): void
338350
```
339351
340-
Defined in: [packages/form-core/src/FieldApi.ts:1223](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1223)
352+
Defined in: [packages/form-core/src/FieldApi.ts:1229](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1229)
341353
342354
Pushes a new value to the field.
343355
@@ -363,7 +375,7 @@ Pushes a new value to the field.
363375
removeValue(index, opts?): void
364376
```
365377
366-
Defined in: [packages/form-core/src/FieldApi.ts:1270](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1270)
378+
Defined in: [packages/form-core/src/FieldApi.ts:1267](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1267)
367379
368380
Removes a value at the specified index.
369381
@@ -422,7 +434,7 @@ Replaces a value at the specified index.
422434
setErrorMap(errorMap): void
423435
```
424436
425-
Defined in: [packages/form-core/src/FieldApi.ts:1645](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1645)
437+
Defined in: [packages/form-core/src/FieldApi.ts:1633](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1633)
426438
427439
Updates the field's errorMap
428440
@@ -444,7 +456,7 @@ Updates the field's errorMap
444456
setMeta(updater): void
445457
```
446458
447-
Defined in: [packages/form-core/src/FieldApi.ts:1191](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1191)
459+
Defined in: [packages/form-core/src/FieldApi.ts:1197](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1197)
448460
449461
Sets the field metadata.
450462
@@ -466,7 +478,7 @@ Sets the field metadata.
466478
setValue(updater, options?): void
467479
```
468480
469-
Defined in: [packages/form-core/src/FieldApi.ts:1175](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1175)
481+
Defined in: [packages/form-core/src/FieldApi.ts:1184](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1184)
470482
471483
Sets the field value and run the `change` validator.
472484
@@ -495,7 +507,7 @@ swapValues(
495507
opts?): void
496508
```
497509
498-
Defined in: [packages/form-core/src/FieldApi.ts:1282](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1282)
510+
Defined in: [packages/form-core/src/FieldApi.ts:1276](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1276)
499511
500512
Swaps the values at the specified indices.
501513
@@ -525,7 +537,7 @@ Swaps the values at the specified indices.
525537
update(opts): void
526538
```
527539
528-
Defined in: [packages/form-core/src/FieldApi.ts:1111](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1111)
540+
Defined in: [packages/form-core/src/FieldApi.ts:1120](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1120)
529541
530542
Updates the field instance with new options.
531543
@@ -547,7 +559,7 @@ Updates the field instance with new options.
547559
validate(cause, opts?): unknown[] | Promise<unknown[]>
548560
```
549561
550-
Defined in: [packages/form-core/src/FieldApi.ts:1585](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1585)
562+
Defined in: [packages/form-core/src/FieldApi.ts:1576](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1576)
551563
552564
Validates the field value.
553565

docs/reference/interfaces/fieldapioptions.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: FieldApiOptions
77

88
# Interface: FieldApiOptions\<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer, TParentSubmitMeta\>
99

10-
Defined in: [packages/form-core/src/FieldApi.ts:446](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L446)
10+
Defined in: [packages/form-core/src/FieldApi.ts:449](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L449)
1111

1212
An object type representing the required options for the FieldApi class.
1313

@@ -63,7 +63,7 @@ An object type representing the required options for the FieldApi class.
6363
optional asyncAlways: boolean;
6464
```
6565

66-
Defined in: [packages/form-core/src/FieldApi.ts:393](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L393)
66+
Defined in: [packages/form-core/src/FieldApi.ts:396](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L396)
6767

6868
If `true`, always run async validation, even if there are errors emitted during synchronous validation.
6969

@@ -79,7 +79,7 @@ If `true`, always run async validation, even if there are errors emitted during
7979
optional asyncDebounceMs: number;
8080
```
8181

82-
Defined in: [packages/form-core/src/FieldApi.ts:389](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L389)
82+
Defined in: [packages/form-core/src/FieldApi.ts:392](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L392)
8383

8484
The default time to debounce async validation if there is not a more specific debounce time passed.
8585

@@ -95,7 +95,7 @@ The default time to debounce async validation if there is not a more specific de
9595
optional defaultMeta: Partial<FieldMeta<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, any, any, any, any, any, any, any>>;
9696
```
9797

98-
Defined in: [packages/form-core/src/FieldApi.ts:412](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L412)
98+
Defined in: [packages/form-core/src/FieldApi.ts:415](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L415)
9999

100100
An optional object with default metadata for the field.
101101

@@ -111,7 +111,7 @@ An optional object with default metadata for the field.
111111
optional defaultValue: NoInfer<TData>;
112112
```
113113

114-
Defined in: [packages/form-core/src/FieldApi.ts:385](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L385)
114+
Defined in: [packages/form-core/src/FieldApi.ts:388](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L388)
115115

116116
An optional default value for the field.
117117

@@ -127,7 +127,7 @@ An optional default value for the field.
127127
optional disableErrorFlat: boolean;
128128
```
129129

130-
Defined in: [packages/form-core/src/FieldApi.ts:440](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L440)
130+
Defined in: [packages/form-core/src/FieldApi.ts:443](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L443)
131131

132132
Disable the `flat(1)` operation on `field.errors`. This is useful if you want to keep the error structure as is. Not suggested for most use-cases.
133133

@@ -143,7 +143,7 @@ Disable the `flat(1)` operation on `field.errors`. This is useful if you want to
143143
form: FormApi<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer, TParentSubmitMeta>;
144144
```
145145

146-
Defined in: [packages/form-core/src/FieldApi.ts:498](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L498)
146+
Defined in: [packages/form-core/src/FieldApi.ts:501](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L501)
147147

148148
***
149149

@@ -153,7 +153,7 @@ Defined in: [packages/form-core/src/FieldApi.ts:498](https://github.com/TanStack
153153
optional listeners: FieldListeners<TParentData, TName, TData>;
154154
```
155155

156-
Defined in: [packages/form-core/src/FieldApi.ts:436](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L436)
156+
Defined in: [packages/form-core/src/FieldApi.ts:439](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L439)
157157

158158
A list of listeners which attach to the corresponding events
159159

@@ -169,7 +169,7 @@ A list of listeners which attach to the corresponding events
169169
name: TName;
170170
```
171171

172-
Defined in: [packages/form-core/src/FieldApi.ts:381](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L381)
172+
Defined in: [packages/form-core/src/FieldApi.ts:384](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L384)
173173

174174
The field name. The type will be `DeepKeys<TParentData>` to ensure your name is a deep key of the parent dataset.
175175

@@ -185,7 +185,7 @@ The field name. The type will be `DeepKeys<TParentData>` to ensure your name is
185185
optional validators: FieldValidators<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync>;
186186
```
187187

188-
Defined in: [packages/form-core/src/FieldApi.ts:397](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L397)
188+
Defined in: [packages/form-core/src/FieldApi.ts:400](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L400)
189189

190190
A list of validators to pass to the field
191191

0 commit comments

Comments
 (0)