Skip to content

Commit

Permalink
fix: set fields to isBlurred only after field blur event (#940)
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Küsgen <[email protected]>
  • Loading branch information
Pascalmh authored Dec 17, 2024
1 parent a4e4cbb commit 1f043e9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 30 deletions.
34 changes: 17 additions & 17 deletions docs/reference/classes/formapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ deleteField<TField>(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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand All @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -514,7 +514,7 @@ resetFieldMeta<TField>(fieldMeta): Record<TField, FieldMeta>
#### 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)
***
Expand All @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
***
Expand Down Expand Up @@ -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)
13 changes: 0 additions & 13 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
}
})
})

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions packages/form-core/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
30 changes: 30 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 1f043e9

Please sign in to comment.