Skip to content

Commit e75b56c

Browse files
committed
fix: incorrect value in add
1 parent 0643abf commit e75b56c

File tree

2 files changed

+6
-26
lines changed

2 files changed

+6
-26
lines changed

packages/components/form/FormList.tsx

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const FormList: React.FC<TdFormListProps> = (props) => {
3939
}, [fullPath, parentFullPath, initialDataFromForm, parentInitialData, props.initialData]);
4040

4141
const [formListValue, setFormListValue] = useState(() => {
42-
const value = cloneDeep(get(form?.store, fullPath) || initialData || []);
42+
const value = get(form?.store, fullPath) || initialData || [];
4343
if (value.length && !get(form?.store, fullPath)) {
4444
set(form?.store, fullPath, value);
4545
}
@@ -65,22 +65,6 @@ const FormList: React.FC<TdFormListProps> = (props) => {
6565
.filter((item) => item !== undefined)
6666
.toString(); // 转化 name
6767

68-
const buildDefaultFieldMap = () => {
69-
if (formListMapRef.current.size <= 0) return {};
70-
const defaultValues: Record<string, any> = {};
71-
formListMapRef.current.forEach((item, itemPath) => {
72-
const itemPathArray = convertNameToArray(itemPath);
73-
const isChildField = itemPathArray.length === convertNameToArray(fullPath).length + 2;
74-
if (!isChildField) return;
75-
const fieldName = itemPathArray[itemPathArray.length - 1];
76-
// add 没有传参时,构建一个包含所有子字段的对象用于占位,确保回调给用户的数据结构完整
77-
// 兼容 add() 或者 add({}) 导致的空对象场景
78-
// https://github.com/Tencent/tdesign-react/issues/2329
79-
defaultValues[fieldName] = item.current.initialData;
80-
});
81-
return defaultValues;
82-
};
83-
8468
const updateFormList = (newFields: any, newFormListValue: any) => {
8569
setFields(newFields);
8670
setFormListValue(newFormListValue);
@@ -98,12 +82,8 @@ const FormList: React.FC<TdFormListProps> = (props) => {
9882
name: index,
9983
isListField: true,
10084
});
101-
const newFormListValue = cloneDeep(formListValue);
102-
if (defaultValue !== undefined) {
103-
newFormListValue.splice(index, 0, defaultValue);
104-
} else {
105-
newFormListValue.splice(index, 0, buildDefaultFieldMap());
106-
}
85+
const newFormListValue = [...formListValue];
86+
newFormListValue.splice(index, 0, cloneDeep(defaultValue));
10787
updateFormList(newFields, newFormListValue);
10888
},
10989
remove(index: number | number[]) {

packages/components/form/_example/form-list.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export default function BaseForm() {
2525
onSubmit={onSubmit}
2626
initialData={{ task: [{ type: 'review' }, { type: 'ui' }] }}
2727
onValuesChange={(change, all) => {
28-
console.log('change:', change, JSON.stringify(change));
29-
console.log('all:', all, JSON.stringify(all));
28+
console.log('change:', change, '\n', JSON.stringify(change));
29+
console.log('all:', all, '\n', JSON.stringify(all));
3030
}}
3131
resetType="initial"
3232
>
@@ -68,7 +68,7 @@ export default function BaseForm() {
6868

6969
<FormItem style={{ marginLeft: 100 }}>
7070
<Space>
71-
<Button onClick={() => add({ taskType: 'dev', notes: '已交付' })}>新增指定项</Button>
71+
<Button onClick={() => add({ type: 'dev', notes: '已交付' })}>新增指定项</Button>
7272
<Button onClick={() => add()}>新增默认项</Button>
7373
</Space>
7474
</FormItem>

0 commit comments

Comments
 (0)