Skip to content

Commit

Permalink
feat(core): skip validate when parent.visible is equal hidden/none (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jul 3, 2021
1 parent 3de82d5 commit 0076ef7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
30 changes: 30 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Expand Up @@ -1113,3 +1113,33 @@ test('reaction in reaction', () => {
expect(field2.value).toEqual(undefined)
expect(field2.display).toEqual('none')
})

test('nested fields hidden and validate', async () => {
const form = attach(createForm())
const parent = attach(
form.createVoidField({
name: 'parent',
})
)
attach(
form.createField({
name: 'aa',
basePath: 'parent',
required: true,
})
)
attach(
form.createField({
name: 'bb',
basePath: 'parent',
required: true,
})
)
try {
await form.validate()
} catch {}
expect(form.invalid).toBeTruthy()
parent.display = 'hidden'
await form.validate()
expect(form.invalid).toBeFalsy()
})
14 changes: 11 additions & 3 deletions packages/core/src/shared/internals.ts
Expand Up @@ -195,15 +195,23 @@ export const validateToFeedbacks = async (
validateFirst: field.props.validateFirst || field.form.props.validateFirst,
context: this,
})
const shouldSkipValidate =
field.display !== 'visible' || field.pattern !== 'editable'
const takeSkipCondition = () => {
if (field.display !== 'visible') return true
if (field.parent?.display) {
if (field.parent.display !== 'visible') return true
}
if (field.pattern !== 'editable') return true

return false
}

batch(() => {
each(results, (messages, type) => {
field.setFeedback({
triggerType,
type,
code: pascalCase(`validate-${type}`),
messages: shouldSkipValidate ? [] : messages,
messages: takeSkipCondition() ? [] : messages,
} as any)
})
})
Expand Down

0 comments on commit 0076ef7

Please sign in to comment.