Skip to content

Commit

Permalink
chore(core): improve display model (#1713)
Browse files Browse the repository at this point in the history
* chore(core): improve display model

* test(core): add VoidField test cases
  • Loading branch information
janryWang authored Jul 3, 2021
1 parent 5ca344e commit bad483d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
80 changes: 80 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,3 +1143,83 @@ test('nested fields hidden and validate', async () => {
await form.validate()
expect(form.invalid).toBeFalsy()
})

test('deep nested fields hidden and validate', async () => {
const form = attach(createForm())
const parent1 = attach(
form.createVoidField({
name: 'parent1',
})
)
const parent2 = attach(
form.createVoidField({
name: 'parent2',
basePath: 'parent1',
})
)
const aa = attach(
form.createField({
name: 'aa',
basePath: 'parent1.parent2',
required: true,
})
)
const bb = attach(
form.createField({
name: 'bb',
basePath: 'parent1.parent2',
required: true,
})
)
try {
await form.validate()
} catch {}
expect(form.invalid).toBeTruthy()
parent2.display = 'visible'
parent1.display = 'hidden'
expect(parent2.display).toEqual('hidden')
expect(aa.display).toEqual('hidden')
expect(bb.display).toEqual('hidden')
await form.validate()
expect(form.invalid).toBeFalsy()
})

test('deep nested fields hidden and validate with middle hidden', async () => {
const form = attach(createForm())
const parent1 = attach(
form.createVoidField({
name: 'parent1',
})
)
const parent2 = attach(
form.createVoidField({
name: 'parent2',
basePath: 'parent1',
})
)
const aa = attach(
form.createField({
name: 'aa',
basePath: 'parent1.parent2',
required: true,
})
)
const bb = attach(
form.createField({
name: 'bb',
basePath: 'parent1.parent2',
required: true,
})
)
try {
await form.validate()
} catch {}
expect(form.invalid).toBeTruthy()
parent2.display = 'hidden'
parent1.display = 'none'
expect(parent2.display).toEqual('hidden')
expect(aa.display).toEqual('hidden')
expect(bb.display).toEqual('hidden')
await form.validate()
expect(form.invalid).toBeFalsy()
})
5 changes: 5 additions & 0 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ export class Field<

get display(): FieldDisplayTypes {
const parentDisplay = this.parent?.display
if (parentDisplay && parentDisplay !== 'visible') {
if (this.selfDisplay && this.selfDisplay !== 'visible')
return this.selfDisplay
return parentDisplay
}
if (this.selfDisplay) return this.selfDisplay
return parentDisplay || this.form.display || 'visible'
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/models/VoidField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ export class VoidField<Decorator = any, Component = any, TextType = any> {

get display(): FieldDisplayTypes {
const parentDisplay = this.parent?.display
if (parentDisplay && parentDisplay !== 'visible') {
if (this.selfDisplay && this.selfDisplay !== 'visible')
return this.selfDisplay
return parentDisplay
}
if (isValid(this.selfDisplay)) return this.selfDisplay
return parentDisplay || this.form.display || 'visible'
}
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ export const validateToFeedbacks = async (
})
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
}

Expand Down

0 comments on commit bad483d

Please sign in to comment.