Skip to content

Commit

Permalink
Added form reset
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurczewski committed Oct 15, 2024
1 parent 6fd8e0b commit 2680ea4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
17 changes: 16 additions & 1 deletion libs/forms/feature/src/use-form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
}
15 changes: 9 additions & 6 deletions libs/forms/feature/src/use-form-register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dispatch>()

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,
})
})

Expand All @@ -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])
}
2 changes: 1 addition & 1 deletion libs/generic-view/ui/src/lib/interactive/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const Form: BaseGenericComponent<
useViewFormRegister(componentKey!, methods)
useFormRegister({
formName: componentKey!,
appForm,
options: {
appForm,
defaultFields: config?.defaultFields,
},
})
Expand Down

0 comments on commit 2680ea4

Please sign in to comment.