Skip to content

Commit 72a09c3

Browse files
fix: deletion of subitems on a field now works (#1199)
* fix: deletion of subitems on a field now works * ci: apply automated fixes and generate docs --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 230c2a0 commit 72a09c3

File tree

3 files changed

+84
-32
lines changed

3 files changed

+84
-32
lines changed

docs/reference/classes/formapi.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Defined in: [packages/form-core/src/FormApi.ts:718](https://github.com/TanStack/
137137
deleteField<TField>(field): void
138138
```
139139
140-
Defined in: [packages/form-core/src/FormApi.ts:1770](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1770)
140+
Defined in: [packages/form-core/src/FormApi.ts:1772](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1772)
141141
142142
#### Type Parameters
143143
@@ -161,7 +161,7 @@ Defined in: [packages/form-core/src/FormApi.ts:1770](https://github.com/TanStack
161161
getAllErrors(): object
162162
```
163163
164-
Defined in: [packages/form-core/src/FormApi.ts:1981](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1981)
164+
Defined in: [packages/form-core/src/FormApi.ts:1988](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1988)
165165
166166
Returns form and field level errors
167167
@@ -212,7 +212,7 @@ errors: (
212212
getFieldInfo<TField>(field): FieldInfo<TFormData>
213213
```
214214
215-
Defined in: [packages/form-core/src/FormApi.ts:1679](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1679)
215+
Defined in: [packages/form-core/src/FormApi.ts:1681](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1681)
216216
217217
Gets the field info of the specified field.
218218
@@ -238,7 +238,7 @@ Gets the field info of the specified field.
238238
getFieldMeta<TField>(field): undefined | AnyFieldMeta
239239
```
240240
241-
Defined in: [packages/form-core/src/FormApi.ts:1670](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1670)
241+
Defined in: [packages/form-core/src/FormApi.ts:1672](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1672)
242242
243243
Gets the metadata of the specified field.
244244
@@ -264,7 +264,7 @@ Gets the metadata of the specified field.
264264
getFieldValue<TField>(field): DeepValue<TFormData, TField>
265265
```
266266
267-
Defined in: [packages/form-core/src/FormApi.ts:1663](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1663)
267+
Defined in: [packages/form-core/src/FormApi.ts:1665](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1665)
268268
269269
Gets the value of the specified field.
270270
@@ -292,7 +292,7 @@ Gets the value of the specified field.
292292
handleSubmit(): Promise<void>
293293
```
294294
295-
Defined in: [packages/form-core/src/FormApi.ts:1579](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1579)
295+
Defined in: [packages/form-core/src/FormApi.ts:1581](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1581)
296296
297297
Handles the form submission, performs validation, and calls the appropriate onSubmit or onInvalidSubmit callbacks.
298298
@@ -306,7 +306,7 @@ Handles the form submission, performs validation, and calls the appropriate onSu
306306
handleSubmit(submitMeta): Promise<void>
307307
```
308308
309-
Defined in: [packages/form-core/src/FormApi.ts:1580](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1580)
309+
Defined in: [packages/form-core/src/FormApi.ts:1582](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1582)
310310
311311
Handles the form submission, performs validation, and calls the appropriate onSubmit or onInvalidSubmit callbacks.
312312
@@ -332,7 +332,7 @@ insertFieldValue<TField>(
332332
opts?): Promise<void>
333333
```
334334
335-
Defined in: [packages/form-core/src/FormApi.ts:1799](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1799)
335+
Defined in: [packages/form-core/src/FormApi.ts:1811](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1811)
336336
337337
#### Type Parameters
338338
@@ -368,7 +368,7 @@ Defined in: [packages/form-core/src/FormApi.ts:1799](https://github.com/TanStack
368368
mount(): () => void
369369
```
370370
371-
Defined in: [packages/form-core/src/FormApi.ts:1047](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1047)
371+
Defined in: [packages/form-core/src/FormApi.ts:1049](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1049)
372372
373373
#### Returns
374374
@@ -390,7 +390,7 @@ moveFieldValues<TField>(
390390
opts?): void
391391
```
392392
393-
Defined in: [packages/form-core/src/FormApi.ts:1928](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1928)
393+
Defined in: [packages/form-core/src/FormApi.ts:1935](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1935)
394394
395395
Moves the value at the first specified index to the second specified index within an array field.
396396
@@ -431,7 +431,7 @@ pushFieldValue<TField>(
431431
opts?): void
432432
```
433433
434-
Defined in: [packages/form-core/src/FormApi.ts:1784](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1784)
434+
Defined in: [packages/form-core/src/FormApi.ts:1796](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1796)
435435
436436
Pushes a value into an array field.
437437
@@ -468,7 +468,7 @@ removeFieldValue<TField>(
468468
opts?): Promise<void>
469469
```
470470
471-
Defined in: [packages/form-core/src/FormApi.ts:1857](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1857)
471+
Defined in: [packages/form-core/src/FormApi.ts:1869](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1869)
472472
473473
Removes a value from an array field at the specified index.
474474
@@ -506,7 +506,7 @@ replaceFieldValue<TField>(
506506
opts?): Promise<void>
507507
```
508508
509-
Defined in: [packages/form-core/src/FormApi.ts:1831](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1831)
509+
Defined in: [packages/form-core/src/FormApi.ts:1843](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1843)
510510
511511
Replaces a value into an array field at the specified index.
512512
@@ -544,7 +544,7 @@ Replaces a value into an array field at the specified index.
544544
reset(values?, opts?): void
545545
```
546546
547-
Defined in: [packages/form-core/src/FormApi.ts:1132](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1132)
547+
Defined in: [packages/form-core/src/FormApi.ts:1134](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1134)
548548
549549
Resets the form state to the default values.
550550
If values are provided, the form will be reset to those values instead and the default values will be updated.
@@ -577,7 +577,7 @@ Optional options to control the reset behavior.
577577
resetFieldMeta<TField>(fieldMeta): Record<TField, AnyFieldMeta>
578578
```
579579
580-
Defined in: [packages/form-core/src/FormApi.ts:1716](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1716)
580+
Defined in: [packages/form-core/src/FormApi.ts:1718](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1718)
581581
582582
#### Type Parameters
583583
@@ -601,7 +601,7 @@ Defined in: [packages/form-core/src/FormApi.ts:1716](https://github.com/TanStack
601601
setErrorMap(errorMap): void
602602
```
603603
604-
Defined in: [packages/form-core/src/FormApi.ts:1955](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1955)
604+
Defined in: [packages/form-core/src/FormApi.ts:1962](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1962)
605605
606606
Updates the form's errorMap
607607
@@ -623,7 +623,7 @@ Updates the form's errorMap
623623
setFieldMeta<TField>(field, updater): void
624624
```
625625
626-
Defined in: [packages/form-core/src/FormApi.ts:1698](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1698)
626+
Defined in: [packages/form-core/src/FormApi.ts:1700](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1700)
627627
628628
Updates the metadata of the specified field.
629629
@@ -656,7 +656,7 @@ setFieldValue<TField>(
656656
opts?): void
657657
```
658658
659-
Defined in: [packages/form-core/src/FormApi.ts:1740](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1740)
659+
Defined in: [packages/form-core/src/FormApi.ts:1742](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1742)
660660
661661
Sets the value of the specified field and optionally updates the touched state.
662662
@@ -694,7 +694,7 @@ swapFieldValues<TField>(
694694
opts?): void
695695
```
696696
697-
Defined in: [packages/form-core/src/FormApi.ts:1899](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1899)
697+
Defined in: [packages/form-core/src/FormApi.ts:1906](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1906)
698698
699699
Swaps the values at the specified indices within an array field.
700700
@@ -732,7 +732,7 @@ Swaps the values at the specified indices within an array field.
732732
update(options?): void
733733
```
734734
735-
Defined in: [packages/form-core/src/FormApi.ts:1064](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1064)
735+
Defined in: [packages/form-core/src/FormApi.ts:1066](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1066)
736736
737737
Updates the form options and form state.
738738
@@ -754,7 +754,7 @@ Updates the form options and form state.
754754
validateAllFields(cause): Promise<unknown[]>
755755
```
756756
757-
Defined in: [packages/form-core/src/FormApi.ts:1158](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1158)
757+
Defined in: [packages/form-core/src/FormApi.ts:1160](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1160)
758758
759759
Validates all fields using the correct handlers for a given validation cause.
760760
@@ -779,7 +779,7 @@ validateArrayFieldsStartingFrom<TField>(
779779
cause): Promise<unknown[]>
780780
```
781781
782-
Defined in: [packages/form-core/src/FormApi.ts:1188](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1188)
782+
Defined in: [packages/form-core/src/FormApi.ts:1190](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1190)
783783
784784
Validates the children of a specified array in the form starting from a given index until the end using the correct handlers for a given validation type.
785785
@@ -813,7 +813,7 @@ Validates the children of a specified array in the form starting from a given in
813813
validateField<TField>(field, cause): unknown[] | Promise<unknown[]>
814814
```
815815
816-
Defined in: [packages/form-core/src/FormApi.ts:1227](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1227)
816+
Defined in: [packages/form-core/src/FormApi.ts:1229](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1229)
817817
818818
Validates a specified field in the form using the correct handlers for a given validation type.
819819

packages/form-core/src/FormApi.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ export class FormApi<
840840
} as AnyFieldMeta
841841
}
842842

843+
if (!Object.keys(currBaseStore.fieldMetaBase).length) return fieldMeta
844+
843845
if (
844846
prevVal &&
845847
originalMetaCount === Object.keys(currBaseStore.fieldMetaBase).length
@@ -1768,14 +1770,24 @@ export class FormApi<
17681770
}
17691771

17701772
deleteField = <TField extends DeepKeys<TFormData>>(field: TField) => {
1773+
const subFieldsToDelete = Object.keys(this.fieldInfo).filter((f) => {
1774+
const fieldStr = field.toString()
1775+
return f !== fieldStr && f.startsWith(fieldStr)
1776+
})
1777+
1778+
const fieldsToDelete = [...subFieldsToDelete, field]
1779+
1780+
// Cleanup the last fields
17711781
this.baseStore.setState((prev) => {
17721782
const newState = { ...prev }
1773-
newState.values = deleteBy(newState.values, field)
1774-
delete newState.fieldMetaBase[field]
1783+
fieldsToDelete.forEach((f) => {
1784+
newState.values = deleteBy(newState.values, f)
1785+
delete this.fieldInfo[f as never]
1786+
delete newState.fieldMetaBase[f as never]
1787+
})
17751788

17761789
return newState
17771790
})
1778-
delete this.fieldInfo[field]
17791791
}
17801792

17811793
/**
@@ -1880,12 +1892,7 @@ export class FormApi<
18801892

18811893
if (lastIndex !== null) {
18821894
const start = `${field}[${lastIndex}]`
1883-
const fieldsToDelete = Object.keys(this.fieldInfo).filter((f) =>
1884-
f.startsWith(start),
1885-
)
1886-
1887-
// Cleanup the last fields
1888-
fieldsToDelete.forEach((f) => this.deleteField(f as TField))
1895+
this.deleteField(start as never)
18891896
}
18901897

18911898
// Validate the whole array + all fields that have shifted

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,4 +1805,49 @@ describe('field api', () => {
18051805
expect(field.getMeta().errors).toStrictEqual([])
18061806
expect(form.state.canSubmit).toBe(true)
18071807
})
1808+
1809+
it('should remove the meta from deleted object fields', async () => {
1810+
const form = new FormApi({
1811+
defaultValues: {
1812+
names: {
1813+
john: {
1814+
firstName: 'John',
1815+
lastName: '',
1816+
},
1817+
martha: {
1818+
firstName: 'Martha',
1819+
lastName: 'Mustermann',
1820+
},
1821+
},
1822+
},
1823+
})
1824+
1825+
const field = new FieldApi({
1826+
form,
1827+
name: 'names.john.lastName',
1828+
validators: {
1829+
onChange: () => 'Last name is required',
1830+
},
1831+
})
1832+
1833+
form.mount()
1834+
field.mount()
1835+
1836+
field.setValue('JohnLastName')
1837+
1838+
expect(form.state.canSubmit).toBe(false)
1839+
1840+
form.deleteField('names.john')
1841+
1842+
expect(form.getFieldValue('names')).toEqual({
1843+
martha: {
1844+
firstName: 'Martha',
1845+
lastName: 'Mustermann',
1846+
},
1847+
})
1848+
1849+
expect(field.getMeta().errorMap).toStrictEqual({})
1850+
expect(field.getMeta().errors).toStrictEqual([])
1851+
expect(form.state.canSubmit).toBe(true)
1852+
})
18081853
})

0 commit comments

Comments
 (0)