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): fix form getFieldsValue bug #3149

Merged
merged 1 commit into from
Oct 31, 2024
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
7 changes: 6 additions & 1 deletion src/form/hooks/useInstance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ export default function useInstance(props: TdFormProps, formRef, formMapRef: Rea
if (nameList === true) {
// 嵌套数组子节点先添加导致外层数据覆盖因而需要倒序遍历
for (const [name, formItemRef] of [...formMapRef.current.entries()].reverse()) {
const fieldValue = calcFieldValue(name, formItemRef?.current.getValue?.());
let fieldValue = null;
if (formItemRef?.current.isFormList) {
fieldValue = calcFieldValue(name, formItemRef?.current.getValue?.());
} else {
fieldValue = calcFieldValue(name, formItemRef?.current.getValue?.(), false);
}
merge(fieldsValue, fieldValue);
}
} else {
Expand Down
22 changes: 15 additions & 7 deletions src/form/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ export function getMapValue(name: NamePath, formMapRef: React.MutableRefObject<M
}

// 将数据整理成对象,数组 name 转化嵌套对象: ['user', 'name'] => { user: { name: '' } }
export function calcFieldValue(name: NamePath, value: any) {
export function calcFieldValue(name: NamePath, value: any, isFormList = true) {
if (Array.isArray(name)) {
const fieldValue = name.reduceRight((prev, curr) => {
const arr = [];
if (/^\d+$/.test(String(curr))) arr[curr] = prev;
return arr.length ? arr : { [curr]: prev };
}, value);
return { ...fieldValue };
if (isFormList) {
const fieldValue = name.reduceRight((prev, curr) => {
const arr = [];
if (/^\d+$/.test(String(curr))) arr[curr] = prev;
return arr.length ? arr : { [curr]: prev };
}, value);
return { ...fieldValue };
}
return name.reduceRight((prev, curr, currentIndex) => {
if (currentIndex === name.length - 1) {
return { [curr]: value };
}
return { [curr]: prev };
}, {});
}

return { [name]: value };
Expand Down
Loading