Skip to content

Commit 02ae480

Browse files
authored
fix: Nest Field will trigger clean all store when preserve=false (#212)
* test: test driven * fix: Nest renderProps clean the hole store
1 parent 9c7f04c commit 02ae480

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/useForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ export class FormStore {
500500
const mergedPreserve = preserve !== undefined ? preserve : this.preserve;
501501
if (mergedPreserve === false && !isListField) {
502502
const namePath = entity.getNamePath();
503-
if (this.getFieldValue(namePath) !== undefined) {
503+
if (namePath.length && this.getFieldValue(namePath) !== undefined) {
504504
this.store = setValue(this.store, namePath, undefined);
505505
}
506506
}

tests/preserve.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,35 @@ describe('Form.Preserve', () => {
137137

138138
errorSpy.mockRestore();
139139
});
140+
141+
it('nest render props should not clean full store', () => {
142+
let form: FormInstance;
143+
144+
const wrapper = mount(
145+
<Form
146+
preserve={false}
147+
ref={instance => {
148+
form = instance;
149+
}}
150+
>
151+
<Form.Field name="light">
152+
<input />
153+
</Form.Field>
154+
<Form.Field shouldUpdate>
155+
{(_, __, { getFieldValue }) =>
156+
getFieldValue('light') === 'bamboo' ? <Form.Field>{() => null}</Form.Field> : null
157+
}
158+
</Form.Field>
159+
</Form>,
160+
);
161+
162+
wrapper.find('input').simulate('change', { target: { value: 'bamboo' } });
163+
expect(form.getFieldsValue()).toEqual({ light: 'bamboo' });
164+
165+
wrapper.find('input').simulate('change', { target: { value: 'little' } });
166+
expect(form.getFieldsValue()).toEqual({ light: 'little' });
167+
168+
wrapper.unmount();
169+
});
140170
});
141171
/* eslint-enable no-template-curly-in-string */

0 commit comments

Comments
 (0)