Skip to content

Commit 4c37153

Browse files
authored
fix: List nest Field should correct removed (#737)
* fix: List remove not match * test: update test * fix: logic adjust * chore: warning update
1 parent 60f43be commit 4c37153

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/Field.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,10 @@ function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>)
689689
const listContext = React.useContext(ListContext);
690690
const namePath = name !== undefined ? getNamePath(name) : undefined;
691691

692+
const isMergedListField = restProps.isListField ?? !!listContext;
693+
692694
let key: string = 'keep';
693-
if (!restProps.isListField) {
695+
if (!isMergedListField) {
694696
key = `_${(namePath || []).join('_')}`;
695697
}
696698

@@ -699,7 +701,7 @@ function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>)
699701
if (
700702
process.env.NODE_ENV !== 'production' &&
701703
restProps.preserve === false &&
702-
restProps.isListField &&
704+
isMergedListField &&
703705
namePath.length <= 1
704706
) {
705707
warning(false, '`preserve` should not apply on Form.List fields.');
@@ -709,7 +711,7 @@ function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>)
709711
<Field
710712
key={key}
711713
name={namePath}
712-
isListField={!!listContext}
714+
isListField={isMergedListField}
713715
{...restProps}
714716
fieldContext={fieldContext}
715717
/>

tests/list.test.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { FormProps } from '../src';
66
import type { ListField, ListOperations, ListProps } from '../src/List';
77
import type { FormInstance, Meta } from '../src/interface';
88
import ListContext from '../src/ListContext';
9-
import { Input } from './common/InfoField';
9+
import InfoField, { Input } from './common/InfoField';
1010
import { changeValue, getInput } from './common';
1111
import timeout from './common/timeout';
1212

@@ -892,4 +892,49 @@ describe('Form.List', () => {
892892
await changeValue(getInput(container, 2), 'changed3');
893893
expect(formRef.current.isFieldsTouched(true)).toBeTruthy();
894894
});
895+
896+
// https://github.com/ant-design/ant-design/issues/51702
897+
it('list nest field should ignore preserve', async () => {
898+
const formRef = React.createRef<FormInstance>();
899+
900+
const { container } = render(
901+
<Form ref={formRef} preserve={false}>
902+
<Form.List name="list">
903+
{(fields, { remove }) => {
904+
return (
905+
<>
906+
{fields.map(field => (
907+
<InfoField key={field.key} name={[field.name, 'user']} />
908+
))}
909+
<button onClick={() => remove(1)}>remove</button>
910+
</>
911+
);
912+
}}
913+
</Form.List>
914+
</Form>,
915+
);
916+
917+
formRef.current!.setFieldValue('list', [
918+
{ user: '1' },
919+
{ user: '2' },
920+
{
921+
user: '3',
922+
},
923+
]);
924+
925+
await act(async () => {
926+
await timeout();
927+
});
928+
929+
expect(container.querySelectorAll('input')).toHaveLength(3);
930+
931+
// Remove 1 should keep correct value
932+
fireEvent.click(container.querySelector('button')!);
933+
934+
await act(async () => {
935+
await timeout();
936+
});
937+
938+
expect(formRef.current!.getFieldValue('list')).toEqual([{ user: '1' }, { user: '3' }]);
939+
});
895940
});

0 commit comments

Comments
 (0)