mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-10 05:00:59 +00:00
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.
This commit is contained in:
@@ -143,8 +143,20 @@ export default function DynamicFormComponent({
|
|||||||
|
|
||||||
// 监听表单值变化
|
// 监听表单值变化
|
||||||
useEffect(() => {
|
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<string, object>,
|
||||||
|
);
|
||||||
|
onSubmit?.(initialFinalValues);
|
||||||
|
|
||||||
const subscription = form.watch(() => {
|
const subscription = form.watch(() => {
|
||||||
// 获取完整的表单值,确保包含所有默认值
|
|
||||||
const formValues = form.getValues();
|
const formValues = form.getValues();
|
||||||
const finalValues = itemConfigList.reduce(
|
const finalValues = itemConfigList.reduce(
|
||||||
(acc, item) => {
|
(acc, item) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user