mirror of
https://github.com/LukeHagar/form.git
synced 2025-12-09 20:37:47 +00:00
fix: form's valid state should be up to date (#475)
This commit is contained in:
@@ -134,8 +134,10 @@ export class FormApi<TFormData, ValidatorType> {
|
|||||||
(field) => field?.isValidating,
|
(field) => field?.isValidating,
|
||||||
)
|
)
|
||||||
|
|
||||||
const isFieldsValid = !fieldMetaValues.some((field) =>
|
const isFieldsValid = !fieldMetaValues.some(
|
||||||
isNonEmptyArray(field?.errors),
|
(field) =>
|
||||||
|
field?.errorMap &&
|
||||||
|
isNonEmptyArray(Object.values(field.errorMap).filter(Boolean)),
|
||||||
)
|
)
|
||||||
|
|
||||||
const isTouched = fieldMetaValues.some((field) => field?.isTouched)
|
const isTouched = fieldMetaValues.some((field) => field?.isTouched)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { expect } from 'vitest'
|
import { expect } from 'vitest'
|
||||||
|
|
||||||
import { FormApi } from '../FormApi'
|
import { FormApi } from '../FormApi'
|
||||||
|
import { FieldApi } from '../FieldApi'
|
||||||
|
|
||||||
describe('form api', () => {
|
describe('form api', () => {
|
||||||
it('should get default form state', () => {
|
it('should get default form state', () => {
|
||||||
@@ -267,4 +268,35 @@ describe('form api', () => {
|
|||||||
|
|
||||||
expect(form.getFieldValue('name')).toEqual('two')
|
expect(form.getFieldValue('name')).toEqual('two')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("form's valid state should be work fine", () => {
|
||||||
|
const form = new FormApi({
|
||||||
|
defaultValues: {
|
||||||
|
name: '',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const field = new FieldApi({
|
||||||
|
form,
|
||||||
|
name: 'name',
|
||||||
|
onChange: (v) => (v.length > 0 ? undefined : 'required'),
|
||||||
|
})
|
||||||
|
|
||||||
|
field.mount()
|
||||||
|
|
||||||
|
field.handleChange('one')
|
||||||
|
|
||||||
|
expect(form.state.isFieldsValid).toEqual(true)
|
||||||
|
expect(form.state.canSubmit).toEqual(true)
|
||||||
|
|
||||||
|
field.handleChange('')
|
||||||
|
|
||||||
|
expect(form.state.isFieldsValid).toEqual(false)
|
||||||
|
expect(form.state.canSubmit).toEqual(false)
|
||||||
|
|
||||||
|
field.handleChange('two')
|
||||||
|
|
||||||
|
expect(form.state.isFieldsValid).toEqual(true)
|
||||||
|
expect(form.state.canSubmit).toEqual(true)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user