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
19 changes: 11 additions & 8 deletions src/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
};
};

Expand Down
94 changes: 94 additions & 0 deletions tests/preserve.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormInstance>();

const Demo: React.FC<{ showMemo: boolean }> = ({ showMemo }) => (
<Form
ref={formRef}
initialValues={{
lines: [
{ id: 1, memo: 'first memo' },
{ id: 2, memo: 'second memo' },
],
}}
>
<Form.List name="lines">
{(fields, { remove }) => (
<>
{fields.map(field => (
<div key={field.key}>
<Form.Field name={[field.name, 'id']}>
<input />
</Form.Field>
{showMemo && (
<Form.Field name={[field.name, 'memo']} preserve={false}>
<input />
</Form.Field>
)}
</div>
))}
<button type="button" className="remove-first" onClick={() => remove(0)}>
remove first row
</button>
</>
)}
</Form.List>
</Form>
);

const { container, rerender } = render(<Demo showMemo />);

fireEvent.click(container.querySelector('.remove-first'));
rerender(<Demo showMemo={false} />);

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<FormInstance>();

const Demo: React.FC<{ showMemo: boolean }> = ({ showMemo }) => (
<Form
ref={formRef}
initialValues={{
lines: [
{ id: 1, memo: 'first memo' },
{ id: 2, memo: 'second memo' },
],
}}
>
<Form.List name="lines">
{(fields, { move }) => (
<>
{fields.map(field => (
<div key={field.key}>
<Form.Field name={[field.name, 'id']}>
<input />
</Form.Field>
{showMemo && (
<Form.Field name={[field.name, 'memo']} preserve={false}>
<input />
</Form.Field>
)}
</div>
))}
<button type="button" className="move-first" onClick={() => move(0, 1)}>
move first row
</button>
</>
)}
</Form.List>
</Form>
);

const { container, rerender } = render(<Demo showMemo />);

fireEvent.click(container.querySelector('.move-first'));
rerender(<Demo showMemo={false} />);

expect(formRef.current?.getFieldsValue(true)).toEqual({
lines: [{ id: 2 }, { id: 1 }],
});
});
});