Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/components/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CSSProperties, useEffect, useRef, useState } from 'react';
import { EventEmitter, Form as FormClass, Webform } from '@formio/js';
import { EventEmitter, Form as FormClass, Webform, Utils } from '@formio/js';
import { Component, Form as CoreFormType } from '@formio/core';
import structuredClone from '@ungap/structured-clone';

Expand Down Expand Up @@ -59,7 +59,7 @@
onNextPage?: (page: number, submission: Submission) => void;
onCancelSubmit?: () => void;
onCancelComponent?: (component: Component) => void;
onChange?: (value: any, flags: any, modified: boolean) => void;

Check warning on line 62 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 62 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 62 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 62 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 62 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type

Check warning on line 62 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
onCustomEvent?: (event: {
type: string;
component: Component;
Expand All @@ -69,17 +69,17 @@
onComponentChange?: (changed: {
instance: Webform;
component: Component;
value: any;

Check warning on line 72 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 72 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 72 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
flags: any;

Check warning on line 73 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 73 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 73 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
}) => void;
onSubmit?: (submission: Submission, saved?: boolean) => void;
onSubmitDone?: (submission: Submission) => void;
onSubmitError?: (error: EventError) => void;
onFormLoad?: (form: JSON) => void;
onError?: (error: EventError | false) => void;
onRender?: (param: any) => void;

Check warning on line 80 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 80 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 80 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
onAttach?: (param: any) => void;

Check warning on line 81 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 81 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 81 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
onBuild?: (param: any) => void;

Check warning on line 82 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 82 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 82 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
onFocus?: (instance: Webform) => void;
onBlur?: (instance: Webform) => void;
onInitialized?: () => void;
Expand All @@ -91,7 +91,7 @@
onSubmissionDeleted?: (submission: Submission) => void;
onRequestDone?: () => void;
otherEvents?: {
[event: string]: (...args: any[]) => void;

Check warning on line 94 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 94 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 94 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
};
};

Expand All @@ -114,7 +114,7 @@
| 'formioform'
| 'Formio'
>,
...args: [string, ...any[]]

Check warning on line 117 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 117 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 117 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
) => {
const [event, ...outputArgs] = args;
if (event.startsWith('formio.')) {
Expand Down Expand Up @@ -228,9 +228,7 @@
if (!options?.events) {
options.events = getDefaultEmitter();
}
if (typeof formSource !== 'string') {
formSource = structuredClone(formSource);
}

const promise = FormConstructor
? new FormConstructor(element, formSource, options)
: new FormClass(element, formSource, options);
Expand All @@ -253,6 +251,7 @@

export const Form = (props: FormProps) => {
const renderElement = useRef<HTMLDivElement | null>(null);
const currentFormJson = useRef<FormType | null>(null);
const { formConstructor, formSource, formReadyCallback } = getEffectiveProps(props);
const {
src,
Expand All @@ -278,6 +277,14 @@
}, [formInstance]);

useEffect(() => {
if (
typeof formSource === 'object' &&
currentFormJson.current &&
Utils._.isEqual(currentFormJson.current, formSource)
) {
return;
}

const createInstance = async () => {
if (renderElement.current === null) {
console.warn('Form element not found');
Expand All @@ -288,10 +295,12 @@
console.warn('Form source not found');
return;
}

currentFormJson.current = formSource && typeof formSource !== 'string'
? structuredClone(formSource)
: null;
const instance = await createWebformInstance(
formConstructor,
formSource,
currentFormJson.current || formSource,
renderElement.current,
options,
);
Expand Down Expand Up @@ -327,17 +336,15 @@
]);

useEffect(() => {
let onAnyHandler = null;
if (formInstance && Object.keys(handlers).length > 0) {
formInstance.onAny((...args: [string, ...any[]]) =>
onAnyEvent(handlers, ...args),
);
onAnyHandler = (...args: [string, ...any[]]) => onAnyEvent(handlers, ...args);
formInstance.onAny(onAnyHandler);
}

Check warning on line 343 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 343 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / setup

Unexpected any. Specify a different type

Check warning on line 343 in src/components/Form.tsx

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type

return () => {
if (formInstance && Object.keys(handlers).length > 0) {
formInstance.offAny((...args: [string, ...any[]]) =>
onAnyEvent(handlers, ...args),
);
if (formInstance && onAnyHandler) {
formInstance.offAny(onAnyHandler);
}
};
}, [formInstance, handlers]);
Expand Down