Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/core/src/__tests__/internals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
deserialize,
isHTMLInputEvent,
} from '../shared/internals'
import { createForm } from '../'
import { createForm, onFieldValueChange } from '../'
import { attach } from './shared'

test('getValuesFromEvent', () => {
Expand Down Expand Up @@ -109,3 +109,31 @@ test('isHTMLInputEvent', () => {
expect(isHTMLInputEvent({ target: {}, stopPropagation() {} })).toBeFalsy()
expect(isHTMLInputEvent({})).toBeFalsy()
})

test('reset when field display is none should be keep value', () => {
const valueChange = jest.fn()
const form = attach(
createForm({
initialValues: {
input: '123',
},
effects: () => {
onFieldValueChange('input', valueChange)
},
})
)
attach(
form.createField({
name: 'input',
})
)
expect(form.values.input).toEqual('123')
form.fields['input'].setDisplay('none')
expect(form.values.input).toBeUndefined()
expect(valueChange).toBeCalledTimes(1)
form.reset()
form.reset()
form.fields['input'].setDisplay('visible')
expect(form.values.input).toEqual('123')
expect(valueChange).toBeCalledTimes(2)
})
7 changes: 7 additions & 0 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,13 @@ export const resetSelf = batch.bound(
target.inputValue = typedDefaultValue
target.inputValues = []
target.caches = {}
if (target.display === 'none') {
const value =
options?.forceClear || isUndef(target.initialValue)
? typedDefaultValue
: toJS(target.initialValue)
target.caches.value = value
}
if (!isUndef(target.value)) {
if (options?.forceClear) {
target.value = typedDefaultValue
Expand Down
Loading