Skip to content

Commit 9ea3ad9

Browse files
committed
fix(core): reset when field display is none should be keep value
1 parent d9a4644 commit 9ea3ad9

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
deserialize,
66
isHTMLInputEvent,
77
} from '../shared/internals'
8-
import { createForm } from '../'
8+
import { createForm, onFieldValueChange } from '../'
99
import { attach } from './shared'
1010

1111
test('getValuesFromEvent', () => {
@@ -109,3 +109,31 @@ test('isHTMLInputEvent', () => {
109109
expect(isHTMLInputEvent({ target: {}, stopPropagation() {} })).toBeFalsy()
110110
expect(isHTMLInputEvent({})).toBeFalsy()
111111
})
112+
113+
test('reset when field display is none should be keep value', () => {
114+
const valueChange = jest.fn()
115+
const form = attach(
116+
createForm({
117+
initialValues: {
118+
input: '123',
119+
},
120+
effects: () => {
121+
onFieldValueChange('input', valueChange)
122+
},
123+
})
124+
)
125+
attach(
126+
form.createField({
127+
name: 'input',
128+
})
129+
)
130+
expect(form.values.input).toEqual('123')
131+
form.fields['input'].setDisplay('none')
132+
expect(form.values.input).toBeUndefined()
133+
expect(valueChange).toBeCalledTimes(1)
134+
form.reset()
135+
form.reset()
136+
form.fields['input'].setDisplay('visible')
137+
expect(form.values.input).toEqual('123')
138+
expect(valueChange).toBeCalledTimes(2)
139+
})

packages/core/src/shared/internals.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,13 @@ export const resetSelf = batch.bound(
995995
target.inputValue = typedDefaultValue
996996
target.inputValues = []
997997
target.caches = {}
998+
if (target.display === 'none') {
999+
const value =
1000+
options?.forceClear || isUndef(target.initialValue)
1001+
? typedDefaultValue
1002+
: toJS(target.initialValue)
1003+
target.caches.value = value
1004+
}
9981005
if (!isUndef(target.value)) {
9991006
if (options?.forceClear) {
10001007
target.value = typedDefaultValue

0 commit comments

Comments
 (0)