Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Form): fixed setFieldsValue is invalid when prop.name type is array #3279

Merged
Merged
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
76 changes: 75 additions & 1 deletion src/form/__tests__/form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Button from '../../button';
import Radio from '../../radio';
import { HelpCircleIcon } from 'tdesign-icons-react';

const { FormItem } = Form;
const { FormItem, FormList } = Form;

describe('Form 组件测试', () => {
const submitFn = vi.fn();
Expand Down Expand Up @@ -116,6 +116,80 @@ describe('Form 组件测试', () => {
expect(queryByText('input1 未填写')).not.toBeTruthy();
});

test('form setFieldsValue', () => {
const mockName = 'name';
const mockName1 = 'name1';
const mockBirthday = '1996-01-24';
const mockBirthday1 = '1996-01-25';
const mockArea = '北京';
const mockArea1 = '北京';
const TestForm = () => {
const [form] = Form.useForm();
const handleSetFormData = () => {
form.setFieldsValue({
user: {
name: mockName1,
},
birthday: mockBirthday1,
address: [
{
area: mockArea1,
},
],
});
};
return (
<Form
form={form}
initialData={{
user: {
name: mockName,
},
birthday: mockBirthday,
address: [
{
area: mockArea,
},
],
}}
>
<FormItem label="姓名" name={['user', 'name']}>
<Input placeholder="name" />
</FormItem>
<FormItem label="生日" name="birthday">
<Input placeholder="birthday" />
</FormItem>
<FormList name="address">
{(fields) => (
<>
{fields.map(({ key, name, ...restField }) => (
<FormItem key={key}>
<FormItem {...restField} name={[name, 'area']} label="地区">
<Input placeholder="area" />
</FormItem>
</FormItem>
))}
</>
)}
</FormList>
<FormItem>
<Button onClick={handleSetFormData}>SetFormData</Button>
</FormItem>
</Form>
);
};

const { getByPlaceholderText, getByText } = render(<TestForm />);
expect((getByPlaceholderText('name') as HTMLInputElement).value).toBe(mockName);
expect((getByPlaceholderText('birthday') as HTMLInputElement).value).toBe(mockBirthday);
expect((getByPlaceholderText('area') as HTMLInputElement).value).toBe(mockArea);

fireEvent.click(getByText('SetFormData'));
expect((getByPlaceholderText('name') as HTMLInputElement).value).toBe(mockName1);
expect((getByPlaceholderText('birthday') as HTMLInputElement).value).toBe(mockBirthday1);
expect((getByPlaceholderText('area') as HTMLInputElement).value).toBe(mockArea1);
});

test('Form.reset works fine', async () => {
const initialVal = 'test input';

Expand Down
16 changes: 15 additions & 1 deletion src/form/hooks/useInstance.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isEmpty from 'lodash/isEmpty';
import isFunction from 'lodash/isFunction';
import isEqual from 'lodash/isEqual';
import merge from 'lodash/merge';
import get from 'lodash/get';
import set from 'lodash/set';
Expand Down Expand Up @@ -155,7 +156,20 @@ export default function useInstance(

nameLists.forEach((nameList) => {
const fieldValue = get(fields, nameList);
const formItemRef = formMapRef.current.get(nameList.length > 1 ? nameList : nameList[0]);

let formItemRef;
if (nameList.length > 1) {
// 如果是数组,由于内存地址不一致,不能直接使用 Map.get 获取到 formItemRef
for (const [mapNameList, _formItemRef] of formMapRef.current.entries()) {
if (isEqual(nameList, mapNameList)) {
formItemRef = _formItemRef;
break;
}
}
} else {
formItemRef = formMapRef.current.get(nameList[0]);
}

if (formItemRef?.current) {
formItemRef?.current?.setValue?.(fieldValue, fields);
} else {
Expand Down
Loading