Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,16 @@ 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, []);
// Form.List 的数组结构(add/remove)会先同步更新 store,
// 而对应的子 Field 要到下一次 commit 才会 register。
// 在这段 render 时序中,fieldEntities 仍然是“上一帧”的结果,
// 用它来拼 list 会得到不完整的数据。
// 因此 List 根节点的值始终以 store 为准,避免依赖 Field 注册时序。
const storeListValue = getValue(this.store, namePath);

if (storeListValue !== undefined) {
mergedValues = setValue(mergedValues, namePath, storeListValue);
return;
}
});

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' }],
});
});
});