From 2680ea4fe2e09b64c38c1269f81a8346611341a9 Mon Sep 17 00:00:00 2001 From: mkurczewski Date: Tue, 15 Oct 2024 09:30:37 +0200 Subject: [PATCH] Added form reset --- libs/forms/feature/src/use-form-field.ts | 17 ++++++++++++++++- libs/forms/feature/src/use-form-register.ts | 15 +++++++++------ .../ui/src/lib/interactive/form/form.tsx | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/libs/forms/feature/src/use-form-field.ts b/libs/forms/feature/src/use-form-field.ts index 814e48a326..3a98646241 100644 --- a/libs/forms/feature/src/use-form-field.ts +++ b/libs/forms/feature/src/use-form-field.ts @@ -8,7 +8,7 @@ import { Dispatch, ReduxRootState } from "Core/__deprecated__/renderer/store" import { get } from "lodash" import { useCallback, useMemo } from "react" import { selectActiveApiDeviceId } from "generic-view/store" -import { selectForm, setFormField } from "forms/store" +import { resetForm, selectForm, setFormField } from "forms/store" interface SetOptions { customFormName?: string @@ -92,9 +92,24 @@ export const useFormField = ({ formName, appForm }: UseFormField = {}) => { [activeDeviceId, appForm, dispatch, formName, getField] ) + const resetAllFields = useCallback( + (customFormName?: string, appForm?: boolean) => { + const targetFormName = customFormName || formName + if (!targetFormName) return + dispatch( + resetForm({ + formName: targetFormName, + deviceId: appForm ? undefined : activeDeviceId, + }) + ) + }, + [activeDeviceId, dispatch, formName] + ) + return { getField, setField, resetField, + resetForm: resetAllFields, } } diff --git a/libs/forms/feature/src/use-form-register.ts b/libs/forms/feature/src/use-form-register.ts index 350a4ec24b..1be432bd5e 100644 --- a/libs/forms/feature/src/use-form-register.ts +++ b/libs/forms/feature/src/use-form-register.ts @@ -11,20 +11,22 @@ import { useEffect } from "react" interface Params { formName: string - appForm?: boolean options?: { + appForm?: boolean defaultFields?: Fields } } -export const useFormRegister = ({ formName, appForm, options }: Params) => { +export const useFormRegister = ({ formName, options }: Params) => { const dispatch = useDispatch() + + const isAppForm = options?.appForm const activeDeviceId = useSelector(selectActiveApiDeviceId) const form = useSelector((state: ReduxRootState) => { - if(!appForm && !activeDeviceId) return undefined + if (!isAppForm && !activeDeviceId) return undefined return selectForm(state, { formName, - deviceId: appForm ? undefined : activeDeviceId, + deviceId: isAppForm ? undefined : activeDeviceId, }) }) @@ -33,12 +35,13 @@ export const useFormRegister = ({ formName, appForm, options }: Params) => { const { defaultFields = {} } = options || {} dispatch( registerForm({ - deviceId: appForm ? undefined : activeDeviceId, + deviceId: isAppForm ? undefined : activeDeviceId, formName, form: { fields: defaultFields, + defaultFields, }, }) ) - }, [activeDeviceId, appForm, dispatch, form, formName, options]) + }, [activeDeviceId, isAppForm, dispatch, form, formName, options]) } diff --git a/libs/generic-view/ui/src/lib/interactive/form/form.tsx b/libs/generic-view/ui/src/lib/interactive/form/form.tsx index 661d6c154e..8e9b74414f 100644 --- a/libs/generic-view/ui/src/lib/interactive/form/form.tsx +++ b/libs/generic-view/ui/src/lib/interactive/form/form.tsx @@ -37,8 +37,8 @@ export const Form: BaseGenericComponent< useViewFormRegister(componentKey!, methods) useFormRegister({ formName: componentKey!, - appForm, options: { + appForm, defaultFields: config?.defaultFields, }, })