@@ -6,7 +6,7 @@ import type { FormProps } from '../src';
66import type { ListField , ListOperations , ListProps } from '../src/List' ;
77import type { FormInstance , Meta } from '../src/interface' ;
88import ListContext from '../src/ListContext' ;
9- import { Input } from './common/InfoField' ;
9+ import InfoField , { Input } from './common/InfoField' ;
1010import { changeValue , getInput } from './common' ;
1111import 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