diff --git a/packages/react-formio-container/src/hooks/useForm.hook.ts b/packages/react-formio-container/src/hooks/useForm.hook.ts index fbab9020..5a44c4b7 100644 --- a/packages/react-formio-container/src/hooks/useForm.hook.ts +++ b/packages/react-formio-container/src/hooks/useForm.hook.ts @@ -11,7 +11,8 @@ import { selectAuth, selectError, selectForm, - selectRoot + selectRoot, + oneOfIsActive } from "@tsed/react-formio"; import { push } from "connected-react-router"; import noop from "lodash/noop"; @@ -52,6 +53,7 @@ export function useForm(props: UseFormProps) { const auth: AuthState = useSelector(selectAuth); const error = useSelector((state) => selectError(type, state)); const form = useSelector((state) => selectForm(type, state)); + const isActive = useSelector(oneOfIsActive(type)); if (form && (!form.tags || !form.tags.length) && formAction === "create") { form.tags = [...(parameters?.query?.tags || [])]; @@ -183,6 +185,7 @@ export function useForm(props: UseFormProps) { return { ...props, + isActive, basePath, formId, formAction, diff --git a/packages/react-formio-container/src/hooks/useSubmission.hook.tsx b/packages/react-formio-container/src/hooks/useSubmission.hook.tsx index a4b89ec3..2f81d58b 100644 --- a/packages/react-formio-container/src/hooks/useSubmission.hook.tsx +++ b/packages/react-formio-container/src/hooks/useSubmission.hook.tsx @@ -2,16 +2,17 @@ import { deleteSubmission, getSubmission as getSubmissionAction, getSubmissions, + oneOfIsActive, receiveForm, refreshSubmissions, saveSubmission as saveSubmissionAction, selectAuth, selectError, selectForm, + selectIsActive, selectRoot, selectSubmission, - Submission, - selectIsActive + Submission } from "@tsed/react-formio"; import { push } from "connected-react-router"; import noop from "lodash/noop"; @@ -52,7 +53,9 @@ export function useSubmission(props: UseSubmissionProps) { selectSubmission(submissionType, state as any) ); - const isActive = useSelector((state) => selectIsActive(type, state)); + const isActive = useSelector(oneOfIsActive(submissionType, formType, type)); + + useSelector((state) => selectIsActive(submissionType, state)); const url = useSelector( (state) => diff --git a/packages/react-formio-container/src/views/form.view.tsx b/packages/react-formio-container/src/views/form.view.tsx index 9141b2bd..f9d177d6 100644 --- a/packages/react-formio-container/src/views/form.view.tsx +++ b/packages/react-formio-container/src/views/form.view.tsx @@ -1,4 +1,4 @@ -import { Alert, Tabs, RemoveModal } from "@tsed/react-formio"; +import { Alert, Tabs, RemoveModal, Loader } from "@tsed/react-formio"; import React from "react"; import { Route, Switch, useParams } from "react-router"; import { FormEditView } from "./formEdit.view"; @@ -16,10 +16,12 @@ function FormComponent({ className, ...props }: ReturnType) { setCurrentRoute, routes, gotoEdit, - i18n + i18n, + isActive } = props; const RemoveModalComponent = props.RemoveModalComponent || RemoveModal; + const LoaderComponent = props.LoaderComponent || Loader; return (
@@ -78,6 +80,7 @@ function FormComponent({ className, ...props }: ReturnType) { /> +
diff --git a/packages/react-formio-container/src/views/formSettings.view.tsx b/packages/react-formio-container/src/views/formSettings.view.tsx index f31a3e19..35739a80 100644 --- a/packages/react-formio-container/src/views/formSettings.view.tsx +++ b/packages/react-formio-container/src/views/formSettings.view.tsx @@ -7,7 +7,7 @@ export function FormSettingsView(props: ReturnType) { const { form, saveForm, i18n } = props; return ( -
+
) { return (
-

) { onClose={gotoEdit} /> )} + +

); } diff --git a/packages/react-formio/src/components/form-access/formAccess.component.tsx b/packages/react-formio/src/components/form-access/formAccess.component.tsx index 51979c2b..fc308c5c 100644 --- a/packages/react-formio/src/components/form-access/formAccess.component.tsx +++ b/packages/react-formio/src/components/form-access/formAccess.component.tsx @@ -3,17 +3,19 @@ import React, { PropsWithChildren, ReactElement, useCallback, + useEffect, useMemo, useState } from "react"; import { FormOptions, FormSchema, Submission } from "../../interfaces"; import { Card } from "../card/card.component"; -import { Form } from "../form/form.component"; +import { ChangedSubmission, Form } from "../form/form.component"; import { AccessRoles, dataAccessToSubmissions, FormAccessSchema, getFormAccess, + shouldUpdate, SubmissionAccess, submissionsToDataAccess, updateSubmissions @@ -46,6 +48,18 @@ function useFormAccess({ [submissions] ); + useEffect(() => { + const input = dataAccessToSubmissions(formDefinition, form); + if (formDefinition?._id) { + if ( + shouldUpdate("access", submissions.access, input) || + shouldUpdate("submissionAccess", submissions.submissionAccess, input) + ) { + setSubmissions(input); + } + } + }, [formDefinition?._id]); + return { options, form, @@ -80,18 +94,26 @@ function NamedFormAccess({ onSubmit, children }: PropsWithChildren) { + const [isValid, setIsValid] = useState(true); + return ( <>
) => { - onChange(name, submission); + onChange={({ data, isValid }: ChangedSubmission) => { + isValid && onChange(name, { data }); + setIsValid(isValid); }} options={options} /> - diff --git a/packages/react-formio/src/components/form-builder/formBuilder.component.tsx b/packages/react-formio/src/components/form-builder/formBuilder.component.tsx index d06b3ea1..e117a6d2 100755 --- a/packages/react-formio/src/components/form-builder/formBuilder.component.tsx +++ b/packages/react-formio/src/components/form-builder/formBuilder.component.tsx @@ -40,7 +40,7 @@ async function createBuilder( }; try { - const builder = await new FormioFormBuilder(el, form, options).ready; + const builder = await new FormioFormBuilder(el, form, { ...options }).ready; const handleEvent = (event: string) => { return (...args: any[]) => { diff --git a/packages/react-formio/src/components/form-settings/formSettings.component.spec.tsx b/packages/react-formio/src/components/form-settings/formSettings.component.spec.tsx index 378b1e94..6c725b9c 100644 --- a/packages/react-formio/src/components/form-settings/formSettings.component.spec.tsx +++ b/packages/react-formio/src/components/form-settings/formSettings.component.spec.tsx @@ -12,7 +12,13 @@ describe("FormSettings", () => { Sandbox.args.form.action = "https://test"; const { getByTestId } = render( - + f + " i18N" + }} + /> ); const btn = getByTestId("submit"); @@ -52,8 +58,6 @@ describe("FormSettings", () => { f + " i18N" }} /> diff --git a/packages/react-formio/src/components/form-settings/formSettings.component.tsx b/packages/react-formio/src/components/form-settings/formSettings.component.tsx index 5a651900..1b87962a 100644 --- a/packages/react-formio/src/components/form-settings/formSettings.component.tsx +++ b/packages/react-formio/src/components/form-settings/formSettings.component.tsx @@ -1,10 +1,10 @@ -import React, { useCallback, useState } from "react"; -import { FormOptions, FormSchema, Submission } from "../../interfaces"; +import React, { useCallback, useEffect, useState } from "react"; +import { FormOptions, FormSchema } from "../../interfaces"; import { getFormSettingsSchema } from "./formSettings.schema"; -import { Form } from "../form/form.component"; +import { ChangedSubmission, Form } from "../form/form.component"; import { - formSettingsToSubmission, FormSettingsSchema, + formSettingsToSubmission, submissionToFormSettings } from "./formSettings.utils"; import isEqual from "lodash/isEqual"; @@ -22,25 +22,36 @@ function useFormSettings({ options }: FormSettingsProps) { const form = getFormSettingsSchema(); + const [isValid, setIsValid] = useState(true); const [submission, setSubmission] = useState(() => formSettingsToSubmission(formDefinition) ); const onChange = useCallback( - (newSubmission: Submission) => { - if (!isEqual(submission.data, newSubmission.data)) { - setSubmission(newSubmission); + ({ data, isValid }: ChangedSubmission) => { + if (isValid) { + setSubmission({ data }); } + setIsValid(isValid); }, [submission] ); + useEffect(() => { + const input = formSettingsToSubmission(formDefinition); + if (formDefinition?._id && !isEqual(submission.data, input.data)) { + setSubmission(input); + } + }, [formDefinition?._id]); + return { options, form, type: formDefinition.type, submission, onChange, + isValid, + setIsValid, onSubmit: () => { onSubmit(submissionToFormSettings(formDefinition, submission)); } @@ -53,7 +64,8 @@ export function FormSettings(props: FormSettingsProps) { submission, options = {}, onChange, - onSubmit + onSubmit, + isValid } = useFormSettings(props); const i18n = options.i18n || ((f: string) => f); @@ -69,6 +81,7 @@ export function FormSettings(props: FormSettingsProps) {