Skip to content

Commit f7a36d2

Browse files
committed
fix: getFieldsValue
1 parent 34783a3 commit f7a36d2

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

packages/components/form/FormList.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,7 @@ const FormList: React.FC<TdFormListProps> = (props) => {
165165
initialData,
166166
isFormList: true,
167167
formListMapRef,
168-
getValue() {
169-
const formListValue = [];
170-
[...formListMapRef.current.values()].forEach((formItemRef) => {
171-
if (!formItemRef.current) return;
172-
const { name, getValue } = formItemRef.current;
173-
const fieldValue = calcFieldValue(name, getValue());
174-
merge(formListValue, fieldValue);
175-
});
176-
return formListValue;
177-
},
168+
getValue: () => get(form?.store, fullPath),
178169
validate: (trigger = 'all') => {
179170
const resultList = [];
180171
const validates = [...formListMapRef.current.values()].map((formItemRef) =>

packages/components/form/__tests__/form-list.test.tsx

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { fireEvent, mockTimeout, render, vi } from '@test/utils';
12
import React from 'react';
23
import { MinusCircleIcon } from 'tdesign-icons-react';
3-
import { fireEvent, mockTimeout, render, vi } from '@test/utils';
44

55
import Button from '../../button';
66
import Input from '../../input';
@@ -305,6 +305,38 @@ describe('Form List 组件测试', () => {
305305
form.clearValidate();
306306
}
307307

308+
function getFieldsValueAll() {
309+
const allValues = form.getFieldsValue(true);
310+
console.log('getFieldsValue(true):', allValues);
311+
document.getElementById('all-values-result')?.setAttribute('data-result', JSON.stringify(allValues));
312+
}
313+
314+
function getFieldsValueByPath() {
315+
// Test 1: Get specific user's data
316+
const usersPath = form.getFieldsValue(['users']);
317+
318+
// Test 2: Get specific nested path - first user's projects
319+
const firstUserProjects = form.getFieldsValue([['users', 0, 'projects']]);
320+
321+
// Test 3: Get specific nested path - first user's first project's tasks
322+
const firstProjectTasks = form.getFieldsValue([['users', 0, 'projects', 0, 'tasks']]);
323+
324+
// Test 4: Get multiple paths at once
325+
const multiplePaths = form.getFieldsValue([
326+
['users', 0, 'name'],
327+
['users', 1, 'name'],
328+
['users', 0, 'projects', 0, 'projectName'],
329+
]);
330+
331+
const result = {
332+
usersPath,
333+
firstUserProjects,
334+
firstProjectTasks,
335+
multiplePaths,
336+
};
337+
document.getElementById('path-values-result')?.setAttribute('data-result', JSON.stringify(result));
338+
}
339+
308340
return (
309341
<Form form={form}>
310342
<FormList name="users">
@@ -425,7 +457,11 @@ describe('Form List 组件测试', () => {
425457
<Button onClick={addNestedData}>addNestedData</Button>
426458
<Button onClick={validate}>validate</Button>
427459
<Button onClick={clearValidate}>clearValidate</Button>
460+
<Button onClick={getFieldsValueAll}>getFieldsValue(true)</Button>
461+
<Button onClick={getFieldsValueByPath}>getFieldsValue(namePath)</Button>
428462
</FormItem>
463+
<div id="all-values-result" data-result="" />
464+
<div id="path-values-result" data-result="" />
429465
</Form>
430466
);
431467
};
@@ -472,6 +508,59 @@ describe('Form List 组件测试', () => {
472508
expect((getByPlaceholderText('task-name-1-0-0') as HTMLInputElement).value).toBe('Write API docs');
473509
expect((getByPlaceholderText('task-status-1-0-0') as HTMLInputElement).value).toBe('in-progress');
474510

511+
// Test getFieldsValue(true) - should get all form values
512+
fireEvent.click(queryByText('getFieldsValue(true)'));
513+
await mockTimeout();
514+
const allValuesResult = container.querySelector('#all-values-result')?.getAttribute('data-result');
515+
const allValues = JSON.parse(allValuesResult || '{}');
516+
expect(allValues).toHaveProperty('users');
517+
expect(Array.isArray(allValues.users)).toBe(true);
518+
expect(allValues.users.length).toBe(2);
519+
expect(allValues.users[0].name).toBe('Charlie');
520+
expect(allValues.users[0].projects.length).toBe(2);
521+
expect(allValues.users[0].projects[0].projectName).toBe('Backend Service');
522+
expect(allValues.users[0].projects[0].tasks.length).toBe(3);
523+
expect(allValues.users[1].name).toBe('David');
524+
525+
// Test getFieldsValue with complex namePath
526+
fireEvent.click(queryByText('getFieldsValue(namePath)'));
527+
await mockTimeout();
528+
const pathValuesResult = container.querySelector('#path-values-result')?.getAttribute('data-result');
529+
const pathValues = JSON.parse(pathValuesResult || '{}');
530+
531+
// Test 1: Verify users path returns all users data
532+
expect(pathValues.usersPath).toHaveProperty('users');
533+
expect(Array.isArray(pathValues.usersPath.users)).toBe(true);
534+
expect(pathValues.usersPath.users.length).toBe(2);
535+
expect(pathValues.usersPath.users[0].name).toBe('Charlie');
536+
expect(pathValues.usersPath.users[1].name).toBe('David');
537+
538+
// Test 2: Verify nested path returns first user's projects
539+
expect(pathValues.firstUserProjects).toHaveProperty('users');
540+
expect(pathValues.firstUserProjects.users[0]).toHaveProperty('projects');
541+
expect(Array.isArray(pathValues.firstUserProjects.users[0].projects)).toBe(true);
542+
expect(pathValues.firstUserProjects.users[0].projects.length).toBe(2);
543+
expect(pathValues.firstUserProjects.users[0].projects[0].projectName).toBe('Backend Service');
544+
expect(pathValues.firstUserProjects.users[0].projects[1].projectName).toBe('DevOps');
545+
546+
// Test 3: Verify deep nested path returns first project's tasks
547+
expect(pathValues.firstProjectTasks).toHaveProperty('users');
548+
expect(pathValues.firstProjectTasks.users[0].projects[0]).toHaveProperty('tasks');
549+
expect(Array.isArray(pathValues.firstProjectTasks.users[0].projects[0].tasks)).toBe(true);
550+
expect(pathValues.firstProjectTasks.users[0].projects[0].tasks.length).toBe(3);
551+
expect(pathValues.firstProjectTasks.users[0].projects[0].tasks[0].taskName).toBe('Database design');
552+
expect(pathValues.firstProjectTasks.users[0].projects[0].tasks[1].taskName).toBe('API development');
553+
expect(pathValues.firstProjectTasks.users[0].projects[0].tasks[2].taskName).toBe('Testing');
554+
555+
// Test 4: Verify multiple paths returns specific fields
556+
expect(pathValues.multiplePaths).toHaveProperty('users');
557+
expect(pathValues.multiplePaths.users[0]).toHaveProperty('name');
558+
expect(pathValues.multiplePaths.users[0].name).toBe('Charlie');
559+
expect(pathValues.multiplePaths.users[1]).toHaveProperty('name');
560+
expect(pathValues.multiplePaths.users[1].name).toBe('David');
561+
expect(pathValues.multiplePaths.users[0].projects[0]).toHaveProperty('projectName');
562+
expect(pathValues.multiplePaths.users[0].projects[0].projectName).toBe('Backend Service');
563+
475564
// Test remove nested task - remove first task of Charlie's Backend Service project
476565
const removeTaskBtn = container.querySelector('.test-remove-task-0-0-0');
477566
fireEvent.click(removeTaskBtn);

0 commit comments

Comments
 (0)