From 94d72c378c7c64e0dff4810d5e3fb62fef9c88b2 Mon Sep 17 00:00:00 2001 From: Dongze Yang <50231148+ydzat@users.noreply.github.com> Date: Wed, 25 Feb 2026 06:34:52 +0100 Subject: [PATCH] fix(web): emit initial form values on mount to prevent saving empty config (#2004) 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. --- .../dynamic-form/DynamicFormComponent.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/web/src/app/home/components/dynamic-form/DynamicFormComponent.tsx b/web/src/app/home/components/dynamic-form/DynamicFormComponent.tsx index f46d42b5..7570a084 100644 --- a/web/src/app/home/components/dynamic-form/DynamicFormComponent.tsx +++ b/web/src/app/home/components/dynamic-form/DynamicFormComponent.tsx @@ -143,8 +143,20 @@ export default function DynamicFormComponent({ // 监听表单值变化 useEffect(() => { + // Emit initial form values immediately so the parent always has a valid snapshot, + // even if the user saves without modifying any field. + // form.watch(callback) only fires on subsequent changes, not on mount. + const formValues = form.getValues(); + const initialFinalValues = itemConfigList.reduce( + (acc, item) => { + acc[item.name] = formValues[item.name] ?? item.default; + return acc; + }, + {} as Record, + ); + onSubmit?.(initialFinalValues); + const subscription = form.watch(() => { - // 获取完整的表单值,确保包含所有默认值 const formValues = form.getValues(); const finalValues = itemConfigList.reduce( (acc, item) => {