diff --git a/docs/reference/classes/formapi.md b/docs/reference/classes/formapi.md index 2925d8061..b83a304bc 100644 --- a/docs/reference/classes/formapi.md +++ b/docs/reference/classes/formapi.md @@ -125,7 +125,7 @@ deleteField(field): void #### Defined in -[packages/form-core/src/FormApi.ts:1122](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1122) +[packages/form-core/src/FormApi.ts:1109](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1109) *** @@ -153,7 +153,7 @@ Gets the field info of the specified field. #### Defined in -[packages/form-core/src/FormApi.ts:1033](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1033) +[packages/form-core/src/FormApi.ts:1021](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1021) *** @@ -181,7 +181,7 @@ Gets the metadata of the specified field. #### Defined in -[packages/form-core/src/FormApi.ts:1024](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1024) +[packages/form-core/src/FormApi.ts:1012](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1012) *** @@ -209,7 +209,7 @@ Gets the value of the specified field. #### Defined in -[packages/form-core/src/FormApi.ts:1017](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1017) +[packages/form-core/src/FormApi.ts:1005](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1005) *** @@ -227,7 +227,7 @@ Handles the form submission, performs validation, and calls the appropriate onSu #### Defined in -[packages/form-core/src/FormApi.ts:958](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L958) +[packages/form-core/src/FormApi.ts:946](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L946) *** @@ -271,7 +271,7 @@ Inserts a value into an array field at the specified index, shifting the subsequ #### Defined in -[packages/form-core/src/FormApi.ts:1154](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1154) +[packages/form-core/src/FormApi.ts:1141](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1141) *** @@ -331,7 +331,7 @@ Moves the value at the first specified index to the second specified index withi #### Defined in -[packages/form-core/src/FormApi.ts:1272](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1272) +[packages/form-core/src/FormApi.ts:1259](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1259) *** @@ -370,7 +370,7 @@ Pushes a value into an array field. #### Defined in -[packages/form-core/src/FormApi.ts:1136](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1136) +[packages/form-core/src/FormApi.ts:1123](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1123) *** @@ -409,7 +409,7 @@ Removes a value from an array field at the specified index. #### Defined in -[packages/form-core/src/FormApi.ts:1207](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1207) +[packages/form-core/src/FormApi.ts:1194](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1194) *** @@ -453,7 +453,7 @@ Replaces a value into an array field at the specified index. #### Defined in -[packages/form-core/src/FormApi.ts:1181](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1181) +[packages/form-core/src/FormApi.ts:1168](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1168) *** @@ -514,7 +514,7 @@ resetFieldMeta(fieldMeta): Record #### Defined in -[packages/form-core/src/FormApi.ts:1067](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1067) +[packages/form-core/src/FormApi.ts:1055](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1055) *** @@ -538,7 +538,7 @@ Updates the form's errorMap #### Defined in -[packages/form-core/src/FormApi.ts:1296](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1296) +[packages/form-core/src/FormApi.ts:1283](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1283) *** @@ -570,7 +570,7 @@ Updates the metadata of the specified field. #### Defined in -[packages/form-core/src/FormApi.ts:1052](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1052) +[packages/form-core/src/FormApi.ts:1040](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1040) *** @@ -609,7 +609,7 @@ Sets the value of the specified field and optionally updates the touched state. #### Defined in -[packages/form-core/src/FormApi.ts:1091](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1091) +[packages/form-core/src/FormApi.ts:1079](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1079) *** @@ -653,7 +653,7 @@ Swaps the values at the specified indices within an array field. #### Defined in -[packages/form-core/src/FormApi.ts:1246](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1246) +[packages/form-core/src/FormApi.ts:1233](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1233) *** @@ -740,7 +740,7 @@ Validates the children of a specified array in the form starting from a given in #### Defined in -[packages/form-core/src/FormApi.ts:646](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L646) +[packages/form-core/src/FormApi.ts:640](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L640) *** @@ -772,4 +772,4 @@ Validates a specified field in the form using the correct handlers for a given v #### Defined in -[packages/form-core/src/FormApi.ts:685](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L685) +[packages/form-core/src/FormApi.ts:679](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L679) diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 163435872..af0190c28 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -627,12 +627,6 @@ export class FormApi< // Mark them as touched field.instance.setMeta((prev) => ({ ...prev, isTouched: true })) } - - // If any fields are not blurred - if (!field.instance.state.meta.isBlurred) { - // Mark them as blurred - field.instance.setMeta((prev) => ({ ...prev, isBlurred: true })) - } }) }) @@ -696,12 +690,6 @@ export class FormApi< fieldInstance.setMeta((prev) => ({ ...prev, isTouched: true })) } - // If the field is not blurred (same logic as in validateAllFields) - if (!fieldInstance.state.meta.isBlurred) { - // Mark it as blurred - fieldInstance.setMeta((prev) => ({ ...prev, isBlurred: true })) - } - return fieldInstance.validate(cause) } @@ -1100,7 +1088,6 @@ export class FormApi< this.setFieldMeta(field, (prev) => ({ ...prev, isTouched: true, - isBlurred: true, isDirty: true, errorMap: { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition diff --git a/packages/form-core/tests/FieldApi.spec.ts b/packages/form-core/tests/FieldApi.spec.ts index 4a6dbf2de..8bb6dd393 100644 --- a/packages/form-core/tests/FieldApi.spec.ts +++ b/packages/form-core/tests/FieldApi.spec.ts @@ -95,6 +95,52 @@ describe('field api', () => { expect(field.getValue()).toBe('other') }) + it('should set isBlurred correctly', () => { + const form = new FormApi({ + defaultValues: { + firstName: '', + }, + }) + form.mount() + + const field = new FieldApi({ + form, + name: 'firstName', + }) + field.mount() + + expect(field.getMeta().isBlurred).toBe(false) + + field.setValue('Bob') + expect(field.getMeta().isBlurred).toBe(false) + + field.handleBlur() + expect(field.getMeta().isBlurred).toBe(true) + }) + + it('should set isBlurred correctly for arrays', () => { + const form = new FormApi({ + defaultValues: { + firstNames: ['Bob'], + }, + }) + form.mount() + + const field = new FieldApi({ + form, + name: 'firstNames', + }) + field.mount() + + expect(field.getMeta().isBlurred).toBe(false) + + field.pushValue('Bill') + expect(field.getMeta().isBlurred).toBe(false) + + field.handleBlur() + expect(field.getMeta().isBlurred).toBe(true) + }) + it('should push an array value correctly', () => { const form = new FormApi({ defaultValues: { diff --git a/packages/form-core/tests/FormApi.spec.ts b/packages/form-core/tests/FormApi.spec.ts index 0dedab0d6..cae8e71c4 100644 --- a/packages/form-core/tests/FormApi.spec.ts +++ b/packages/form-core/tests/FormApi.spec.ts @@ -2385,3 +2385,33 @@ describe('form api', () => { ) }) }) + +it('should not change the onBlur state of the fields when the form is submitted', async () => { + const form = new FormApi({ + defaultValues: { + firstName: '', + lastName: '', + }, + }) + + const firstNameField = new FieldApi({ + form, + name: 'firstName', + }) + firstNameField.mount() + + const lastNameField = new FieldApi({ + form, + name: 'lastName', + }) + lastNameField.mount() + + firstNameField.handleBlur() + + expect(firstNameField.state.meta.isBlurred).toBe(true) + + await form.handleSubmit() + + expect(firstNameField.state.meta.isBlurred).toBe(true) + expect(lastNameField.state.meta.isBlurred).toBe(false) +})