@otl-core/forms 1.1.19 → 1.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/form-context.cjs +99 -0
- package/dist/form-context.cjs.map +1 -0
- package/dist/form-context.d.cts +55 -0
- package/dist/form-context.d.ts +55 -0
- package/dist/form-context.js +80 -0
- package/dist/form-context.js.map +1 -0
- package/dist/index.cjs +25 -943
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -84
- package/dist/index.d.ts +6 -84
- package/dist/index.js +3 -939
- package/dist/index.js.map +1 -1
- package/dist/use-form-action.cjs +281 -0
- package/dist/use-form-action.cjs.map +1 -0
- package/dist/use-form-action.d.cts +12 -0
- package/dist/use-form-action.d.ts +12 -0
- package/dist/use-form-action.js +264 -0
- package/dist/use-form-action.js.map +1 -0
- package/dist/use-form-field.cjs +181 -0
- package/dist/use-form-field.cjs.map +1 -0
- package/dist/use-form-field.d.cts +19 -0
- package/dist/use-form-field.d.ts +19 -0
- package/dist/use-form-field.js +160 -0
- package/dist/use-form-field.js.map +1 -0
- package/dist/utils/page.utils.cjs +76 -0
- package/dist/utils/page.utils.cjs.map +1 -0
- package/dist/utils/page.utils.d.cts +7 -0
- package/dist/utils/page.utils.d.ts +7 -0
- package/dist/utils/page.utils.js +50 -0
- package/dist/utils/page.utils.js.map +1 -0
- package/dist/utils/rule.utils.cjs +94 -0
- package/dist/utils/rule.utils.cjs.map +1 -0
- package/dist/utils/rule.utils.d.cts +6 -0
- package/dist/utils/rule.utils.d.ts +6 -0
- package/dist/utils/rule.utils.js +69 -0
- package/dist/utils/rule.utils.js.map +1 -0
- package/dist/utils/validation.utils.cjs +391 -0
- package/dist/utils/validation.utils.cjs.map +1 -0
- package/dist/utils/validation.utils.d.cts +15 -0
- package/dist/utils/validation.utils.d.ts +15 -0
- package/dist/utils/validation.utils.js +366 -0
- package/dist/utils/validation.utils.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var form_context_exports = {};
|
|
21
|
+
__export(form_context_exports, {
|
|
22
|
+
FormProvider: () => FormProvider,
|
|
23
|
+
useForm: () => useForm
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(form_context_exports);
|
|
26
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
27
|
+
var import_react = require("react");
|
|
28
|
+
const FormContext = (0, import_react.createContext)(void 0);
|
|
29
|
+
function useForm() {
|
|
30
|
+
const context = (0, import_react.useContext)(FormContext);
|
|
31
|
+
if (!context) {
|
|
32
|
+
throw new Error("useForm must be used within a FormProvider");
|
|
33
|
+
}
|
|
34
|
+
return context;
|
|
35
|
+
}
|
|
36
|
+
function FormProvider({
|
|
37
|
+
children,
|
|
38
|
+
formId,
|
|
39
|
+
document,
|
|
40
|
+
settings,
|
|
41
|
+
analyticsSettings,
|
|
42
|
+
onAnalyticsEvent,
|
|
43
|
+
formName,
|
|
44
|
+
environmentVariantId,
|
|
45
|
+
formVariantId,
|
|
46
|
+
locale
|
|
47
|
+
}) {
|
|
48
|
+
const [currentPageId, setCurrentPageId] = (0, import_react.useState)(
|
|
49
|
+
document.pages[0]?.id ?? ""
|
|
50
|
+
);
|
|
51
|
+
const [formValues, setFormValues] = (0, import_react.useState)({});
|
|
52
|
+
const [loading, setLoading] = (0, import_react.useState)(false);
|
|
53
|
+
const [errors, setErrors] = (0, import_react.useState)(null);
|
|
54
|
+
const [globalError, setGlobalError] = (0, import_react.useState)(null);
|
|
55
|
+
const [hasStarted, setHasStarted] = (0, import_react.useState)(false);
|
|
56
|
+
const currentPage = (0, import_react.useMemo)(
|
|
57
|
+
() => document.pages.find((p) => p.id === currentPageId) ?? document.pages[0],
|
|
58
|
+
[document, currentPageId]
|
|
59
|
+
);
|
|
60
|
+
const setCurrentPage = (0, import_react.useCallback)(
|
|
61
|
+
(page) => setCurrentPageId(page.id),
|
|
62
|
+
[]
|
|
63
|
+
);
|
|
64
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
65
|
+
FormContext.Provider,
|
|
66
|
+
{
|
|
67
|
+
value: {
|
|
68
|
+
formId,
|
|
69
|
+
document,
|
|
70
|
+
settings,
|
|
71
|
+
loading,
|
|
72
|
+
setLoading,
|
|
73
|
+
currentPage,
|
|
74
|
+
setCurrentPage,
|
|
75
|
+
formValues,
|
|
76
|
+
setFormValues,
|
|
77
|
+
errors,
|
|
78
|
+
setErrors,
|
|
79
|
+
globalError,
|
|
80
|
+
setGlobalError,
|
|
81
|
+
analyticsSettings,
|
|
82
|
+
onAnalyticsEvent,
|
|
83
|
+
formName,
|
|
84
|
+
hasStarted,
|
|
85
|
+
setHasStarted,
|
|
86
|
+
environmentVariantId,
|
|
87
|
+
formVariantId,
|
|
88
|
+
locale
|
|
89
|
+
},
|
|
90
|
+
children
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
95
|
+
0 && (module.exports = {
|
|
96
|
+
FormProvider,
|
|
97
|
+
useForm
|
|
98
|
+
});
|
|
99
|
+
//# sourceMappingURL=form-context.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/form-context.tsx"],"sourcesContent":["\"use client\";\n\nimport type {\n FormAnalyticsCallback,\n FormAnalyticsSettings,\n FormDocument,\n FormPage,\n FormSettings,\n} from \"@otl-core/cms-types\";\nimport React, {\n createContext,\n useCallback,\n useContext,\n useMemo,\n useState,\n} from \"react\";\n\ninterface FormContextValue {\n /** The actual form ID (from the form definition / PostgreSQL) */\n formId: string;\n document: FormDocument;\n settings: FormSettings;\n\n loading: boolean;\n setLoading: React.Dispatch<React.SetStateAction<boolean>>;\n\n currentPage: FormPage;\n setCurrentPage: (page: FormPage) => void;\n\n formValues: Record<string, unknown>;\n setFormValues: React.Dispatch<React.SetStateAction<Record<string, unknown>>>;\n\n errors: { [key: string]: string } | null;\n setErrors: React.Dispatch<\n React.SetStateAction<{ [key: string]: string } | null>\n >;\n globalError: string | null;\n setGlobalError: React.Dispatch<React.SetStateAction<string | null>>;\n\n analyticsSettings?: FormAnalyticsSettings;\n onAnalyticsEvent?: FormAnalyticsCallback;\n formName?: string;\n hasStarted: boolean;\n setHasStarted: React.Dispatch<React.SetStateAction<boolean>>;\n /** A/B test: which page variant the form is rendered on */\n environmentVariantId?: string;\n /** A/B test: which form variant was resolved (for multivariate forms) */\n formVariantId?: string;\n /** Locale for the form submission */\n locale?: string;\n}\n\nconst FormContext = createContext<FormContextValue | undefined>(undefined);\n\nexport function useForm(): FormContextValue {\n const context = useContext(FormContext);\n if (!context) {\n throw new Error(\"useForm must be used within a FormProvider\");\n }\n return context;\n}\n\nexport interface FormProviderProps {\n children: React.ReactNode;\n /** The actual form ID (from the form definition / PostgreSQL) */\n formId: string;\n document: FormDocument;\n settings: FormSettings;\n analyticsSettings?: FormAnalyticsSettings;\n onAnalyticsEvent?: FormAnalyticsCallback;\n formName?: string;\n /** A/B test: which page variant the form is rendered on */\n environmentVariantId?: string;\n /** A/B test: which form variant was resolved (for multivariate forms) */\n formVariantId?: string;\n /** Locale for the form submission */\n locale?: string;\n}\n\nexport function FormProvider({\n children,\n formId,\n document,\n settings,\n analyticsSettings,\n onAnalyticsEvent,\n formName,\n environmentVariantId,\n formVariantId,\n locale,\n}: FormProviderProps) {\n const [currentPageId, setCurrentPageId] = useState<string>(\n document.pages[0]?.id ?? \"\",\n );\n const [formValues, setFormValues] = useState<Record<string, unknown>>({});\n\n const [loading, setLoading] = useState(false);\n const [errors, setErrors] = useState<{ [key: string]: string } | null>(null);\n const [globalError, setGlobalError] = useState<string | null>(null);\n const [hasStarted, setHasStarted] = useState(false);\n\n // Derive current page from document prop + stored page ID\n const currentPage = useMemo(\n () =>\n document.pages.find((p) => p.id === currentPageId) ?? document.pages[0],\n [document, currentPageId],\n );\n\n const setCurrentPage = useCallback(\n (page: FormPage) => setCurrentPageId(page.id),\n [],\n );\n\n return (\n <FormContext.Provider\n value={{\n formId,\n document,\n settings,\n\n loading,\n setLoading,\n\n currentPage,\n setCurrentPage,\n\n formValues,\n setFormValues,\n\n errors,\n setErrors,\n\n globalError,\n setGlobalError,\n\n analyticsSettings,\n onAnalyticsEvent,\n formName,\n hasStarted,\n setHasStarted,\n environmentVariantId,\n formVariantId,\n locale,\n }}\n >\n {children}\n </FormContext.Provider>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkHI;AAzGJ,mBAMO;AAqCP,MAAM,kBAAc,4BAA4C,MAAS;AAElE,SAAS,UAA4B;AAC1C,QAAM,cAAU,yBAAW,WAAW;AACtC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AACA,SAAO;AACT;AAmBO,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAsB;AACpB,QAAM,CAAC,eAAe,gBAAgB,QAAI;AAAA,IACxC,SAAS,MAAM,CAAC,GAAG,MAAM;AAAA,EAC3B;AACA,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAkC,CAAC,CAAC;AAExE,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAS,KAAK;AAC5C,QAAM,CAAC,QAAQ,SAAS,QAAI,uBAA2C,IAAI;AAC3E,QAAM,CAAC,aAAa,cAAc,QAAI,uBAAwB,IAAI;AAClE,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAS,KAAK;AAGlD,QAAM,kBAAc;AAAA,IAClB,MACE,SAAS,MAAM,KAAK,CAAC,MAAM,EAAE,OAAO,aAAa,KAAK,SAAS,MAAM,CAAC;AAAA,IACxE,CAAC,UAAU,aAAa;AAAA,EAC1B;AAEA,QAAM,qBAAiB;AAAA,IACrB,CAAC,SAAmB,iBAAiB,KAAK,EAAE;AAAA,IAC5C,CAAC;AAAA,EACH;AAEA,SACE;AAAA,IAAC,YAAY;AAAA,IAAZ;AAAA,MACC,OAAO;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,MAEC;AAAA;AAAA,EACH;AAEJ;","names":[]}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { FormDocument, FormSettings, FormAnalyticsSettings, FormAnalyticsCallback, FormPage } from '@otl-core/cms-types';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
|
|
5
|
+
interface FormContextValue {
|
|
6
|
+
/** The actual form ID (from the form definition / PostgreSQL) */
|
|
7
|
+
formId: string;
|
|
8
|
+
document: FormDocument;
|
|
9
|
+
settings: FormSettings;
|
|
10
|
+
loading: boolean;
|
|
11
|
+
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
|
|
12
|
+
currentPage: FormPage;
|
|
13
|
+
setCurrentPage: (page: FormPage) => void;
|
|
14
|
+
formValues: Record<string, unknown>;
|
|
15
|
+
setFormValues: React.Dispatch<React.SetStateAction<Record<string, unknown>>>;
|
|
16
|
+
errors: {
|
|
17
|
+
[key: string]: string;
|
|
18
|
+
} | null;
|
|
19
|
+
setErrors: React.Dispatch<React.SetStateAction<{
|
|
20
|
+
[key: string]: string;
|
|
21
|
+
} | null>>;
|
|
22
|
+
globalError: string | null;
|
|
23
|
+
setGlobalError: React.Dispatch<React.SetStateAction<string | null>>;
|
|
24
|
+
analyticsSettings?: FormAnalyticsSettings;
|
|
25
|
+
onAnalyticsEvent?: FormAnalyticsCallback;
|
|
26
|
+
formName?: string;
|
|
27
|
+
hasStarted: boolean;
|
|
28
|
+
setHasStarted: React.Dispatch<React.SetStateAction<boolean>>;
|
|
29
|
+
/** A/B test: which page variant the form is rendered on */
|
|
30
|
+
environmentVariantId?: string;
|
|
31
|
+
/** A/B test: which form variant was resolved (for multivariate forms) */
|
|
32
|
+
formVariantId?: string;
|
|
33
|
+
/** Locale for the form submission */
|
|
34
|
+
locale?: string;
|
|
35
|
+
}
|
|
36
|
+
declare function useForm(): FormContextValue;
|
|
37
|
+
interface FormProviderProps {
|
|
38
|
+
children: React.ReactNode;
|
|
39
|
+
/** The actual form ID (from the form definition / PostgreSQL) */
|
|
40
|
+
formId: string;
|
|
41
|
+
document: FormDocument;
|
|
42
|
+
settings: FormSettings;
|
|
43
|
+
analyticsSettings?: FormAnalyticsSettings;
|
|
44
|
+
onAnalyticsEvent?: FormAnalyticsCallback;
|
|
45
|
+
formName?: string;
|
|
46
|
+
/** A/B test: which page variant the form is rendered on */
|
|
47
|
+
environmentVariantId?: string;
|
|
48
|
+
/** A/B test: which form variant was resolved (for multivariate forms) */
|
|
49
|
+
formVariantId?: string;
|
|
50
|
+
/** Locale for the form submission */
|
|
51
|
+
locale?: string;
|
|
52
|
+
}
|
|
53
|
+
declare function FormProvider({ children, formId, document, settings, analyticsSettings, onAnalyticsEvent, formName, environmentVariantId, formVariantId, locale, }: FormProviderProps): react_jsx_runtime.JSX.Element;
|
|
54
|
+
|
|
55
|
+
export { FormProvider, type FormProviderProps, useForm };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { FormDocument, FormSettings, FormAnalyticsSettings, FormAnalyticsCallback, FormPage } from '@otl-core/cms-types';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
|
|
5
|
+
interface FormContextValue {
|
|
6
|
+
/** The actual form ID (from the form definition / PostgreSQL) */
|
|
7
|
+
formId: string;
|
|
8
|
+
document: FormDocument;
|
|
9
|
+
settings: FormSettings;
|
|
10
|
+
loading: boolean;
|
|
11
|
+
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
|
|
12
|
+
currentPage: FormPage;
|
|
13
|
+
setCurrentPage: (page: FormPage) => void;
|
|
14
|
+
formValues: Record<string, unknown>;
|
|
15
|
+
setFormValues: React.Dispatch<React.SetStateAction<Record<string, unknown>>>;
|
|
16
|
+
errors: {
|
|
17
|
+
[key: string]: string;
|
|
18
|
+
} | null;
|
|
19
|
+
setErrors: React.Dispatch<React.SetStateAction<{
|
|
20
|
+
[key: string]: string;
|
|
21
|
+
} | null>>;
|
|
22
|
+
globalError: string | null;
|
|
23
|
+
setGlobalError: React.Dispatch<React.SetStateAction<string | null>>;
|
|
24
|
+
analyticsSettings?: FormAnalyticsSettings;
|
|
25
|
+
onAnalyticsEvent?: FormAnalyticsCallback;
|
|
26
|
+
formName?: string;
|
|
27
|
+
hasStarted: boolean;
|
|
28
|
+
setHasStarted: React.Dispatch<React.SetStateAction<boolean>>;
|
|
29
|
+
/** A/B test: which page variant the form is rendered on */
|
|
30
|
+
environmentVariantId?: string;
|
|
31
|
+
/** A/B test: which form variant was resolved (for multivariate forms) */
|
|
32
|
+
formVariantId?: string;
|
|
33
|
+
/** Locale for the form submission */
|
|
34
|
+
locale?: string;
|
|
35
|
+
}
|
|
36
|
+
declare function useForm(): FormContextValue;
|
|
37
|
+
interface FormProviderProps {
|
|
38
|
+
children: React.ReactNode;
|
|
39
|
+
/** The actual form ID (from the form definition / PostgreSQL) */
|
|
40
|
+
formId: string;
|
|
41
|
+
document: FormDocument;
|
|
42
|
+
settings: FormSettings;
|
|
43
|
+
analyticsSettings?: FormAnalyticsSettings;
|
|
44
|
+
onAnalyticsEvent?: FormAnalyticsCallback;
|
|
45
|
+
formName?: string;
|
|
46
|
+
/** A/B test: which page variant the form is rendered on */
|
|
47
|
+
environmentVariantId?: string;
|
|
48
|
+
/** A/B test: which form variant was resolved (for multivariate forms) */
|
|
49
|
+
formVariantId?: string;
|
|
50
|
+
/** Locale for the form submission */
|
|
51
|
+
locale?: string;
|
|
52
|
+
}
|
|
53
|
+
declare function FormProvider({ children, formId, document, settings, analyticsSettings, onAnalyticsEvent, formName, environmentVariantId, formVariantId, locale, }: FormProviderProps): react_jsx_runtime.JSX.Element;
|
|
54
|
+
|
|
55
|
+
export { FormProvider, type FormProviderProps, useForm };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useMemo,
|
|
8
|
+
useState
|
|
9
|
+
} from "react";
|
|
10
|
+
const FormContext = createContext(void 0);
|
|
11
|
+
function useForm() {
|
|
12
|
+
const context = useContext(FormContext);
|
|
13
|
+
if (!context) {
|
|
14
|
+
throw new Error("useForm must be used within a FormProvider");
|
|
15
|
+
}
|
|
16
|
+
return context;
|
|
17
|
+
}
|
|
18
|
+
function FormProvider({
|
|
19
|
+
children,
|
|
20
|
+
formId,
|
|
21
|
+
document,
|
|
22
|
+
settings,
|
|
23
|
+
analyticsSettings,
|
|
24
|
+
onAnalyticsEvent,
|
|
25
|
+
formName,
|
|
26
|
+
environmentVariantId,
|
|
27
|
+
formVariantId,
|
|
28
|
+
locale
|
|
29
|
+
}) {
|
|
30
|
+
const [currentPageId, setCurrentPageId] = useState(
|
|
31
|
+
document.pages[0]?.id ?? ""
|
|
32
|
+
);
|
|
33
|
+
const [formValues, setFormValues] = useState({});
|
|
34
|
+
const [loading, setLoading] = useState(false);
|
|
35
|
+
const [errors, setErrors] = useState(null);
|
|
36
|
+
const [globalError, setGlobalError] = useState(null);
|
|
37
|
+
const [hasStarted, setHasStarted] = useState(false);
|
|
38
|
+
const currentPage = useMemo(
|
|
39
|
+
() => document.pages.find((p) => p.id === currentPageId) ?? document.pages[0],
|
|
40
|
+
[document, currentPageId]
|
|
41
|
+
);
|
|
42
|
+
const setCurrentPage = useCallback(
|
|
43
|
+
(page) => setCurrentPageId(page.id),
|
|
44
|
+
[]
|
|
45
|
+
);
|
|
46
|
+
return /* @__PURE__ */ jsx(
|
|
47
|
+
FormContext.Provider,
|
|
48
|
+
{
|
|
49
|
+
value: {
|
|
50
|
+
formId,
|
|
51
|
+
document,
|
|
52
|
+
settings,
|
|
53
|
+
loading,
|
|
54
|
+
setLoading,
|
|
55
|
+
currentPage,
|
|
56
|
+
setCurrentPage,
|
|
57
|
+
formValues,
|
|
58
|
+
setFormValues,
|
|
59
|
+
errors,
|
|
60
|
+
setErrors,
|
|
61
|
+
globalError,
|
|
62
|
+
setGlobalError,
|
|
63
|
+
analyticsSettings,
|
|
64
|
+
onAnalyticsEvent,
|
|
65
|
+
formName,
|
|
66
|
+
hasStarted,
|
|
67
|
+
setHasStarted,
|
|
68
|
+
environmentVariantId,
|
|
69
|
+
formVariantId,
|
|
70
|
+
locale
|
|
71
|
+
},
|
|
72
|
+
children
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
FormProvider,
|
|
78
|
+
useForm
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=form-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/form-context.tsx"],"sourcesContent":["\"use client\";\n\nimport type {\n FormAnalyticsCallback,\n FormAnalyticsSettings,\n FormDocument,\n FormPage,\n FormSettings,\n} from \"@otl-core/cms-types\";\nimport React, {\n createContext,\n useCallback,\n useContext,\n useMemo,\n useState,\n} from \"react\";\n\ninterface FormContextValue {\n /** The actual form ID (from the form definition / PostgreSQL) */\n formId: string;\n document: FormDocument;\n settings: FormSettings;\n\n loading: boolean;\n setLoading: React.Dispatch<React.SetStateAction<boolean>>;\n\n currentPage: FormPage;\n setCurrentPage: (page: FormPage) => void;\n\n formValues: Record<string, unknown>;\n setFormValues: React.Dispatch<React.SetStateAction<Record<string, unknown>>>;\n\n errors: { [key: string]: string } | null;\n setErrors: React.Dispatch<\n React.SetStateAction<{ [key: string]: string } | null>\n >;\n globalError: string | null;\n setGlobalError: React.Dispatch<React.SetStateAction<string | null>>;\n\n analyticsSettings?: FormAnalyticsSettings;\n onAnalyticsEvent?: FormAnalyticsCallback;\n formName?: string;\n hasStarted: boolean;\n setHasStarted: React.Dispatch<React.SetStateAction<boolean>>;\n /** A/B test: which page variant the form is rendered on */\n environmentVariantId?: string;\n /** A/B test: which form variant was resolved (for multivariate forms) */\n formVariantId?: string;\n /** Locale for the form submission */\n locale?: string;\n}\n\nconst FormContext = createContext<FormContextValue | undefined>(undefined);\n\nexport function useForm(): FormContextValue {\n const context = useContext(FormContext);\n if (!context) {\n throw new Error(\"useForm must be used within a FormProvider\");\n }\n return context;\n}\n\nexport interface FormProviderProps {\n children: React.ReactNode;\n /** The actual form ID (from the form definition / PostgreSQL) */\n formId: string;\n document: FormDocument;\n settings: FormSettings;\n analyticsSettings?: FormAnalyticsSettings;\n onAnalyticsEvent?: FormAnalyticsCallback;\n formName?: string;\n /** A/B test: which page variant the form is rendered on */\n environmentVariantId?: string;\n /** A/B test: which form variant was resolved (for multivariate forms) */\n formVariantId?: string;\n /** Locale for the form submission */\n locale?: string;\n}\n\nexport function FormProvider({\n children,\n formId,\n document,\n settings,\n analyticsSettings,\n onAnalyticsEvent,\n formName,\n environmentVariantId,\n formVariantId,\n locale,\n}: FormProviderProps) {\n const [currentPageId, setCurrentPageId] = useState<string>(\n document.pages[0]?.id ?? \"\",\n );\n const [formValues, setFormValues] = useState<Record<string, unknown>>({});\n\n const [loading, setLoading] = useState(false);\n const [errors, setErrors] = useState<{ [key: string]: string } | null>(null);\n const [globalError, setGlobalError] = useState<string | null>(null);\n const [hasStarted, setHasStarted] = useState(false);\n\n // Derive current page from document prop + stored page ID\n const currentPage = useMemo(\n () =>\n document.pages.find((p) => p.id === currentPageId) ?? document.pages[0],\n [document, currentPageId],\n );\n\n const setCurrentPage = useCallback(\n (page: FormPage) => setCurrentPageId(page.id),\n [],\n );\n\n return (\n <FormContext.Provider\n value={{\n formId,\n document,\n settings,\n\n loading,\n setLoading,\n\n currentPage,\n setCurrentPage,\n\n formValues,\n setFormValues,\n\n errors,\n setErrors,\n\n globalError,\n setGlobalError,\n\n analyticsSettings,\n onAnalyticsEvent,\n formName,\n hasStarted,\n setHasStarted,\n environmentVariantId,\n formVariantId,\n locale,\n }}\n >\n {children}\n </FormContext.Provider>\n );\n}\n"],"mappings":";AAkHI;AAzGJ;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAqCP,MAAM,cAAc,cAA4C,MAAS;AAElE,SAAS,UAA4B;AAC1C,QAAM,UAAU,WAAW,WAAW;AACtC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AACA,SAAO;AACT;AAmBO,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAsB;AACpB,QAAM,CAAC,eAAe,gBAAgB,IAAI;AAAA,IACxC,SAAS,MAAM,CAAC,GAAG,MAAM;AAAA,EAC3B;AACA,QAAM,CAAC,YAAY,aAAa,IAAI,SAAkC,CAAC,CAAC;AAExE,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,KAAK;AAC5C,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA2C,IAAI;AAC3E,QAAM,CAAC,aAAa,cAAc,IAAI,SAAwB,IAAI;AAClE,QAAM,CAAC,YAAY,aAAa,IAAI,SAAS,KAAK;AAGlD,QAAM,cAAc;AAAA,IAClB,MACE,SAAS,MAAM,KAAK,CAAC,MAAM,EAAE,OAAO,aAAa,KAAK,SAAS,MAAM,CAAC;AAAA,IACxE,CAAC,UAAU,aAAa;AAAA,EAC1B;AAEA,QAAM,iBAAiB;AAAA,IACrB,CAAC,SAAmB,iBAAiB,KAAK,EAAE;AAAA,IAC5C,CAAC;AAAA,EACH;AAEA,SACE;AAAA,IAAC,YAAY;AAAA,IAAZ;AAAA,MACC,OAAO;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QAEA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,MAEC;AAAA;AAAA,EACH;AAEJ;","names":[]}
|