diff --git a/src/hooks/useForm.ts b/src/hooks/useForm.ts index 0385e563..0201ebbc 100644 --- a/src/hooks/useForm.ts +++ b/src/hooks/useForm.ts @@ -675,31 +675,34 @@ export class FormStore { return (isListField?: boolean, preserve?: boolean, subNamePath: InternalNamePath = []) => { this.fieldEntities = this.fieldEntities.filter(item => item !== entity); + // `namePath` may be changed by `List` move/remove after registration + const currentNamePath = entity.getNamePath(); + // Clean up store value if not preserve if (!this.isMergedPreserve(preserve) && (!isListField || subNamePath.length > 1)) { - const defaultValue = isListField ? undefined : this.getInitialValue(namePath); + const defaultValue = isListField ? undefined : this.getInitialValue(currentNamePath); if ( - namePath.length && - this.getFieldValue(namePath) !== defaultValue && + currentNamePath.length && + this.getFieldValue(currentNamePath) !== defaultValue && this.fieldEntities.every( field => // Only reset when no namePath exist - !matchNamePath(field.getNamePath(), namePath), + !matchNamePath(field.getNamePath(), currentNamePath), ) ) { const prevStore = this.store; - this.updateStore(setValue(prevStore, namePath, defaultValue, true)); + this.updateStore(setValue(prevStore, currentNamePath, defaultValue, true)); // Notify that field is unmount - this.notifyObservers(prevStore, [namePath], { type: 'remove' }); + this.notifyObservers(prevStore, [currentNamePath], { type: 'remove' }); // Dependencies update - this.triggerDependenciesUpdate(prevStore, namePath); + this.triggerDependenciesUpdate(prevStore, currentNamePath); } } - this.notifyWatch([namePath]); + this.notifyWatch([currentNamePath]); }; }; diff --git a/tests/preserve.test.tsx b/tests/preserve.test.tsx index e4951f2d..edab3e12 100644 --- a/tests/preserve.test.tsx +++ b/tests/preserve.test.tsx @@ -445,4 +445,98 @@ describe('Form.Preserve', () => { fireEvent.click(container.querySelector('button')); expect(container.querySelector('input')).toBeFalsy(); }); + + it('clean up nested list field with preserve={false} after rows are removed', () => { + const formRef = React.createRef(); + + const Demo: React.FC<{ showMemo: boolean }> = ({ showMemo }) => ( +
+ + {(fields, { remove }) => ( + <> + {fields.map(field => ( +
+ + + + {showMemo && ( + + + + )} +
+ ))} + + + )} +
+
+ ); + + const { container, rerender } = render(); + + fireEvent.click(container.querySelector('.remove-first')); + rerender(); + + expect(formRef.current?.getFieldsValue(true)).toEqual({ + lines: [{ id: 2 }], + }); + }); + + it('clean up nested list field with preserve={false} after rows are moved', () => { + const formRef = React.createRef(); + + const Demo: React.FC<{ showMemo: boolean }> = ({ showMemo }) => ( +
+ + {(fields, { move }) => ( + <> + {fields.map(field => ( +
+ + + + {showMemo && ( + + + + )} +
+ ))} + + + )} +
+
+ ); + + const { container, rerender } = render(); + + fireEvent.click(container.querySelector('.move-first')); + rerender(); + + expect(formRef.current?.getFieldsValue(true)).toEqual({ + lines: [{ id: 2 }, { id: 1 }], + }); + }); });