Skip to content

Commit 6db0a30

Browse files
committed
fix: only set fields to isBlurred after field blur or form submit
Signed-off-by: Pascal Küsgen <pascalkuesgen@gmail.com>
1 parent a440028 commit 6db0a30

File tree

4 files changed

+66
-22
lines changed

4 files changed

+66
-22
lines changed

docs/reference/classes/formapi.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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:1014](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1014)
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:1005](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1005)
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:998](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L998)
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:925](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L925)
221221
222222
***
223223
@@ -449,7 +449,7 @@ resetFieldMeta<TField>(fieldMeta): Record<TField, FieldMeta>
449449
450450
#### Defined in
451451
452-
[packages/form-core/src/FormApi.ts:1047](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1047)
452+
[packages/form-core/src/FormApi.ts:1048](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1048)
453453
454454
***
455455
@@ -499,7 +499,7 @@ Updates the metadata of the specified field.
499499
500500
#### Defined in
501501
502-
[packages/form-core/src/FormApi.ts:1032](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1032)
502+
[packages/form-core/src/FormApi.ts:1033](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1033)
503503
504504
***
505505
@@ -532,7 +532,7 @@ Sets the value of the specified field and optionally updates the touched state.
532532
533533
#### Defined in
534534
535-
[packages/form-core/src/FormApi.ts:1071](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1071)
535+
[packages/form-core/src/FormApi.ts:1072](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1072)
536536
537537
***
538538
@@ -645,7 +645,7 @@ Validates the children of a specified array in the form starting from a given in
645645
646646
#### Defined in
647647
648-
[packages/form-core/src/FormApi.ts:625](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L625)
648+
[packages/form-core/src/FormApi.ts:619](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L619)
649649
650650
***
651651
@@ -673,4 +673,4 @@ Validates a specified field in the form using the correct handlers for a given v
673673
674674
#### Defined in
675675
676-
[packages/form-core/src/FormApi.ts:664](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L664)
676+
[packages/form-core/src/FormApi.ts:658](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L658)

packages/form-core/src/FormApi.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,6 @@ export class FormApi<
606606
// Mark them as touched
607607
field.instance.setMeta((prev) => ({ ...prev, isTouched: true }))
608608
}
609-
610-
// If any fields are not blurred
611-
if (!field.instance.state.meta.isBlurred) {
612-
// Mark them as blurred
613-
field.instance.setMeta((prev) => ({ ...prev, isBlurred: true }))
614-
}
615609
})
616610
})
617611

@@ -675,12 +669,6 @@ export class FormApi<
675669
fieldInstance.setMeta((prev) => ({ ...prev, isTouched: true }))
676670
}
677671

678-
// If the field is not blurred (same logic as in validateAllFields)
679-
if (!fieldInstance.state.meta.isBlurred) {
680-
// Mark it as blurred
681-
fieldInstance.setMeta((prev) => ({ ...prev, isBlurred: true }))
682-
}
683-
684672
return fieldInstance.validate(cause)
685673
}
686674

@@ -952,6 +940,19 @@ export class FormApi<
952940
this.store.setState((prev) => ({ ...prev, isSubmitting: false }))
953941
}
954942

943+
// Set all fields to blurred
944+
this.store.batch(() => {
945+
void (
946+
Object.values(this.fieldInfo) as FieldInfo<any, TFormValidator>[]
947+
).forEach((field) => {
948+
if (!field.instance) return
949+
950+
if (!field.instance.state.meta.isBlurred) {
951+
field.instance.setMeta((prev) => ({ ...prev, isBlurred: true }))
952+
}
953+
})
954+
})
955+
955956
// Validate all fields
956957
await this.validateAllFields('submit')
957958

@@ -1080,7 +1081,6 @@ export class FormApi<
10801081
this.setFieldMeta(field, (prev) => ({
10811082
...prev,
10821083
isTouched: true,
1083-
isBlurred: true,
10841084
isDirty: true,
10851085
errorMap: {
10861086
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ describe('field api', () => {
9595
expect(field.getValue()).toBe('other')
9696
})
9797

98+
it('should set isBlurred correctly', () => {
99+
const form = new FormApi({
100+
defaultValues: {
101+
names: ['test'],
102+
},
103+
})
104+
form.mount()
105+
106+
const field = new FieldApi({
107+
form,
108+
name: 'names',
109+
})
110+
field.mount()
111+
112+
expect(field.getMeta().isBlurred).toBe(false)
113+
114+
field.pushValue('other')
115+
expect(field.getMeta().isBlurred).toBe(false)
116+
117+
field.handleBlur()
118+
expect(field.getMeta().isBlurred).toBe(true)
119+
})
120+
98121
it('should push an array value correctly', () => {
99122
const form = new FormApi({
100123
defaultValues: {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,3 +2053,24 @@ describe('form api', () => {
20532053
expect(passconfirmField.state.meta.errors.length).toBe(0)
20542054
})
20552055
})
2056+
2057+
it('should update the onBlur state of the fields when the form is submitted', async () => {
2058+
const form = new FormApi({
2059+
defaultValues: {
2060+
firstName: '',
2061+
},
2062+
})
2063+
2064+
const field = new FieldApi({
2065+
form,
2066+
name: 'firstName',
2067+
})
2068+
2069+
field.mount()
2070+
2071+
expect(field.state.meta.isBlurred).toBe(false)
2072+
2073+
await form.handleSubmit()
2074+
2075+
expect(field.state.meta.isBlurred).toBe(true)
2076+
})

0 commit comments

Comments
 (0)