Skip to content

Commit

Permalink
fix: field hidden with null value (#3783)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsbhqt authored Apr 10, 2023
1 parent b458efd commit f8c2040
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class Field<
(display) => {
const value = this.value
if (display !== 'none') {
if (!isValid(value)) {
if (value === undefined && this.caches.value !== undefined) {
this.setValue(this.caches.value)
this.caches.value = undefined
}
Expand Down
38 changes: 37 additions & 1 deletion packages/react/src/__tests__/expression.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React from 'react'
import { render, waitFor } from '@testing-library/react'
import { createForm } from '@formily/core'
import { FormProvider, ExpressionScope, createSchemaField, useField } from '..'
import {
FormProvider,
ExpressionScope,
createSchemaField,
useField,
Field,
} from '..'

test('expression scope', async () => {
const Container = (props) => {
Expand Down Expand Up @@ -73,3 +79,33 @@ test('x-compile-omitted', async () => {
expect(queryByTestId('input')?.textContent).toBe('{{fake}}123321extra')
})
})

test('field hidden & visible', async () => {
const form = createForm({ initialValues: { empty: null } })
const { findByTestId } = render(
<FormProvider form={form}>
<div data-testid="testid">
<Field name="empty" component={['input']} />
</div>
</FormProvider>
)
await findByTestId('testid')
//
expect(form.fields.empty.hidden).toBe(false)
expect(form.fields.empty.value).toBe(null)
form.fields.empty.hidden = true
expect(form.fields.empty.hidden).toBe(true)
expect(form.fields.empty.value).toBe(null)
form.fields.empty.hidden = false
expect(form.fields.empty.hidden).toBe(false)
expect(form.fields.empty.value).toBe(null)
//
expect(form.fields.empty.visible).toBe(true)
expect(form.fields.empty.value).toBe(null)
form.fields.empty.visible = false
expect(form.fields.empty.visible).toBe(false)
expect(form.fields.empty.value).toBe(undefined)
form.fields.empty.visible = true
expect(form.fields.empty.visible).toBe(true)
expect(form.fields.empty.value).toBe(null)
})

0 comments on commit f8c2040

Please sign in to comment.