From 7f85f42347cf5c58478108b05548040e364ba8d4 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 26 May 2025 15:59:22 +0300 Subject: [PATCH] FIO-9944: Fixes an issue where some form instances will not be destroyed --- src/components/Form.tsx | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/components/Form.tsx b/src/components/Form.tsx index cb87afa37..45b81df7f 100644 --- a/src/components/Form.tsx +++ b/src/components/Form.tsx @@ -252,7 +252,8 @@ const getEffectiveProps = (props: FormProps) => { export const Form = (props: FormProps) => { const renderElement = useRef(null); const currentFormJson = useRef(null); - const { formConstructor, formSource, formReadyCallback } = getEffectiveProps(props); + const { formConstructor, formSource, formReadyCallback } = + getEffectiveProps(props); const { src, form, @@ -295,9 +296,10 @@ export const Form = (props: FormProps) => { console.warn('Form source not found'); return; } - currentFormJson.current = formSource && typeof formSource !== 'string' - ? structuredClone(formSource) - : null; + currentFormJson.current = + formSource && typeof formSource !== 'string' + ? structuredClone(formSource) + : null; const instance = await createWebformInstance( formConstructor, currentFormJson.current || formSource, @@ -319,7 +321,12 @@ export const Form = (props: FormProps) => { if (formReadyCallback) { formReadyCallback(instance); } - setFormInstance(instance); + setFormInstance((prevInstance: any) => { + if (prevInstance) { + prevInstance.destroy(true); + } + return instance; + }); } else { console.warn('Failed to create form instance'); } @@ -338,7 +345,8 @@ export const Form = (props: FormProps) => { useEffect(() => { let onAnyHandler = null; if (formInstance && Object.keys(handlers).length > 0) { - onAnyHandler = (...args: [string, ...any[]]) => onAnyEvent(handlers, ...args); + onAnyHandler = (...args: [string, ...any[]]) => + onAnyEvent(handlers, ...args); formInstance.onAny(onAnyHandler); }