Skip to content

Commit

Permalink
fix: only set fields to isBlurred after field blur or form submit
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Küsgen <[email protected]>
  • Loading branch information
Pascalmh committed Nov 26, 2024
1 parent aaeb8db commit 791c3c1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
26 changes: 13 additions & 13 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,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 @@ -690,12 +684,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 @@ -967,6 +955,19 @@ export class FormApi<
this.store.setState((prev) => ({ ...prev, isSubmitting: false }))
}

// Set all fields to blurred
this.store.batch(() => {
void (
Object.values(this.fieldInfo) as FieldInfo<any, TFormValidator>[]
).forEach((field) => {
if (!field.instance) return

if (!field.instance.state.meta.isBlurred) {
field.instance.setMeta((prev) => ({ ...prev, isBlurred: true }))
}
})
})

// Validate form and all fields
await this.validateAllFields('submit')

Expand Down Expand Up @@ -1094,7 +1095,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
23 changes: 23 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,29 @@ describe('field api', () => {
expect(field.getValue()).toBe('other')
})

it('should set isBlurred correctly', () => {
const form = new FormApi({
defaultValues: {
names: ['test'],
},
})
form.mount()

const field = new FieldApi({
form,
name: 'names',
})
field.mount()

expect(field.getMeta().isBlurred).toBe(false)

field.pushValue('other')
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
21 changes: 21 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2354,3 +2354,24 @@ describe('form api', () => {
expect(passconfirmField.state.meta.errors.length).toBe(0)
})
})

it('should update the onBlur state of the fields when the form is submitted', async () => {
const form = new FormApi({
defaultValues: {
firstName: '',
},
})

const field = new FieldApi({
form,
name: 'firstName',
})

field.mount()

expect(field.state.meta.isBlurred).toBe(false)

await form.handleSubmit()

expect(field.state.meta.isBlurred).toBe(true)
})

0 comments on commit 791c3c1

Please sign in to comment.