From 367e356765c26ddf6df6bb8031560062843f0160 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Thu, 18 Sep 2025 11:44:08 +0200 Subject: [PATCH] fix: prevent recursive form sync in type editor --- app/components/TypeEditForm.vue | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/app/components/TypeEditForm.vue b/app/components/TypeEditForm.vue index 274fd54..f6d2bcc 100644 --- a/app/components/TypeEditForm.vue +++ b/app/components/TypeEditForm.vue @@ -103,10 +103,22 @@ const formData = reactive(createDefaultForm(props.modelValue)) const allExpanded = ref(false) const expandAllTrigger = ref(0) +let syncingFromParent = false +const toPlainObject = (value) => JSON.parse(JSON.stringify(value)) +const lastSnapshot = ref(toPlainObject(createDefaultForm(props.modelValue))) + watch( () => props.modelValue, (value) => { - Object.assign(formData, createDefaultForm(value)) + const normalized = createDefaultForm(value) + if (JSON.stringify(normalized) === JSON.stringify(lastSnapshot.value)) { + return + } + + syncingFromParent = true + Object.assign(formData, normalized) + lastSnapshot.value = toPlainObject(normalized) + syncingFromParent = false }, { deep: true } ) @@ -114,7 +126,14 @@ watch( watch( formData, (value) => { - emit('update:modelValue', createDefaultForm(value)) + if (syncingFromParent) return + const normalized = createDefaultForm(value) + if (JSON.stringify(normalized) === JSON.stringify(lastSnapshot.value)) { + return + } + + lastSnapshot.value = toPlainObject(normalized) + emit('update:modelValue', normalized) }, { deep: true } ) @@ -125,7 +144,11 @@ const toggleAllSections = () => { } const resetForm = () => { - Object.assign(formData, createDefaultForm(props.modelValue)) + const normalized = createDefaultForm(props.modelValue) + syncingFromParent = true + Object.assign(formData, normalized) + lastSnapshot.value = toPlainObject(normalized) + syncingFromParent = false allExpanded.value = false expandAllTrigger.value += 1 }