Skip to content
Open
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
17 changes: 15 additions & 2 deletions packages/components/form/FormList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const FormList: React.FC<TdFormListProps> = (props) => {
if (data !== undefined) callback(formItemRef, data);
});

updateFormList(newFields, fieldData);
updateFormList(newFields, cloneDeep(fieldData));
}

useEffect(() => {
Expand Down Expand Up @@ -177,8 +177,21 @@ const FormList: React.FC<TdFormListProps> = (props) => {
});
});
},
// TODO 支持局部更新数据
setValue: (fieldData) => {
// 支持局部更新数据: { [index]: value }
if (fieldData !== null && typeof fieldData === 'object' && !Array.isArray(fieldData)) {
const currentValue = cloneDeep(get(form?.store, fullPath) || []);
Object.keys(fieldData).forEach((key) => {
const index = Number(key);
if (!Number.isNaN(index)) {
currentValue[index] = merge({}, currentValue[index], fieldData[key]);
}
});
setListFields(currentValue, (formItemRef, data) => {
formItemRef?.current?.setValue?.(data);
});
return;
}
setListFields(fieldData, (formItemRef, data) => {
formItemRef?.current?.setValue?.(data);
});
Expand Down
22 changes: 17 additions & 5 deletions packages/components/form/hooks/useInstance.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cloneDeep, isArray, isEmpty, isFunction, isObject, merge, set, get } from 'lodash-es';
import { cloneDeep, get, isArray, isEmpty, isFunction, isObject, merge, set } from 'lodash-es';
import log from '@tdesign/common-js/log/index';
import useConfig from '../../hooks/useConfig';
import { calcFieldValue, findFormItem, travelMapFromObject } from '../utils';
Expand Down Expand Up @@ -162,6 +162,15 @@ export default function useInstance(
return cloneDeep(fieldsValue);
}

/**
* 将值同步写入 floatingFormDataRef 和 form.store
*(无对应 FormItem 时的 fallback)
*/
function updateFloatingValue(name: NamePath, value: any) {
set(floatingFormDataRef.current, name, value);
set(form?.store, name, value);
}

/**
* 对外方法,设置对应 FormItem 的值
*/
Expand All @@ -182,9 +191,8 @@ export default function useInstance(
setValueByPath(value[key], [...path, key]);
});
} else {
set(floatingFormDataRef.current, path, value);
// 确保 store 始终包含所有被 set 的字段
set(form?.store, path, value);
updateFloatingValue(path, value);
}
};

Expand All @@ -198,9 +206,13 @@ export default function useInstance(
if (!Array.isArray(fields)) throw new TypeError('The parameter of "setFields" must be an array');

fields.forEach((field) => {
const { name, ...restFields } = field;
const { name, value, ...restFields } = field;
const formItemRef = findFormItem(name, formMapRef);
formItemRef?.current?.setField(restFields);
if (formItemRef?.current) {
formItemRef.current.setField({ value, ...restFields });
} else if ('value' in field) {
updateFloatingValue(name, value);
}
});
}

Expand Down
Loading