Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: List nest Field should correct removed #737

Merged
merged 4 commits into from
Nov 27, 2024
Merged
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
8 changes: 5 additions & 3 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,10 @@ function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>)
const listContext = React.useContext(ListContext);
const namePath = name !== undefined ? getNamePath(name) : undefined;

const isMergedListField = restProps.isListField ?? !!listContext;

let key: string = 'keep';
if (!restProps.isListField) {
if (!isMergedListField) {
key = `_${(namePath || []).join('_')}`;
}

Expand All @@ -699,7 +701,7 @@ function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>)
if (
process.env.NODE_ENV !== 'production' &&
restProps.preserve === false &&
restProps.isListField &&
isMergedListField &&
namePath.length <= 1
) {
warning(false, '`preserve` should not apply on Form.List fields.');
Expand All @@ -709,7 +711,7 @@ function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>)
<Field
key={key}
name={namePath}
isListField={!!listContext}
isListField={isMergedListField}
{...restProps}
fieldContext={fieldContext}
/>
Expand Down
47 changes: 46 additions & 1 deletion tests/list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { FormProps } from '../src';
import type { ListField, ListOperations, ListProps } from '../src/List';
import type { FormInstance, Meta } from '../src/interface';
import ListContext from '../src/ListContext';
import { Input } from './common/InfoField';
import InfoField, { Input } from './common/InfoField';
import { changeValue, getInput } from './common';
import timeout from './common/timeout';

Expand Down Expand Up @@ -892,4 +892,49 @@ describe('Form.List', () => {
await changeValue(getInput(container, 2), 'changed3');
expect(formRef.current.isFieldsTouched(true)).toBeTruthy();
});

// https://github.com/ant-design/ant-design/issues/51702
it('list nest field should ignore preserve', async () => {
const formRef = React.createRef<FormInstance>();

const { container } = render(
<Form ref={formRef} preserve={false}>
<Form.List name="list">
{(fields, { remove }) => {
return (
<>
{fields.map(field => (
<InfoField key={field.key} name={[field.name, 'user']} />
))}
<button onClick={() => remove(1)}>remove</button>
</>
);
}}
</Form.List>
</Form>,
);

formRef.current!.setFieldValue('list', [
{ user: '1' },
{ user: '2' },
{
user: '3',
},
]);

await act(async () => {
await timeout();
});

expect(container.querySelectorAll('input')).toHaveLength(3);

// Remove 1 should keep correct value
fireEvent.click(container.querySelector('button')!);

await act(async () => {
await timeout();
});

expect(formRef.current!.getFieldValue('list')).toEqual([{ user: '1' }, { user: '3' }]);
});
});