Skip to content

Commit 617717c

Browse files
authored
fix(core): fix value filtered from none-hidden #3477 (#3481)
1 parent e1539bb commit 617717c

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

packages/core/src/__tests__/field.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2298,12 +2298,39 @@ test('field actions', () => {
22982298

22992299
test('field hidden value', () => {
23002300
const form = attach(createForm())
2301-
attach(
2301+
const aa = attach(
23022302
form.createField({
23032303
name: 'aa',
23042304
hidden: true,
23052305
initialValue: '123',
23062306
})
23072307
)
23082308
expect(form.values).toEqual({ aa: '123' })
2309+
2310+
const objectField = attach(
2311+
form.createObjectField({
2312+
name: 'object',
2313+
hidden: true,
2314+
})
2315+
)
2316+
const arrayField = attach(
2317+
form.createArrayField({
2318+
name: 'array',
2319+
hidden: true,
2320+
})
2321+
)
2322+
2323+
aa.setDisplay('none')
2324+
objectField.setDisplay('none')
2325+
arrayField.setDisplay('none')
2326+
expect(aa.value).toBeUndefined()
2327+
expect(objectField.value).toBeUndefined()
2328+
expect(arrayField.value).toBeUndefined()
2329+
2330+
aa.setDisplay('hidden')
2331+
objectField.setDisplay('hidden')
2332+
arrayField.setDisplay('hidden')
2333+
expect(aa.value).toEqual('123')
2334+
expect(objectField.value).toEqual({})
2335+
expect(arrayField.value).toEqual([])
23092336
})

packages/core/src/models/Field.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
isValid,
3-
isEmpty,
4-
toArr,
5-
FormPathPattern,
6-
isArr,
7-
} from '@formily/shared'
1+
import { isValid, toArr, FormPathPattern, isArr } from '@formily/shared'
82
import {
93
ValidatorTriggerType,
104
parseValidatorDescriptions,
@@ -245,16 +239,14 @@ export class Field<
245239
() => this.display,
246240
(display) => {
247241
const value = this.value
248-
if (display === 'visible') {
249-
if (isEmpty(value)) {
242+
if (display !== 'none') {
243+
if (!isValid(value)) {
250244
this.setValue(this.caches.value)
251245
this.caches.value = undefined
252246
}
253247
} else {
254248
this.caches.value = toJS(value) ?? toJS(this.initialValue)
255-
if (display === 'none') {
256-
this.form.deleteValuesIn(this.path)
257-
}
249+
this.form.deleteValuesIn(this.path)
258250
}
259251
if (display === 'none' || display === 'hidden') {
260252
this.setFeedback({

0 commit comments

Comments
 (0)