Skip to content

Commit ac0512d

Browse files
committed
fix(web): emit initial form values on mount to prevent saving empty config
DynamicFormComponent uses form.watch(callback) to notify parent of form values, but react-hook-form's watch callback only fires on subsequent changes, not on mount. This causes PluginForm's currentFormValues to remain as {} if the user saves without modifying any field, overwriting the existing plugin config with an empty object in the database.
1 parent 20f0e99 commit ac0512d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

web/src/app/home/components/dynamic-form/DynamicFormComponent.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,20 @@ export default function DynamicFormComponent({
143143

144144
// 监听表单值变化
145145
useEffect(() => {
146+
// Emit initial form values immediately so the parent always has a valid snapshot,
147+
// even if the user saves without modifying any field.
148+
// form.watch(callback) only fires on subsequent changes, not on mount.
149+
const formValues = form.getValues();
150+
const initialFinalValues = itemConfigList.reduce(
151+
(acc, item) => {
152+
acc[item.name] = formValues[item.name] ?? item.default;
153+
return acc;
154+
},
155+
{} as Record<string, object>,
156+
);
157+
onSubmit?.(initialFinalValues);
158+
146159
const subscription = form.watch(() => {
147-
// 获取完整的表单值,确保包含所有默认值
148160
const formValues = form.getValues();
149161
const finalValues = itemConfigList.reduce(
150162
(acc, item) => {

0 commit comments

Comments
 (0)