Skip to content

Commit 576cd83

Browse files
vara855Balastrongautofix-ci[bot]
authored
feat: add values parameter to form.reset to reset the form with new values (#791)
* feat: added ability to specify values for `form.reset` method * test: add test for new default values * feat: reset defaultValues and control overriding with a flag * ci: apply automated fixes and generate docs * test: add default meta to failing test --------- Co-authored-by: Leonardo Montini <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent a7adecb commit 576cd83

File tree

3 files changed

+165
-22
lines changed

3 files changed

+165
-22
lines changed

docs/reference/classes/formapi.md

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ deleteField<TField>(field): void
121121
122122
#### Defined in
123123
124-
[packages/form-core/src/FormApi.ts:1102](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1102)
124+
[packages/form-core/src/FormApi.ts:1117](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1117)
125125
126126
***
127127
@@ -147,7 +147,7 @@ Gets the field info of the specified field.
147147
148148
#### Defined in
149149
150-
[packages/form-core/src/FormApi.ts:1013](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1013)
150+
[packages/form-core/src/FormApi.ts:1028](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1028)
151151
152152
***
153153
@@ -173,7 +173,7 @@ Gets the metadata of the specified field.
173173
174174
#### Defined in
175175
176-
[packages/form-core/src/FormApi.ts:1004](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1004)
176+
[packages/form-core/src/FormApi.ts:1019](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1019)
177177
178178
***
179179
@@ -199,7 +199,7 @@ Gets the value of the specified field.
199199
200200
#### Defined in
201201
202-
[packages/form-core/src/FormApi.ts:997](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L997)
202+
[packages/form-core/src/FormApi.ts:1012](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1012)
203203
204204
***
205205
@@ -217,7 +217,7 @@ Handles the form submission, performs validation, and calls the appropriate onSu
217217
218218
#### Defined in
219219
220-
[packages/form-core/src/FormApi.ts:937](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L937)
220+
[packages/form-core/src/FormApi.ts:952](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L952)
221221
222222
***
223223
@@ -253,7 +253,7 @@ Inserts a value into an array field at the specified index, shifting the subsequ
253253
254254
#### Defined in
255255
256-
[packages/form-core/src/FormApi.ts:1134](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1134)
256+
[packages/form-core/src/FormApi.ts:1149](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1149)
257257
258258
***
259259
@@ -305,7 +305,7 @@ Moves the value at the first specified index to the second specified index withi
305305
306306
#### Defined in
307307
308-
[packages/form-core/src/FormApi.ts:1252](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1252)
308+
[packages/form-core/src/FormApi.ts:1267](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1267)
309309
310310
***
311311
@@ -338,7 +338,7 @@ Pushes a value into an array field.
338338
339339
#### Defined in
340340
341-
[packages/form-core/src/FormApi.ts:1116](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1116)
341+
[packages/form-core/src/FormApi.ts:1131](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1131)
342342
343343
***
344344
@@ -371,7 +371,7 @@ Removes a value from an array field at the specified index.
371371
372372
#### Defined in
373373
374-
[packages/form-core/src/FormApi.ts:1187](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1187)
374+
[packages/form-core/src/FormApi.ts:1202](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1202)
375375
376376
***
377377
@@ -407,25 +407,38 @@ Replaces a value into an array field at the specified index.
407407
408408
#### Defined in
409409
410-
[packages/form-core/src/FormApi.ts:1161](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1161)
410+
[packages/form-core/src/FormApi.ts:1176](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1176)
411411
412412
***
413413
414414
### reset()
415415
416416
```ts
417-
reset(): void
417+
reset(values?, opts?): void
418418
```
419419
420420
Resets the form state to the default values.
421+
If values are provided, the form will be reset to those values instead and the default values will be updated.
422+
423+
#### Parameters
424+
425+
• **values?**: `TFormData`
426+
427+
Optional values to reset the form to.
428+
429+
• **opts?**
430+
431+
Optional options to control the reset behavior.
432+
433+
• **opts.keepDefaultValues?**: `boolean`
421434
422435
#### Returns
423436
424437
`void`
425438
426439
#### Defined in
427440
428-
[packages/form-core/src/FormApi.ts:576](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L576)
441+
[packages/form-core/src/FormApi.ts:580](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L580)
429442
430443
***
431444
@@ -449,7 +462,7 @@ resetFieldMeta<TField>(fieldMeta): Record<TField, FieldMeta>
449462
450463
#### Defined in
451464
452-
[packages/form-core/src/FormApi.ts:1047](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1047)
465+
[packages/form-core/src/FormApi.ts:1062](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1062)
453466
454467
***
455468
@@ -471,7 +484,7 @@ Updates the form's errorMap
471484
472485
#### Defined in
473486
474-
[packages/form-core/src/FormApi.ts:1276](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1276)
487+
[packages/form-core/src/FormApi.ts:1291](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1291)
475488
476489
***
477490
@@ -499,7 +512,7 @@ Updates the metadata of the specified field.
499512
500513
#### Defined in
501514
502-
[packages/form-core/src/FormApi.ts:1032](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1032)
515+
[packages/form-core/src/FormApi.ts:1047](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1047)
503516
504517
***
505518
@@ -532,7 +545,7 @@ Sets the value of the specified field and optionally updates the touched state.
532545
533546
#### Defined in
534547
535-
[packages/form-core/src/FormApi.ts:1071](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1071)
548+
[packages/form-core/src/FormApi.ts:1086](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1086)
536549
537550
***
538551
@@ -568,7 +581,7 @@ Swaps the values at the specified indices within an array field.
568581
569582
#### Defined in
570583
571-
[packages/form-core/src/FormApi.ts:1226](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1226)
584+
[packages/form-core/src/FormApi.ts:1241](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1241)
572585
573586
***
574587
@@ -612,7 +625,7 @@ Validates all fields in the form using the correct handlers for a given validati
612625
613626
#### Defined in
614627
615-
[packages/form-core/src/FormApi.ts:591](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L591)
628+
[packages/form-core/src/FormApi.ts:606](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L606)
616629
617630
***
618631
@@ -645,7 +658,7 @@ Validates the children of a specified array in the form starting from a given in
645658
646659
#### Defined in
647660
648-
[packages/form-core/src/FormApi.ts:625](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L625)
661+
[packages/form-core/src/FormApi.ts:640](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L640)
649662
650663
***
651664
@@ -673,4 +686,4 @@ Validates a specified field in the form using the correct handlers for a given v
673686
674687
#### Defined in
675688
676-
[packages/form-core/src/FormApi.ts:664](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L664)
689+
[packages/form-core/src/FormApi.ts:679](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L679)

packages/form-core/src/FormApi.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,14 +572,29 @@ export class FormApi<
572572

573573
/**
574574
* Resets the form state to the default values.
575+
* If values are provided, the form will be reset to those values instead and the default values will be updated.
576+
*
577+
* @param values - Optional values to reset the form to.
578+
* @param opts - Optional options to control the reset behavior.
575579
*/
576-
reset = () => {
580+
reset = (values?: TFormData, opts?: { keepDefaultValues?: boolean }) => {
577581
const { fieldMeta: currentFieldMeta } = this.state
578582
const fieldMeta = this.resetFieldMeta(currentFieldMeta)
583+
584+
if (values && !opts?.keepDefaultValues) {
585+
this.options = {
586+
...this.options,
587+
defaultValues: values,
588+
}
589+
}
590+
579591
this.store.setState(() =>
580592
getDefaultFormState({
581593
...(this.options.defaultState as any),
582-
values: this.options.defaultValues ?? this.options.defaultState?.values,
594+
values:
595+
values ??
596+
this.options.defaultValues ??
597+
this.options.defaultState?.values,
583598
fieldMeta,
584599
}),
585600
)

packages/form-core/tests/FormApi.spec.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,121 @@ describe('form api', () => {
197197
})
198198
})
199199

200+
it('should reset with provided custom default values', () => {
201+
const defaultValues = {
202+
name: 'test',
203+
surname: 'test2',
204+
age: 30,
205+
}
206+
const form = new FormApi({
207+
defaultValues: defaultValues,
208+
})
209+
210+
form.mount()
211+
form.setFieldValue('name', 'other')
212+
213+
expect(form.state.values).toEqual({
214+
name: 'other',
215+
surname: 'test2',
216+
age: 30,
217+
})
218+
219+
form.reset()
220+
221+
expect(form.state.values).toEqual(defaultValues)
222+
223+
form.setFieldValue('name', 'other2')
224+
form.setFieldValue('age', 33)
225+
226+
const resetValues = {
227+
name: 'reset name',
228+
age: 40,
229+
surname: 'reset surname',
230+
}
231+
form.reset(resetValues)
232+
233+
expect(form.state).toEqual({
234+
values: {
235+
name: 'reset name',
236+
age: 40,
237+
surname: 'reset surname',
238+
},
239+
errors: [],
240+
errorMap: {},
241+
fieldMeta: {
242+
name: {
243+
isValidating: false,
244+
isTouched: false,
245+
isBlurred: false,
246+
isDirty: false,
247+
isPristine: true,
248+
errors: [],
249+
errorMap: {},
250+
},
251+
age: {
252+
isValidating: false,
253+
isTouched: false,
254+
isBlurred: false,
255+
isDirty: false,
256+
isPristine: true,
257+
errors: [],
258+
errorMap: {},
259+
},
260+
},
261+
canSubmit: true,
262+
isFieldsValid: true,
263+
isFieldsValidating: false,
264+
isFormValid: true,
265+
isFormValidating: false,
266+
isSubmitted: false,
267+
isSubmitting: false,
268+
isTouched: false,
269+
isPristine: true,
270+
isDirty: false,
271+
isValid: true,
272+
isBlurred: false,
273+
isValidating: false,
274+
submissionAttempts: 0,
275+
validationMetaMap: {
276+
onChange: undefined,
277+
onBlur: undefined,
278+
onSubmit: undefined,
279+
onMount: undefined,
280+
onServer: undefined,
281+
},
282+
})
283+
})
284+
285+
it('should reset and set the new default values that are restored after an empty reset', () => {
286+
const form = new FormApi({ defaultValues: { name: 'initial' } })
287+
form.mount()
288+
289+
const field = new FieldApi({ form, name: 'name' })
290+
field.mount()
291+
292+
form.reset({ name: 'other' })
293+
expect(form.state.values).toEqual({ name: 'other' })
294+
295+
field.handleChange('')
296+
form.reset()
297+
expect(form.state.values).toEqual({ name: 'other' })
298+
})
299+
300+
it('should reset without setting the new default values that are restored after an empty reset if opts.keepDefaultValues is true', () => {
301+
const form = new FormApi({ defaultValues: { name: 'initial' } })
302+
form.mount()
303+
304+
const field = new FieldApi({ form, name: 'name' })
305+
field.mount()
306+
307+
form.reset({ name: 'other' }, { keepDefaultValues: true })
308+
expect(form.state.values).toEqual({ name: 'other' })
309+
310+
field.handleChange('')
311+
form.reset()
312+
expect(form.state.values).toEqual({ name: 'initial' })
313+
})
314+
200315
it('should not wipe validators when resetting', () => {
201316
const form = new FormApi({
202317
defaultValues: {

0 commit comments

Comments
 (0)