fix(frontend): Add newly spawned sections in the same order as template

This commit is contained in:
2026-02-14 09:07:14 +01:00
parent c85fefefb8
commit 31efbe72ab

View File

@@ -69,7 +69,31 @@ export function useSectionSpawning() {
}
const newSection = spawnSectionFromTemplate(templateSection, element.reference!, triggerValue)
return sections.concat(newSection as FormElementSectionDto)
// Find template index
const templateIndex = sections.findIndex(
(s) => s.isTemplate && s.templateReference === trigger.templateReference
)
if (templateIndex === -1) {
// Fallback: append if template not found (shouldn't happen)
return sections.concat(newSection as FormElementSectionDto)
}
// Find insertion position: after template and after any existing spawned sections from same template
let insertionIndex = templateIndex + 1
while (insertionIndex < sections.length) {
const section = sections[insertionIndex]!
if (section.isTemplate || section.templateReference !== trigger.templateReference) {
break
}
insertionIndex++
}
// Insert at calculated position
const result = [...sections]
result.splice(insertionIndex, 0, newSection as FormElementSectionDto)
return result
}
function updateSpawnedSectionTitles(