Skip to content

Commit 593b214

Browse files
authored
fix: Preserve should not break Form.List (#163)
1 parent 782d14f commit 593b214

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/useForm.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,10 @@ export class FormStore {
499499
// Clean up store value if preserve
500500
const mergedPreserve = preserve !== undefined ? preserve : this.preserve;
501501
if (mergedPreserve === false) {
502-
this.store = setValue(this.store, entity.getNamePath(), undefined);
502+
const namePath = entity.getNamePath();
503+
if (this.getFieldValue(namePath) !== undefined) {
504+
this.store = setValue(this.store, namePath, undefined);
505+
}
503506
}
504507
};
505508
};

tests/list.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,45 @@ describe('Form.List', () => {
283283

284284
errorSpy.mockRestore();
285285
});
286+
287+
// https://github.com/ant-design/ant-design/issues/25584
288+
it('preserve should not break list', async () => {
289+
let operation;
290+
const [wrapper] = generateForm(
291+
(fields, opt) => {
292+
operation = opt;
293+
return (
294+
<div>
295+
{fields.map(field => (
296+
<Field {...field}>
297+
<Input />
298+
</Field>
299+
))}
300+
</div>
301+
);
302+
},
303+
{ preserve: false },
304+
);
305+
306+
// Add
307+
act(() => {
308+
operation.add();
309+
});
310+
wrapper.update();
311+
expect(wrapper.find(Input)).toHaveLength(1);
312+
313+
// Remove
314+
act(() => {
315+
operation.remove(0);
316+
});
317+
wrapper.update();
318+
expect(wrapper.find(Input)).toHaveLength(0);
319+
320+
// Add
321+
act(() => {
322+
operation.add();
323+
});
324+
wrapper.update();
325+
expect(wrapper.find(Input)).toHaveLength(1);
326+
});
286327
});

0 commit comments

Comments
 (0)