Skip to content

Commit f8c2040

Browse files
authored
fix: field hidden with null value (#3783)
1 parent b458efd commit f8c2040

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/core/src/models/Field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export class Field<
246246
(display) => {
247247
const value = this.value
248248
if (display !== 'none') {
249-
if (!isValid(value)) {
249+
if (value === undefined && this.caches.value !== undefined) {
250250
this.setValue(this.caches.value)
251251
this.caches.value = undefined
252252
}

packages/react/src/__tests__/expression.spec.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import React from 'react'
22
import { render, waitFor } from '@testing-library/react'
33
import { createForm } from '@formily/core'
4-
import { FormProvider, ExpressionScope, createSchemaField, useField } from '..'
4+
import {
5+
FormProvider,
6+
ExpressionScope,
7+
createSchemaField,
8+
useField,
9+
Field,
10+
} from '..'
511

612
test('expression scope', async () => {
713
const Container = (props) => {
@@ -73,3 +79,33 @@ test('x-compile-omitted', async () => {
7379
expect(queryByTestId('input')?.textContent).toBe('{{fake}}123321extra')
7480
})
7581
})
82+
83+
test('field hidden & visible', async () => {
84+
const form = createForm({ initialValues: { empty: null } })
85+
const { findByTestId } = render(
86+
<FormProvider form={form}>
87+
<div data-testid="testid">
88+
<Field name="empty" component={['input']} />
89+
</div>
90+
</FormProvider>
91+
)
92+
await findByTestId('testid')
93+
//
94+
expect(form.fields.empty.hidden).toBe(false)
95+
expect(form.fields.empty.value).toBe(null)
96+
form.fields.empty.hidden = true
97+
expect(form.fields.empty.hidden).toBe(true)
98+
expect(form.fields.empty.value).toBe(null)
99+
form.fields.empty.hidden = false
100+
expect(form.fields.empty.hidden).toBe(false)
101+
expect(form.fields.empty.value).toBe(null)
102+
//
103+
expect(form.fields.empty.visible).toBe(true)
104+
expect(form.fields.empty.value).toBe(null)
105+
form.fields.empty.visible = false
106+
expect(form.fields.empty.visible).toBe(false)
107+
expect(form.fields.empty.value).toBe(undefined)
108+
form.fields.empty.visible = true
109+
expect(form.fields.empty.visible).toBe(true)
110+
expect(form.fields.empty.value).toBe(null)
111+
})

0 commit comments

Comments
 (0)