From 8058a952101f5abe9dbe5a32a55ca618a06b9bc5 Mon Sep 17 00:00:00 2001 From: AlekseyManetov Date: Mon, 7 Oct 2024 17:50:53 +0200 Subject: [PATCH] [Form]: fixed bug when `beforeLeave` modal appears in case of redirect in `onSuccess` callback. --- public/docs/content/examples-labeledInput-Basic.json | 12 +++++++++++- uui-core/src/data/forms/__tests__/useForm.test.tsx | 4 ++-- uui-core/src/data/forms/useForm.ts | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/public/docs/content/examples-labeledInput-Basic.json b/public/docs/content/examples-labeledInput-Basic.json index b97dc59021..b9da7fc246 100644 --- a/public/docs/content/examples-labeledInput-Basic.json +++ b/public/docs/content/examples-labeledInput-Basic.json @@ -1 +1,11 @@ -{"object":"value","document":{"object":"document","data":{},"nodes":[{"object":"block","type":"paragraph","data":{},"nodes":[{"object":"text","text":"Basic LabeledInput usage","marks":[]}]}]}} \ No newline at end of file +[ + { + "type": "paragraph", + "data": {}, + "children": [ + { + "text": "Basic LabeledInput usage" + } + ] + } +] \ No newline at end of file diff --git a/uui-core/src/data/forms/__tests__/useForm.test.tsx b/uui-core/src/data/forms/__tests__/useForm.test.tsx index 324c35ed05..dddd5f962b 100644 --- a/uui-core/src/data/forms/__tests__/useForm.test.tsx +++ b/uui-core/src/data/forms/__tests__/useForm.test.tsx @@ -625,7 +625,7 @@ describe('useForm', () => { it('Should unblock router when form is saved', async () => { const saveMock = jest.fn().mockResolvedValue(true); - const beforeLeaveMock = jest.fn().mockResolvedValue(false); + const beforeLeaveMock = jest.fn().mockRejectedValue(false); const { wrapper, testUuiCtx: svc } = getDefaultUUiContextWrapper(); const props = { value: testData, @@ -676,7 +676,7 @@ describe('useForm', () => { expect(beforeLeaveMock).toHaveBeenCalledTimes(1); }); - it('Should make redirect and lock form aging if beforeLeave return "remain"', async () => { + it('Should make redirect and lock form aging if beforeLeave returns "remain"', async () => { const saveMock = jest.fn().mockResolvedValue(true); const beforeLeaveMock = jest.fn().mockResolvedValue('remain'); const { wrapper, testUuiCtx: svc } = getDefaultUUiContextWrapper(); diff --git a/uui-core/src/data/forms/useForm.ts b/uui-core/src/data/forms/useForm.ts index dd8f46c2ef..b331d64855 100644 --- a/uui-core/src/data/forms/useForm.ts +++ b/uui-core/src/data/forms/useForm.ts @@ -259,9 +259,9 @@ export function useForm(props: UseFormProps): IFormApi { } flushSync(() => { resetForm(newState); + removeUnsavedChanges(); + unblock(); }); - removeUnsavedChanges(); - unblock(); if (propsRef.current.onSuccess && response) { propsRef.current.onSuccess(response.form, isSavedBeforeLeave);