Skip to content

Commit

Permalink
fix(core): fix value filtered from none-hidden #3477 (#3481)
Browse files Browse the repository at this point in the history
  • Loading branch information
lumdzeehol committed Oct 23, 2022
1 parent e1539bb commit 617717c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
29 changes: 28 additions & 1 deletion packages/core/src/__tests__/field.spec.ts
Expand Up @@ -2298,12 +2298,39 @@ test('field actions', () => {

test('field hidden value', () => {
const form = attach(createForm())
attach(
const aa = attach(
form.createField({
name: 'aa',
hidden: true,
initialValue: '123',
})
)
expect(form.values).toEqual({ aa: '123' })

const objectField = attach(
form.createObjectField({
name: 'object',
hidden: true,
})
)
const arrayField = attach(
form.createArrayField({
name: 'array',
hidden: true,
})
)

aa.setDisplay('none')
objectField.setDisplay('none')
arrayField.setDisplay('none')
expect(aa.value).toBeUndefined()
expect(objectField.value).toBeUndefined()
expect(arrayField.value).toBeUndefined()

aa.setDisplay('hidden')
objectField.setDisplay('hidden')
arrayField.setDisplay('hidden')
expect(aa.value).toEqual('123')
expect(objectField.value).toEqual({})
expect(arrayField.value).toEqual([])
})
16 changes: 4 additions & 12 deletions packages/core/src/models/Field.ts
@@ -1,10 +1,4 @@
import {
isValid,
isEmpty,
toArr,
FormPathPattern,
isArr,
} from '@formily/shared'
import { isValid, toArr, FormPathPattern, isArr } from '@formily/shared'
import {
ValidatorTriggerType,
parseValidatorDescriptions,
Expand Down Expand Up @@ -245,16 +239,14 @@ export class Field<
() => this.display,
(display) => {
const value = this.value
if (display === 'visible') {
if (isEmpty(value)) {
if (display !== 'none') {
if (!isValid(value)) {
this.setValue(this.caches.value)
this.caches.value = undefined
}
} else {
this.caches.value = toJS(value) ?? toJS(this.initialValue)
if (display === 'none') {
this.form.deleteValuesIn(this.path)
}
this.form.deleteValuesIn(this.path)
}
if (display === 'none' || display === 'hidden') {
this.setFeedback({
Expand Down

0 comments on commit 617717c

Please sign in to comment.