Skip to content

Commit 1aeae44

Browse files
authored
fix: Reset should ignore ListField (#622)
* test: test driven * test: test driven * fix: ListField reset
1 parent 657f623 commit 1aeae44

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/useForm.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { merge } from 'rc-util/lib/utils/set';
12
import warning from 'rc-util/lib/warning';
23
import * as React from 'react';
34
import { HOOK_MARK } from './FieldContext';
@@ -6,12 +7,15 @@ import type {
67
FieldData,
78
FieldEntity,
89
FieldError,
10+
FilterFunc,
911
FormInstance,
12+
GetFieldsValueConfig,
1013
InternalFieldData,
1114
InternalFormInstance,
1215
InternalHooks,
1316
InternalNamePath,
1417
InternalValidateFields,
18+
InternalValidateOptions,
1519
Meta,
1620
NamePath,
1721
NotifyInfo,
@@ -20,14 +24,10 @@ import type {
2024
StoreValue,
2125
ValidateErrorEntity,
2226
ValidateMessages,
23-
InternalValidateOptions,
2427
ValuedNotifyInfo,
2528
WatchCallBack,
26-
FilterFunc,
27-
GetFieldsValueConfig,
2829
} from './interface';
2930
import { allPromiseFinish } from './utils/asyncUtil';
30-
import { merge } from 'rc-util/lib/utils/set';
3131
import { defaultValidateMessages } from './utils/messages';
3232
import NameMap from './utils/NameMap';
3333
import {
@@ -510,8 +510,10 @@ export class FormStore {
510510
);
511511
} else if (records) {
512512
const originValue = this.getFieldValue(namePath);
513+
const isListField = field.isListField();
514+
513515
// Set `initialValue`
514-
if (!info.skipExist || originValue === undefined) {
516+
if (!isListField && (!info.skipExist || originValue === undefined)) {
515517
this.updateStore(setValue(this.store, namePath, [...records][0].value));
516518
}
517519
}

tests/initialValue.test.tsx

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { useState } from 'react';
1+
import { act, fireEvent, render } from '@testing-library/react';
22
import { resetWarned } from 'rc-util/lib/warning';
3-
import Form, { Field, useForm, List, type FormInstance } from '../src';
4-
import { Input } from './common/InfoField';
3+
import React, { useState } from 'react';
4+
import Form, { Field, List, useForm, type FormInstance } from '../src';
55
import { changeValue, getInput } from './common';
6-
import { act, fireEvent, render } from '@testing-library/react';
6+
import { Input } from './common/InfoField';
77

88
describe('Form.InitialValues', () => {
99
it('works', () => {
@@ -382,4 +382,35 @@ describe('Form.InitialValues', () => {
382382
unmount();
383383
});
384384
});
385+
386+
it('should ignore in Form.List', () => {
387+
const { container } = render(
388+
<Form>
389+
<Form.List name="list">
390+
{(fields, { add }) => (
391+
<>
392+
<button
393+
onClick={() => {
394+
add();
395+
}}
396+
/>
397+
{fields.map(field => (
398+
<Field {...field} initialValue="bamboo" key={field.key}>
399+
<Input />
400+
</Field>
401+
))}
402+
</>
403+
)}
404+
</Form.List>
405+
</Form>,
406+
);
407+
408+
fireEvent.click(container.querySelector('button'));
409+
fireEvent.change(getInput(container), { target: { value: 'light' } });
410+
expect(getInput(container).value).toEqual('light');
411+
412+
// Reset
413+
fireEvent.reset(container.querySelector('form'));
414+
expect(container.querySelectorAll('input')).toHaveLength(0);
415+
});
385416
});

0 commit comments

Comments
 (0)