fix: prevent recursive form sync in type editor
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user