Skip to content

Commit a43a38a

Browse files
committed
feat: review
1 parent 6f2f069 commit a43a38a

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/useForm.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,10 +925,8 @@ export class FormStore {
925925

926926
// Add field if not provide `nameList`
927927
if (!provideNameList) {
928-
if (field.isList()) {
929-
if (namePathList.find(name => name.toString().includes(fieldNamePath.toString()))) {
930-
return;
931-
}
928+
if (field.isList() && namePathList.some(name => matchNamePath(name, fieldNamePath, true))) {
929+
return;
932930
}
933931
namePathList.push(fieldNamePath);
934932
}

tests/list.test.tsx

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { fireEvent, render, act } from '@testing-library/react';
33
import { resetWarned } from '@rc-component/util/lib/warning';
44
import Form, { Field, List } from '../src';
@@ -937,4 +937,67 @@ describe('Form.List', () => {
937937

938938
expect(formRef.current!.getFieldValue('list')).toEqual([{ user: '1' }, { user: '3' }]);
939939
});
940+
941+
it('list unmount', async () => {
942+
const valueRef = React.createRef();
943+
944+
const Demo = () => {
945+
const [isShow, setIsShow] = useState(true);
946+
return (
947+
<Form
948+
initialValues={{
949+
users: [
950+
{ name: 'a', age: '1' },
951+
{ name: 'b', age: '2' },
952+
],
953+
}}
954+
onFinish={values => {
955+
valueRef.current = values;
956+
}}
957+
>
958+
<Form.List name="users">
959+
{fields => {
960+
return fields.map(field => (
961+
<div key={field.key} style={{ display: 'flex', gap: 10 }}>
962+
<InfoField name={[field.name, 'name']}>
963+
<Input />
964+
</InfoField>
965+
{isShow && (
966+
<InfoField name={[field.name, 'age']}>
967+
<Input />
968+
</InfoField>
969+
)}
970+
</div>
971+
));
972+
}}
973+
</Form.List>
974+
<button data-testid="hide" type="button" onClick={() => setIsShow(c => !c)}>
975+
隐藏
976+
</button>
977+
<button type="submit" data-testid="submit">
978+
Submit
979+
</button>
980+
</Form>
981+
);
982+
};
983+
984+
const { queryByTestId } = render(<Demo />);
985+
fireEvent.click(queryByTestId('submit'));
986+
await act(async () => {
987+
await timeout();
988+
});
989+
expect(valueRef.current).toEqual({
990+
users: [
991+
{ name: 'a', age: '1' },
992+
{ name: 'b', age: '2' },
993+
],
994+
});
995+
996+
fireEvent.click(queryByTestId('hide'));
997+
fireEvent.click(queryByTestId('submit'));
998+
await act(async () => {
999+
await timeout();
1000+
});
1001+
expect(valueRef.current).toEqual({ users: [{ name: 'a' }, { name: 'b' }] });
1002+
});
9401003
});

0 commit comments

Comments
 (0)