Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
13 changes: 11 additions & 2 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,17 @@ export class FormStore {

// We need fill the list as [] if Form.List is empty
listNamePaths.forEach(namePath => {
if (!getValue(mergedValues, namePath)) {
mergedValues = setValue(mergedValues, namePath, []);
const listValue = getValue(mergedValues, namePath);

// NOTE: `getFieldsValue(['list'])` may not include list root when Form.List isn't wrapped by a parent Field.
// In this case we should fallback to store value first, and only fill `[]` when store doesn't have it.
if (listValue === undefined) {
const storeListValue = getValue(this.store, namePath);
mergedValues = setValue(
mergedValues,
namePath,
storeListValue === undefined ? [] : storeListValue,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved readability and conciseness, you can use the nullish coalescing operator (??) to provide a default value for storeListValue when it is null or undefined.

Suggested change
storeListValue === undefined ? [] : storeListValue,
storeListValue ?? [],

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?? 会漏null

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是 storeListValue === undefined 会漏 null, ??不会

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你目的只想判断undefined的话,就不用理会它

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我的意思就是只想判断undefined;null的时候不处理成[]

);
}
});

Expand Down
35 changes: 35 additions & 0 deletions tests/list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1139,4 +1139,39 @@ describe('Form.List', () => {
{ list: [{ name: 'John', tags: ['react', 'ts', 'redux'] }] },
);
});

it('getFieldsValue should return list root value when Form.List is not wrapped by parent Field', async () => {
let operation: ListOperations;

const [container] = generateForm((fields, opt) => {
operation = opt;
return (
<div>
{fields.map(field => (
<div key={field.key}>
<Field {...field} name={[field.name, 'name']}>
<Input />
</Field>
<Field {...field} name={[field.name, 'value']}>
<Input />
</Field>
</div>
))}
</div>
);
});

// First add a row
act(() => {
operation.add();
});

// Fill some values
await changeValue(getInput(container, 0), 'n1'); // list[0].name
await changeValue(getInput(container, 1), '1'); // list[0].value

expect(form.current?.getFieldsValue(['list'])).toEqual({
list: [{ name: 'n1', value: '1' }],
});
});
});