@encatch/ws-react 0.0.50-beta.0 → 0.0.50-beta.3

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/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # @encatch/ws-react
2
2
 
3
+
3
4
  React wrapper for Encatch web form engine. This package provides React components and hooks to easily integrate Encatch forms into your React applications with full event handling capabilities.
4
5
 
5
6
  ## Purpose
@@ -1,6 +1,6 @@
1
1
  import { AppProps, Section } from "@encatch/schema";
2
2
  import React from "react";
3
- import type { OnFormEventHandler } from "./types";
3
+ import type { EncatchFormConfig, OnFormEventHandler } from "./types";
4
4
  declare module "react" {
5
5
  namespace JSX {
6
6
  interface IntrinsicElements {
@@ -30,26 +30,34 @@ declare module "react" {
30
30
  }
31
31
  }
32
32
  }
33
+ interface EncatchPreviewFormData {
34
+ theme: AppProps["theme"];
35
+ currentMode: "light" | "dark";
36
+ currentLanguage: AppProps["currentLanguage"];
37
+ questions: AppProps["questions"];
38
+ questionLanguages: AppProps["questionLanguages"];
39
+ otherConfigurationProperties: AppProps["otherConfigurationProperties"];
40
+ welcomeScreenProperties: AppProps["welcomeScreenProperties"];
41
+ endScreenProperties: AppProps["endScreenProperties"];
42
+ translations: AppProps["translations"];
43
+ sections: Section[];
44
+ themeSettings: AppProps["themeSettings"] | null;
45
+ onClose: () => void;
46
+ onSectionChange: (index: number) => void;
47
+ apiKey: string;
48
+ hostUrl: string;
49
+ feedbackConfigId: string;
50
+ identifier: string;
51
+ }
33
52
  interface EncatchPreviewProps {
34
- formData: {
35
- theme: AppProps["theme"];
36
- currentMode: "light" | "dark";
37
- currentLanguage: AppProps["currentLanguage"];
38
- questions: AppProps["questions"];
39
- questionLanguages: AppProps["questionLanguages"];
40
- otherConfigurationProperties: AppProps["otherConfigurationProperties"];
41
- welcomeScreenProperties: AppProps["welcomeScreenProperties"];
42
- endScreenProperties: AppProps["endScreenProperties"];
43
- translations: AppProps["translations"];
44
- sections: Section[];
45
- themeSettings: AppProps["themeSettings"] | null;
46
- onClose: () => void;
47
- onSectionChange: (index: number) => void;
48
- apiKey: string;
49
- hostUrl: string;
50
- feedbackConfigId: string;
51
- identifier: string;
52
- };
53
+ /** Required for inline mode (same-document rendering). Omit when using formConfig + formPageUrl. */
54
+ formData?: EncatchPreviewFormData;
55
+ /** Full form config for iframe mode. Use with formPageUrl. */
56
+ formConfig?: EncatchFormConfig;
57
+ /** URL of the iframe form page (e.g. shareable origin + /encatch-preview-form). Use with formConfig. */
58
+ formPageUrl?: string;
59
+ /** Optional form id for iframe URL query and message payloads. Defaults to formConfig.formId or "preview". */
60
+ formId?: string;
53
61
  cssLink?: string;
54
62
  scale?: number;
55
63
  persistMode?: "retain" | "reset" | "none";
@@ -0,0 +1,32 @@
1
+ /**
2
+ * PostMessage types for EncatchPreview iframe communication.
3
+ * Keeps message type strings and payload shapes consistent between
4
+ * EncatchPreview (parent) and the iframe form page.
5
+ */
6
+ /** Message types sent from parent (EncatchPreview) to iframe */
7
+ export type SDKMessageType = "sdk:formConfig" | "sdk:theme" | "sdk:locale" | "sdk:resetData" | "sdk:prefillResponses";
8
+ /** Message types sent from iframe to parent */
9
+ export type FormMessageType = "form:ready" | "form:submit" | "form:complete" | "form:close" | "form:error" | "form:resize" | "form:closeButton" | "form:themeData";
10
+ /** Event-publisher messages (prefixed with encatch:) from iframe to parent */
11
+ export type EncatchFormEventType = "encatch:form:show" | "encatch:form:started" | "encatch:form:section:change" | "encatch:form:question:answered";
12
+ export interface SDKMessage {
13
+ type: SDKMessageType;
14
+ data?: Record<string, unknown>;
15
+ }
16
+ export interface FormMessage {
17
+ type: FormMessageType | EncatchFormEventType;
18
+ formId?: string;
19
+ data?: Record<string, unknown>;
20
+ payload?: Record<string, unknown>;
21
+ }
22
+ /** Normalize encatch:* message type to form:* for handler switch */
23
+ export declare function normalizeMessageType(type: string, data: {
24
+ type: string;
25
+ formId?: string;
26
+ data?: Record<string, unknown>;
27
+ payload?: Record<string, unknown>;
28
+ }): {
29
+ messageType: string;
30
+ formId: string | undefined;
31
+ payload: Record<string, unknown> | undefined;
32
+ };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { EncatchPreview } from "./EncatchPreview";
2
2
  export { useEncatchFormEvent, useEncatchFormEventAll } from "./useEncatchFormEvent";
3
3
  export type { FormEventType, FormEventPayload, SubscriptionOptions, FormIdFilter, } from "./useEncatchFormEvent";
4
- export type { FormEventBuilder, OnFormEventHandler, OnSubmitCallback, OnShowCallback, OnCloseCallback, OnSectionChangeCallback, OnQuestionAnsweredCallback, OnErrorCallback, } from "./types";
4
+ export type { EncatchFormConfig, EncatchPreviewIframeModeProps, FormEventBuilder, OnFormEventHandler, OnSubmitCallback, OnShowCallback, OnCloseCallback, OnSectionChangeCallback, OnQuestionAnsweredCallback, OnErrorCallback, } from "./types";
package/dist/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
- import I, { useState as U, useRef as oe, useEffect as S, useCallback as se } from "react";
2
- var A = { exports: {} }, P = {};
1
+ import U, { useState as ee, useRef as B, useEffect as y, useCallback as re } from "react";
2
+ var $ = { exports: {} }, M = {};
3
3
  /**
4
4
  * @license React
5
5
  * react-jsx-runtime.production.js
@@ -9,29 +9,29 @@ var A = { exports: {} }, P = {};
9
9
  * This source code is licensed under the MIT license found in the
10
10
  * LICENSE file in the root directory of this source tree.
11
11
  */
12
- var J;
12
+ var te;
13
13
  function ie() {
14
- if (J) return P;
15
- J = 1;
14
+ if (te) return M;
15
+ te = 1;
16
16
  var o = Symbol.for("react.transitional.element"), r = Symbol.for("react.fragment");
17
- function t(u, s, c) {
18
- var m = null;
19
- if (c !== void 0 && (m = "" + c), s.key !== void 0 && (m = "" + s.key), "key" in s) {
20
- c = {};
21
- for (var p in s)
22
- p !== "key" && (c[p] = s[p]);
23
- } else c = s;
24
- return s = c.ref, {
17
+ function n(u, c, f) {
18
+ var w = null;
19
+ if (f !== void 0 && (w = "" + f), c.key !== void 0 && (w = "" + c.key), "key" in c) {
20
+ f = {};
21
+ for (var d in c)
22
+ d !== "key" && (f[d] = c[d]);
23
+ } else f = c;
24
+ return c = f.ref, {
25
25
  $$typeof: o,
26
26
  type: u,
27
- key: m,
28
- ref: s !== void 0 ? s : null,
29
- props: c
27
+ key: w,
28
+ ref: c !== void 0 ? c : null,
29
+ props: f
30
30
  };
31
31
  }
32
- return P.Fragment = r, P.jsx = t, P.jsxs = t, P;
32
+ return M.Fragment = r, M.jsx = n, M.jsxs = n, M;
33
33
  }
34
- var k = {};
34
+ var F = {};
35
35
  /**
36
36
  * @license React
37
37
  * react-jsx-runtime.development.js
@@ -41,47 +41,47 @@ var k = {};
41
41
  * This source code is licensed under the MIT license found in the
42
42
  * LICENSE file in the root directory of this source tree.
43
43
  */
44
- var V;
45
- function ue() {
46
- return V || (V = 1, process.env.NODE_ENV !== "production" && (function() {
44
+ var ne;
45
+ function ae() {
46
+ return ne || (ne = 1, process.env.NODE_ENV !== "production" && (function() {
47
47
  function o(e) {
48
48
  if (e == null) return null;
49
49
  if (typeof e == "function")
50
- return e.$$typeof === re ? null : e.displayName || e.name || null;
50
+ return e.$$typeof === V ? null : e.displayName || e.name || null;
51
51
  if (typeof e == "string") return e;
52
52
  switch (e) {
53
- case f:
53
+ case g:
54
54
  return "Fragment";
55
- case Q:
55
+ case k:
56
56
  return "Profiler";
57
- case G:
57
+ case A:
58
58
  return "StrictMode";
59
- case Z:
59
+ case L:
60
60
  return "Suspense";
61
- case K:
61
+ case Y:
62
62
  return "SuspenseList";
63
- case ee:
63
+ case W:
64
64
  return "Activity";
65
65
  }
66
66
  if (typeof e == "object")
67
67
  switch (typeof e.tag == "number" && console.error(
68
68
  "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
69
69
  ), e.$$typeof) {
70
- case i:
70
+ case J:
71
71
  return "Portal";
72
- case B:
72
+ case N:
73
73
  return (e.displayName || "Context") + ".Provider";
74
- case X:
74
+ case j:
75
75
  return (e._context.displayName || "Context") + ".Consumer";
76
- case H:
77
- var n = e.render;
78
- return e = e.displayName, e || (e = n.displayName || n.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
79
- case D:
80
- return n = e.displayName || null, n !== null ? n : o(e.type) || "Memo";
81
- case M:
82
- n = e._payload, e = e._init;
76
+ case i:
77
+ var s = e.render;
78
+ return e = e.displayName, e || (e = s.displayName || s.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
79
+ case t:
80
+ return s = e.displayName || null, s !== null ? s : o(e.type) || "Memo";
81
+ case b:
82
+ s = e._payload, e = e._init;
83
83
  try {
84
- return o(e(n));
84
+ return o(e(s));
85
85
  } catch {
86
86
  }
87
87
  }
@@ -90,53 +90,53 @@ function ue() {
90
90
  function r(e) {
91
91
  return "" + e;
92
92
  }
93
- function t(e) {
93
+ function n(e) {
94
94
  try {
95
95
  r(e);
96
- var n = !1;
96
+ var s = !1;
97
97
  } catch {
98
- n = !0;
98
+ s = !0;
99
99
  }
100
- if (n) {
101
- n = console;
102
- var l = n.error, d = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
100
+ if (s) {
101
+ s = console;
102
+ var l = s.error, m = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
103
103
  return l.call(
104
- n,
104
+ s,
105
105
  "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
106
- d
106
+ m
107
107
  ), r(e);
108
108
  }
109
109
  }
110
110
  function u(e) {
111
- if (e === f) return "<>";
112
- if (typeof e == "object" && e !== null && e.$$typeof === M)
111
+ if (e === g) return "<>";
112
+ if (typeof e == "object" && e !== null && e.$$typeof === b)
113
113
  return "<...>";
114
114
  try {
115
- var n = o(e);
116
- return n ? "<" + n + ">" : "<...>";
115
+ var s = o(e);
116
+ return s ? "<" + s + ">" : "<...>";
117
117
  } catch {
118
118
  return "<...>";
119
119
  }
120
120
  }
121
- function s() {
122
- var e = C.A;
121
+ function c() {
122
+ var e = E.A;
123
123
  return e === null ? null : e.getOwner();
124
124
  }
125
- function c() {
125
+ function f() {
126
126
  return Error("react-stack-top-frame");
127
127
  }
128
- function m(e) {
129
- if (F.call(e, "key")) {
130
- var n = Object.getOwnPropertyDescriptor(e, "key").get;
131
- if (n && n.isReactWarning) return !1;
128
+ function w(e) {
129
+ if (I.call(e, "key")) {
130
+ var s = Object.getOwnPropertyDescriptor(e, "key").get;
131
+ if (s && s.isReactWarning) return !1;
132
132
  }
133
133
  return e.key !== void 0;
134
134
  }
135
- function p(e, n) {
135
+ function d(e, s) {
136
136
  function l() {
137
- Y || (Y = !0, console.error(
137
+ X || (X = !0, console.error(
138
138
  "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
139
- n
139
+ s
140
140
  ));
141
141
  }
142
142
  l.isReactWarning = !0, Object.defineProperty(e, "key", {
@@ -146,17 +146,17 @@ function ue() {
146
146
  }
147
147
  function R() {
148
148
  var e = o(this.type);
149
- return L[e] || (L[e] = !0, console.error(
149
+ return H[e] || (H[e] = !0, console.error(
150
150
  "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
151
151
  )), e = this.props.ref, e !== void 0 ? e : null;
152
152
  }
153
- function T(e, n, l, d, w, h, x, j) {
154
- return l = h.ref, e = {
155
- $$typeof: y,
153
+ function h(e, s, l, m, S, v, z, Q) {
154
+ return l = v.ref, e = {
155
+ $$typeof: q,
156
156
  type: e,
157
- key: n,
158
- props: h,
159
- _owner: w
157
+ key: s,
158
+ props: v,
159
+ _owner: S
160
160
  }, (l !== void 0 ? l : null) !== null ? Object.defineProperty(e, "ref", {
161
161
  enumerable: !1,
162
162
  get: R
@@ -174,119 +174,119 @@ function ue() {
174
174
  configurable: !1,
175
175
  enumerable: !1,
176
176
  writable: !0,
177
- value: x
177
+ value: z
178
178
  }), Object.defineProperty(e, "_debugTask", {
179
179
  configurable: !1,
180
180
  enumerable: !1,
181
181
  writable: !0,
182
- value: j
182
+ value: Q
183
183
  }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
184
184
  }
185
- function E(e, n, l, d, w, h, x, j) {
186
- var b = n.children;
187
- if (b !== void 0)
188
- if (d)
189
- if (te(b)) {
190
- for (d = 0; d < b.length; d++)
191
- v(b[d]);
192
- Object.freeze && Object.freeze(b);
185
+ function C(e, s, l, m, S, v, z, Q) {
186
+ var p = s.children;
187
+ if (p !== void 0)
188
+ if (m)
189
+ if (P(p)) {
190
+ for (m = 0; m < p.length; m++)
191
+ x(p[m]);
192
+ Object.freeze && Object.freeze(p);
193
193
  } else
194
194
  console.error(
195
195
  "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
196
196
  );
197
- else v(b);
198
- if (F.call(n, "key")) {
199
- b = o(e);
200
- var _ = Object.keys(n).filter(function(ne) {
201
- return ne !== "key";
197
+ else x(p);
198
+ if (I.call(s, "key")) {
199
+ p = o(e);
200
+ var O = Object.keys(s).filter(function(se) {
201
+ return se !== "key";
202
202
  });
203
- d = 0 < _.length ? "{key: someKey, " + _.join(": ..., ") + ": ...}" : "{key: someKey}", q[b + d] || (_ = 0 < _.length ? "{" + _.join(": ..., ") + ": ...}" : "{}", console.error(
203
+ m = 0 < O.length ? "{key: someKey, " + O.join(": ..., ") + ": ...}" : "{key: someKey}", K[p + m] || (O = 0 < O.length ? "{" + O.join(": ..., ") + ": ...}" : "{}", console.error(
204
204
  `A props object containing a "key" prop is being spread into JSX:
205
205
  let props = %s;
206
206
  <%s {...props} />
207
207
  React keys must be passed directly to JSX without using spread:
208
208
  let props = %s;
209
209
  <%s key={someKey} {...props} />`,
210
- d,
211
- b,
212
- _,
213
- b
214
- ), q[b + d] = !0);
210
+ m,
211
+ p,
212
+ O,
213
+ p
214
+ ), K[p + m] = !0);
215
215
  }
216
- if (b = null, l !== void 0 && (t(l), b = "" + l), m(n) && (t(n.key), b = "" + n.key), "key" in n) {
216
+ if (p = null, l !== void 0 && (n(l), p = "" + l), w(s) && (n(s.key), p = "" + s.key), "key" in s) {
217
217
  l = {};
218
- for (var N in n)
219
- N !== "key" && (l[N] = n[N]);
220
- } else l = n;
221
- return b && p(
218
+ for (var G in s)
219
+ G !== "key" && (l[G] = s[G]);
220
+ } else l = s;
221
+ return p && d(
222
222
  l,
223
223
  typeof e == "function" ? e.displayName || e.name || "Unknown" : e
224
- ), T(
224
+ ), h(
225
225
  e,
226
- b,
227
- h,
228
- w,
229
- s(),
226
+ p,
227
+ v,
228
+ S,
229
+ c(),
230
230
  l,
231
- x,
232
- j
231
+ z,
232
+ Q
233
233
  );
234
234
  }
235
- function v(e) {
236
- typeof e == "object" && e !== null && e.$$typeof === y && e._store && (e._store.validated = 1);
235
+ function x(e) {
236
+ typeof e == "object" && e !== null && e.$$typeof === q && e._store && (e._store.validated = 1);
237
237
  }
238
- var a = I, y = Symbol.for("react.transitional.element"), i = Symbol.for("react.portal"), f = Symbol.for("react.fragment"), G = Symbol.for("react.strict_mode"), Q = Symbol.for("react.profiler"), X = Symbol.for("react.consumer"), B = Symbol.for("react.context"), H = Symbol.for("react.forward_ref"), Z = Symbol.for("react.suspense"), K = Symbol.for("react.suspense_list"), D = Symbol.for("react.memo"), M = Symbol.for("react.lazy"), ee = Symbol.for("react.activity"), re = Symbol.for("react.client.reference"), C = a.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, F = Object.prototype.hasOwnProperty, te = Array.isArray, O = console.createTask ? console.createTask : function() {
238
+ var T = U, q = Symbol.for("react.transitional.element"), J = Symbol.for("react.portal"), g = Symbol.for("react.fragment"), A = Symbol.for("react.strict_mode"), k = Symbol.for("react.profiler"), j = Symbol.for("react.consumer"), N = Symbol.for("react.context"), i = Symbol.for("react.forward_ref"), L = Symbol.for("react.suspense"), Y = Symbol.for("react.suspense_list"), t = Symbol.for("react.memo"), b = Symbol.for("react.lazy"), W = Symbol.for("react.activity"), V = Symbol.for("react.client.reference"), E = T.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, I = Object.prototype.hasOwnProperty, P = Array.isArray, a = console.createTask ? console.createTask : function() {
239
239
  return null;
240
240
  };
241
- a = {
241
+ T = {
242
242
  react_stack_bottom_frame: function(e) {
243
243
  return e();
244
244
  }
245
245
  };
246
- var Y, L = {}, W = a.react_stack_bottom_frame.bind(
247
- a,
248
- c
249
- )(), $ = O(u(c)), q = {};
250
- k.Fragment = f, k.jsx = function(e, n, l, d, w) {
251
- var h = 1e4 > C.recentlyCreatedOwnerStacks++;
252
- return E(
246
+ var X, H = {}, D = T.react_stack_bottom_frame.bind(
247
+ T,
248
+ f
249
+ )(), Z = a(u(f)), K = {};
250
+ F.Fragment = g, F.jsx = function(e, s, l, m, S) {
251
+ var v = 1e4 > E.recentlyCreatedOwnerStacks++;
252
+ return C(
253
253
  e,
254
- n,
254
+ s,
255
255
  l,
256
256
  !1,
257
- d,
258
- w,
259
- h ? Error("react-stack-top-frame") : W,
260
- h ? O(u(e)) : $
257
+ m,
258
+ S,
259
+ v ? Error("react-stack-top-frame") : D,
260
+ v ? a(u(e)) : Z
261
261
  );
262
- }, k.jsxs = function(e, n, l, d, w) {
263
- var h = 1e4 > C.recentlyCreatedOwnerStacks++;
264
- return E(
262
+ }, F.jsxs = function(e, s, l, m, S) {
263
+ var v = 1e4 > E.recentlyCreatedOwnerStacks++;
264
+ return C(
265
265
  e,
266
- n,
266
+ s,
267
267
  l,
268
268
  !0,
269
- d,
270
- w,
271
- h ? Error("react-stack-top-frame") : W,
272
- h ? O(u(e)) : $
269
+ m,
270
+ S,
271
+ v ? Error("react-stack-top-frame") : D,
272
+ v ? a(u(e)) : Z
273
273
  );
274
274
  };
275
- })()), k;
275
+ })()), F;
276
276
  }
277
- var z;
277
+ var oe;
278
278
  function ce() {
279
- return z || (z = 1, process.env.NODE_ENV === "production" ? A.exports = ie() : A.exports = ue()), A.exports;
279
+ return oe || (oe = 1, process.env.NODE_ENV === "production" ? $.exports = ie() : $.exports = ae()), $.exports;
280
280
  }
281
- var ae = ce();
281
+ var ue = ce();
282
282
  class le extends EventTarget {
283
283
  subscriptions = /* @__PURE__ */ new Map();
284
284
  targetOrigin = "*";
285
285
  /**
286
286
  * Check if a form ID matches the filter
287
287
  */
288
- matchesFilter(r, t) {
289
- return !t || t === "*" || t === null || t === void 0 ? !0 : typeof t == "string" ? r === t : Array.isArray(t) ? t.includes(r || "") : typeof t == "function" ? t(r || "") : !1;
288
+ matchesFilter(r, n) {
289
+ return !n || n === "*" || n === null || n === void 0 ? !0 : typeof n == "string" ? r === n : Array.isArray(n) ? n.includes(r || "") : typeof n == "function" ? n(r || "") : !1;
290
290
  }
291
291
  /**
292
292
  * Get form ID from payload
@@ -297,42 +297,42 @@ class le extends EventTarget {
297
297
  /**
298
298
  * Publish an event to all matching subscribers
299
299
  */
300
- publish(r, t, u) {
300
+ publish(r, n, u) {
301
301
  typeof window < "u" && window.dispatchEvent(
302
302
  new CustomEvent(r, {
303
- detail: t,
303
+ detail: n,
304
304
  bubbles: !0
305
305
  })
306
- ), u?.usePostMessage !== !1 && this.sendPostMessage(r, t);
306
+ ), u?.usePostMessage !== !1 && this.sendPostMessage(r, n);
307
307
  }
308
308
  /**
309
309
  * Subscribe to form events with optional form ID filtering
310
310
  */
311
- subscribe(r, t, u) {
312
- const s = Symbol("subscription"), c = {
311
+ subscribe(r, n, u) {
312
+ const c = Symbol("subscription"), f = {
313
313
  eventType: r,
314
- handler: t,
314
+ handler: n,
315
315
  filter: u?.formId,
316
- id: s,
316
+ id: c,
317
317
  once: u?.once
318
318
  };
319
- this.subscriptions.has(r) || this.subscriptions.set(r, []), this.subscriptions.get(r).push(c);
320
- const m = (p) => {
321
- const R = p, T = this.getFormIdFromPayload(R.detail);
322
- this.matchesFilter(T, u?.formId) && (t(R.detail), u?.once && typeof window < "u" && window.removeEventListener(r, m));
319
+ this.subscriptions.has(r) || this.subscriptions.set(r, []), this.subscriptions.get(r).push(f);
320
+ const w = (d) => {
321
+ const R = d, h = this.getFormIdFromPayload(R.detail);
322
+ this.matchesFilter(h, u?.formId) && (n(R.detail), u?.once && typeof window < "u" && window.removeEventListener(r, w));
323
323
  };
324
- return typeof window < "u" && window.addEventListener(r, m), () => {
325
- this.unsubscribe(s), typeof window < "u" && window.removeEventListener(r, m);
324
+ return typeof window < "u" && window.addEventListener(r, w), () => {
325
+ this.unsubscribe(c), typeof window < "u" && window.removeEventListener(r, w);
326
326
  };
327
327
  }
328
328
  /**
329
329
  * Unsubscribe by subscription ID
330
330
  */
331
331
  unsubscribe(r) {
332
- for (const [t, u] of this.subscriptions.entries()) {
333
- const s = u.findIndex((c) => c.id === r);
334
- if (s !== -1) {
335
- u.splice(s, 1), u.length === 0 && this.subscriptions.delete(t);
332
+ for (const [n, u] of this.subscriptions.entries()) {
333
+ const c = u.findIndex((f) => f.id === r);
334
+ if (c !== -1) {
335
+ u.splice(c, 1), u.length === 0 && this.subscriptions.delete(n);
336
336
  break;
337
337
  }
338
338
  }
@@ -340,7 +340,7 @@ class le extends EventTarget {
340
340
  /**
341
341
  * Subscribe to all form events with optional filtering
342
342
  */
343
- subscribeAll(r, t) {
343
+ subscribeAll(r, n) {
344
344
  const u = [
345
345
  "form:show",
346
346
  "form:started",
@@ -349,23 +349,23 @@ class le extends EventTarget {
349
349
  "form:section:change",
350
350
  "form:question:answered",
351
351
  "form:error"
352
- ], s = [];
353
- return u.forEach((c) => {
354
- const m = this.subscribe(
355
- c,
356
- (p) => r(c, p),
357
- t
352
+ ], c = [];
353
+ return u.forEach((f) => {
354
+ const w = this.subscribe(
355
+ f,
356
+ (d) => r(f, d),
357
+ n
358
358
  );
359
- s.push(m);
359
+ c.push(w);
360
360
  }), () => {
361
- s.forEach((c) => c());
361
+ c.forEach((f) => f());
362
362
  };
363
363
  }
364
364
  /**
365
365
  * Get active subscriptions count (for debugging)
366
366
  */
367
367
  getSubscriptionCount(r) {
368
- return r ? this.subscriptions.get(r)?.length || 0 : Array.from(this.subscriptions.values()).reduce((t, u) => t + u.length, 0);
368
+ return r ? this.subscriptions.get(r)?.length || 0 : Array.from(this.subscriptions.values()).reduce((n, u) => n + u.length, 0);
369
369
  }
370
370
  /**
371
371
  * Clear all subscriptions for an event type (or all events)
@@ -373,127 +373,239 @@ class le extends EventTarget {
373
373
  clearSubscriptions(r) {
374
374
  r ? this.subscriptions.delete(r) : this.subscriptions.clear();
375
375
  }
376
- sendPostMessage(r, t) {
376
+ sendPostMessage(r, n) {
377
377
  const u = {
378
378
  type: `encatch:${r}`,
379
- payload: t,
379
+ payload: n,
380
380
  source: "encatch-form-engine"
381
381
  };
382
- window.parent && window.parent !== window && window.parent.postMessage(u, this.targetOrigin), document.querySelectorAll("iframe").forEach((s) => {
383
- s.contentWindow && s.contentWindow.postMessage(u, this.targetOrigin);
382
+ window.parent && window.parent !== window && window.parent.postMessage(u, this.targetOrigin), document.querySelectorAll("iframe").forEach((c) => {
383
+ c.contentWindow && c.contentWindow.postMessage(u, this.targetOrigin);
384
384
  }), typeof window.ReactNativeWebView < "u" && window.ReactNativeWebView.postMessage(JSON.stringify({
385
385
  action: r.replace("form:", ""),
386
- data: JSON.stringify(t)
386
+ data: JSON.stringify(n)
387
387
  }));
388
388
  }
389
389
  }
390
- const g = new le(), de = ({
390
+ const _ = new le(), de = ({
391
391
  formData: o,
392
- cssLink: r,
393
- scale: t = 100,
394
- persistMode: u = "none",
395
- instanceId: s,
396
- onFormEvent: c
392
+ formConfig: r,
393
+ formPageUrl: n,
394
+ formId: u,
395
+ cssLink: c,
396
+ scale: f = 100,
397
+ persistMode: w = "none",
398
+ instanceId: d,
399
+ onFormEvent: R
397
400
  }) => {
398
- const [m, p] = U(!1), [R, T] = U(!0), E = I.useRef(null), v = I.useRef(
401
+ const h = !!(r && n), C = u ?? r?.formId ?? d ?? "preview", [x, T] = ee(!1), [q, J] = ee(!0), g = U.useRef(null), A = U.useRef(
399
402
  null
400
- ), a = oe({});
401
- S(() => {
402
- if (!c) return;
403
- c({
404
- onSubmit: (f) => {
405
- a.current.onSubmit = f;
403
+ ), k = U.useRef(null), j = B(!1), N = B(() => {
404
+ }), i = B({});
405
+ y(() => {
406
+ if (!R) return;
407
+ R({
408
+ onSubmit: (b) => {
409
+ i.current.onSubmit = b;
406
410
  },
407
- onShow: (f) => {
408
- a.current.onShow = f;
411
+ onShow: (b) => {
412
+ i.current.onShow = b;
409
413
  },
410
- onClose: (f) => {
411
- a.current.onClose = f;
414
+ onClose: (b) => {
415
+ i.current.onClose = b;
412
416
  },
413
- onSectionChange: (f) => {
414
- a.current.onSectionChange = f;
417
+ onSectionChange: (b) => {
418
+ i.current.onSectionChange = b;
415
419
  },
416
- onQuestionAnswered: (f) => {
417
- a.current.onQuestionAnswered = f;
420
+ onQuestionAnswered: (b) => {
421
+ i.current.onQuestionAnswered = b;
418
422
  },
419
- onError: (f) => {
420
- a.current.onError = f;
423
+ onError: (b) => {
424
+ i.current.onError = b;
421
425
  }
422
426
  });
423
- }, [c]), S(() => {
424
- if (!s) return;
425
- const i = [];
426
- return a.current.onSubmit && i.push(
427
- g.subscribe(
427
+ }, [R]);
428
+ const L = re(() => {
429
+ if (!k.current?.contentWindow || !r) return;
430
+ const t = { formId: C, ...r };
431
+ k.current.contentWindow.postMessage(
432
+ { type: "sdk:formConfig", data: t },
433
+ "*"
434
+ );
435
+ }, [r, C]);
436
+ N.current = L, y(() => {
437
+ !h || !j.current || N.current();
438
+ }, [h, r, L]), y(() => {
439
+ if (!h || !g.current) return;
440
+ const t = document.createElement("iframe"), b = new URL(n, window.location.origin);
441
+ b.searchParams.set("formId", C), t.src = b.toString(), t.title = "Encatch form", t.style.width = "100%", t.style.height = "100%", t.style.border = "none", k.current = t, g.current.appendChild(t);
442
+ const W = (V) => {
443
+ const E = V.data;
444
+ if (!E || typeof E != "object" || !E.type) return;
445
+ let I = E.type, P = E.formId, a = E.data;
446
+ switch (I.startsWith("encatch:") && (I = I.replace("encatch:", ""), P = E.payload?.feedbackConfigurationId, a = E.payload), I) {
447
+ case "form:ready":
448
+ j.current = !0, N.current();
449
+ break;
450
+ case "form:close":
451
+ i.current.onClose?.({ timestamp: Date.now() });
452
+ break;
453
+ case "form:complete":
454
+ i.current.onClose?.({ timestamp: Date.now() });
455
+ break;
456
+ case "form:submit":
457
+ a && i.current.onSubmit && i.current.onSubmit({
458
+ feedbackConfigurationId: a.feedbackConfigurationId,
459
+ feedbackIdentifier: a.feedbackIdentifier,
460
+ response: a.response,
461
+ isPartialSubmit: a.isPartialSubmit,
462
+ timestamp: Date.now()
463
+ });
464
+ break;
465
+ case "form:show":
466
+ a && i.current.onShow && i.current.onShow({
467
+ feedbackConfigurationId: a.feedbackConfigurationId,
468
+ feedbackIdentifier: a.feedbackIdentifier,
469
+ timestamp: Date.now()
470
+ });
471
+ break;
472
+ case "form:started":
473
+ a && i.current.onShow && i.current.onShow({
474
+ feedbackConfigurationId: a.feedbackConfigurationId,
475
+ feedbackIdentifier: a.feedbackIdentifier,
476
+ timestamp: Date.now()
477
+ });
478
+ break;
479
+ case "form:section:change":
480
+ a && i.current.onSectionChange && i.current.onSectionChange({
481
+ feedbackConfigurationId: a.feedbackConfigurationId ?? P ?? "",
482
+ sectionIndex: a.sectionIndex,
483
+ sectionId: a.sectionId,
484
+ timestamp: Date.now()
485
+ });
486
+ break;
487
+ case "form:question:answered":
488
+ a && i.current.onQuestionAnswered && i.current.onQuestionAnswered({
489
+ feedbackConfigurationId: a.feedbackConfigurationId ?? P ?? "",
490
+ questionId: a.questionId,
491
+ questionType: a.questionType,
492
+ answer: a.answer,
493
+ timestamp: Date.now()
494
+ });
495
+ break;
496
+ case "form:error":
497
+ a && i.current.onError && i.current.onError({
498
+ feedbackConfigurationId: a.feedbackConfigurationId ?? P ?? "",
499
+ questionId: a.questionId,
500
+ error: a.error ?? "Unknown error",
501
+ timestamp: Date.now()
502
+ });
503
+ break;
504
+ }
505
+ };
506
+ return window.addEventListener("message", W), () => {
507
+ if (window.removeEventListener("message", W), j.current = !1, g.current && k.current)
508
+ try {
509
+ g.current.removeChild(k.current);
510
+ } catch {
511
+ }
512
+ k.current = null;
513
+ };
514
+ }, [h, n, C]), y(() => {
515
+ if (h || !d) return;
516
+ const t = [];
517
+ return i.current.onSubmit && t.push(
518
+ _.subscribe(
428
519
  "form:submit",
429
- a.current.onSubmit,
430
- { formId: s }
520
+ i.current.onSubmit,
521
+ { formId: d }
431
522
  )
432
- ), a.current.onShow && i.push(
433
- g.subscribe(
523
+ ), i.current.onShow && t.push(
524
+ _.subscribe(
434
525
  "form:show",
435
- a.current.onShow,
436
- { formId: s }
526
+ i.current.onShow,
527
+ { formId: d }
437
528
  )
438
- ), a.current.onClose && i.push(
439
- g.subscribe(
529
+ ), i.current.onClose && t.push(
530
+ _.subscribe(
440
531
  "form:close",
441
- a.current.onClose,
442
- { formId: s }
532
+ i.current.onClose,
533
+ { formId: d }
443
534
  )
444
- ), a.current.onSectionChange && i.push(
445
- g.subscribe(
535
+ ), i.current.onSectionChange && t.push(
536
+ _.subscribe(
446
537
  "form:section:change",
447
- a.current.onSectionChange,
448
- { formId: s }
538
+ i.current.onSectionChange,
539
+ { formId: d }
449
540
  )
450
- ), a.current.onQuestionAnswered && i.push(
451
- g.subscribe(
541
+ ), i.current.onQuestionAnswered && t.push(
542
+ _.subscribe(
452
543
  "form:question:answered",
453
- a.current.onQuestionAnswered,
454
- { formId: s }
544
+ i.current.onQuestionAnswered,
545
+ { formId: d }
455
546
  )
456
- ), a.current.onError && i.push(
457
- g.subscribe(
547
+ ), i.current.onError && t.push(
548
+ _.subscribe(
458
549
  "form:error",
459
- a.current.onError,
460
- { formId: s }
550
+ i.current.onError,
551
+ { formId: d }
461
552
  )
462
- ), () => {
463
- i.forEach((f) => f());
464
- };
465
- }, [s]), S(() => {
466
- (() => {
467
- try {
468
- return o ? !0 : (console.warn("[EncatchPreview] formData is not defined"), !1);
469
- } catch (f) {
470
- return console.error("[EncatchPreview] Error during data validation:", f), !1;
471
- }
472
- })() ? p(!0) : console.warn(
473
- "[EncatchPreview] Data validation failed, retrying in 100ms..."
474
- );
475
- }, [o]);
476
- const y = se(() => {
477
- o.onClose && o.onClose(), T(!1);
478
- }, [o.onClose]);
479
- return S(() => {
480
- if (!m || !E.current)
553
+ ), () => t.forEach((b) => b());
554
+ }, [h, d]), y(() => {
555
+ if (h) {
556
+ T(!1);
481
557
  return;
482
- const i = document.createElement("encatch-app");
483
- return i.theme = o.theme, i.currentMode = o.currentMode, i.currentLanguage = o.currentLanguage, i.questions = o.questions, i.questionLanguages = o.questionLanguages, i.otherConfigurationProperties = o.otherConfigurationProperties, i.welcomeScreenProperties = o.welcomeScreenProperties, i.endScreenProperties = o.endScreenProperties, i.translations = o.translations, i.sections = o.sections, i.themeSettings = o.themeSettings, i.onClose = y, i.onSectionChange = o.onSectionChange, i.apiKey = o.apiKey, i.hostUrl = o.hostUrl, i.feedbackConfigId = o.feedbackConfigId, i.identifier = o.identifier, r && (i.cssLink = r), i.scale = t, i.persistMode = u, s && (i.instanceId = s), v.current = i, E.current.appendChild(i), () => {
484
- E.current && v.current && E.current.removeChild(v.current), v.current = null;
558
+ }
559
+ try {
560
+ if (!o) {
561
+ console.warn("[EncatchPreview] formData is not defined (inline mode)");
562
+ return;
563
+ }
564
+ T(!0);
565
+ } catch (t) {
566
+ console.error("[EncatchPreview] Error during data validation:", t);
567
+ }
568
+ }, [h, o]);
569
+ const Y = re(() => {
570
+ o?.onClose && o.onClose(), J(!1);
571
+ }, [o?.onClose]);
572
+ return y(() => {
573
+ if (h || !x || !g.current || !o) return;
574
+ const t = document.createElement("encatch-app");
575
+ return t.theme = o.theme, t.currentMode = o.currentMode, t.currentLanguage = o.currentLanguage, t.questions = o.questions, t.questionLanguages = o.questionLanguages, t.otherConfigurationProperties = o.otherConfigurationProperties, t.welcomeScreenProperties = o.welcomeScreenProperties, t.endScreenProperties = o.endScreenProperties, t.translations = o.translations, t.sections = o.sections, t.themeSettings = o.themeSettings, t.onClose = Y, t.onSectionChange = o.onSectionChange, t.apiKey = o.apiKey, t.hostUrl = o.hostUrl, t.feedbackConfigId = o.feedbackConfigId, t.identifier = o.identifier, c && (t.cssLink = c), t.scale = f, t.persistMode = w, d && (t.instanceId = d), A.current = t, g.current.appendChild(t), () => {
576
+ if (g.current && A.current)
577
+ try {
578
+ g.current.removeChild(A.current);
579
+ } catch {
580
+ }
581
+ A.current = null;
485
582
  };
486
- }, [m, o, r, t, y, u, s]), R ? /* @__PURE__ */ ae.jsx("div", { ref: E }) : null;
583
+ }, [
584
+ h,
585
+ x,
586
+ o,
587
+ c,
588
+ f,
589
+ Y,
590
+ w,
591
+ d
592
+ ]), q ? /* @__PURE__ */ ue.jsx(
593
+ "div",
594
+ {
595
+ ref: g,
596
+ style: h ? { width: "100%", height: "100%", minHeight: 400 } : void 0
597
+ }
598
+ ) : null;
487
599
  };
488
- function be(o, r, t) {
489
- S(() => g.subscribe(
600
+ function be(o, r, n) {
601
+ y(() => _.subscribe(
490
602
  o,
491
603
  r,
492
- t
493
- ), [o, t?.formId, r]);
604
+ n
605
+ ), [o, n?.formId, r]);
494
606
  }
495
607
  function me(o, r) {
496
- S(() => g.subscribeAll(o, r), [r?.formId, o]);
608
+ y(() => _.subscribeAll(o, r), [r?.formId, o]);
497
609
  }
498
610
  export {
499
611
  de as EncatchPreview,
package/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(w,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],d):(w=typeof globalThis<"u"?globalThis:w||self,d(w.WsReact={},w.React))})(this,(function(w,d){"use strict";var C={exports:{}},y={};/**
1
+ (function(S,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],f):(S=typeof globalThis<"u"?globalThis:S||self,f(S.WsReact={},S.React))})(this,(function(S,f){"use strict";var L={exports:{}},x={};/**
2
2
  * @license React
3
3
  * react-jsx-runtime.production.js
4
4
  *
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * This source code is licensed under the MIT license found in the
8
8
  * LICENSE file in the root directory of this source tree.
9
- */var M;function z(){if(M)return y;M=1;var o=Symbol.for("react.transitional.element"),r=Symbol.for("react.fragment");function t(u,s,c){var E=null;if(c!==void 0&&(E=""+c),s.key!==void 0&&(E=""+s.key),"key"in s){c={};for(var h in s)h!=="key"&&(c[h]=s[h])}else c=s;return s=c.ref,{$$typeof:o,type:u,key:E,ref:s!==void 0?s:null,props:c}}return y.Fragment=r,y.jsx=t,y.jsxs=t,y}var P={};/**
9
+ */var B;function ne(){if(B)return x;B=1;var o=Symbol.for("react.transitional.element"),r=Symbol.for("react.fragment");function t(a,c,d){var E=null;if(d!==void 0&&(E=""+d),c.key!==void 0&&(E=""+c.key),"key"in c){d={};for(var b in c)b!=="key"&&(d[b]=c[b])}else d=c;return c=d.ref,{$$typeof:o,type:a,key:E,ref:c!==void 0?c:null,props:d}}return x.Fragment=r,x.jsx=t,x.jsxs=t,x}var N={};/**
10
10
  * @license React
11
11
  * react-jsx-runtime.development.js
12
12
  *
@@ -14,9 +14,9 @@
14
14
  *
15
15
  * This source code is licensed under the MIT license found in the
16
16
  * LICENSE file in the root directory of this source tree.
17
- */var F;function G(){return F||(F=1,process.env.NODE_ENV!=="production"&&(function(){function o(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===ce?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case f:return"Fragment";case ee:return"Profiler";case D:return"StrictMode";case oe:return"Suspense";case se:return"SuspenseList";case ue:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case i:return"Portal";case te:return(e.displayName||"Context")+".Provider";case re:return(e._context.displayName||"Context")+".Consumer";case ne:var n=e.render;return e=e.displayName,e||(e=n.displayName||n.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case ie:return n=e.displayName||null,n!==null?n:o(e.type)||"Memo";case L:n=e._payload,e=e._init;try{return o(e(n))}catch{}}return null}function r(e){return""+e}function t(e){try{r(e);var n=!1}catch{n=!0}if(n){n=console;var l=n.error,b=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return l.call(n,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",b),r(e)}}function u(e){if(e===f)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===L)return"<...>";try{var n=o(e);return n?"<"+n+">":"<...>"}catch{return"<...>"}}function s(){var e=O.A;return e===null?null:e.getOwner()}function c(){return Error("react-stack-top-frame")}function E(e){if(W.call(e,"key")){var n=Object.getOwnPropertyDescriptor(e,"key").get;if(n&&n.isReactWarning)return!1}return e.key!==void 0}function h(e,n){function l(){q||(q=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",n))}l.isReactWarning=!0,Object.defineProperty(e,"key",{get:l,configurable:!0})}function R(){var e=o(this.type);return $[e]||($[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function A(e,n,l,b,S,p,x,N){return l=p.ref,e={$$typeof:k,type:e,key:n,props:p,_owner:S},(l!==void 0?l:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:R}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:x}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:N}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function g(e,n,l,b,S,p,x,N){var m=n.children;if(m!==void 0)if(b)if(ae(m)){for(b=0;b<m.length;b++)_(m[b]);Object.freeze&&Object.freeze(m)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else _(m);if(W.call(n,"key")){m=o(e);var T=Object.keys(n).filter(function(le){return le!=="key"});b=0<T.length?"{key: someKey, "+T.join(": ..., ")+": ...}":"{key: someKey}",V[m+b]||(T=0<T.length?"{"+T.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
17
+ */var X;function te(){return X||(X=1,process.env.NODE_ENV!=="production"&&(function(){function o(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===V?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case g:return"Fragment";case _:return"Profiler";case A:return"StrictMode";case W:return"Suspense";case $:return"SuspenseList";case U:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case J:return"Portal";case q:return(e.displayName||"Context")+".Provider";case F:return(e._context.displayName||"Context")+".Consumer";case i:var s=e.render;return e=e.displayName,e||(e=s.displayName||s.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case n:return s=e.displayName||null,s!==null?s:o(e.type)||"Memo";case m:s=e._payload,e=e._init;try{return o(e(s))}catch{}}return null}function r(e){return""+e}function t(e){try{r(e);var s=!1}catch{s=!0}if(s){s=console;var l=s.error,p=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return l.call(s,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",p),r(e)}}function a(e){if(e===g)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===m)return"<...>";try{var s=o(e);return s?"<"+s+">":"<...>"}catch{return"<...>"}}function c(){var e=v.A;return e===null?null:e.getOwner()}function d(){return Error("react-stack-top-frame")}function E(e){if(P.call(e,"key")){var s=Object.getOwnPropertyDescriptor(e,"key").get;if(s&&s.isReactWarning)return!1}return e.key!==void 0}function b(e,s){function l(){D||(D=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",s))}l.isReactWarning=!0,Object.defineProperty(e,"key",{get:l,configurable:!0})}function R(){var e=o(this.type);return Z[e]||(Z[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function w(e,s,l,p,T,k,z,Q){return l=k.ref,e={$$typeof:Y,type:e,key:s,props:k,_owner:T},(l!==void 0?l:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:R}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:z}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:Q}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function C(e,s,l,p,T,k,z,Q){var h=s.children;if(h!==void 0)if(p)if(O(h)){for(p=0;p<h.length;p++)M(h[p]);Object.freeze&&Object.freeze(h)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else M(h);if(P.call(s,"key")){h=o(e);var j=Object.keys(s).filter(function(fe){return fe!=="key"});p=0<j.length?"{key: someKey, "+j.join(": ..., ")+": ...}":"{key: someKey}",re[h+p]||(j=0<j.length?"{"+j.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
18
18
  let props = %s;
19
19
  <%s {...props} />
20
20
  React keys must be passed directly to JSX without using spread:
21
21
  let props = %s;
22
- <%s key={someKey} {...props} />`,b,m,T,m),V[m+b]=!0)}if(m=null,l!==void 0&&(t(l),m=""+l),E(n)&&(t(n.key),m=""+n.key),"key"in n){l={};for(var I in n)I!=="key"&&(l[I]=n[I])}else l=n;return m&&h(l,typeof e=="function"?e.displayName||e.name||"Unknown":e),A(e,m,p,S,s(),l,x,N)}function _(e){typeof e=="object"&&e!==null&&e.$$typeof===k&&e._store&&(e._store.validated=1)}var a=d,k=Symbol.for("react.transitional.element"),i=Symbol.for("react.portal"),f=Symbol.for("react.fragment"),D=Symbol.for("react.strict_mode"),ee=Symbol.for("react.profiler"),re=Symbol.for("react.consumer"),te=Symbol.for("react.context"),ne=Symbol.for("react.forward_ref"),oe=Symbol.for("react.suspense"),se=Symbol.for("react.suspense_list"),ie=Symbol.for("react.memo"),L=Symbol.for("react.lazy"),ue=Symbol.for("react.activity"),ce=Symbol.for("react.client.reference"),O=a.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,W=Object.prototype.hasOwnProperty,ae=Array.isArray,j=console.createTask?console.createTask:function(){return null};a={react_stack_bottom_frame:function(e){return e()}};var q,$={},U=a.react_stack_bottom_frame.bind(a,c)(),J=j(u(c)),V={};P.Fragment=f,P.jsx=function(e,n,l,b,S){var p=1e4>O.recentlyCreatedOwnerStacks++;return g(e,n,l,!1,b,S,p?Error("react-stack-top-frame"):U,p?j(u(e)):J)},P.jsxs=function(e,n,l,b,S){var p=1e4>O.recentlyCreatedOwnerStacks++;return g(e,n,l,!0,b,S,p?Error("react-stack-top-frame"):U,p?j(u(e)):J)}})()),P}var Y;function Q(){return Y||(Y=1,process.env.NODE_ENV==="production"?C.exports=z():C.exports=G()),C.exports}var X=Q();class B extends EventTarget{subscriptions=new Map;targetOrigin="*";matchesFilter(r,t){return!t||t==="*"||t===null||t===void 0?!0:typeof t=="string"?r===t:Array.isArray(t)?t.includes(r||""):typeof t=="function"?t(r||""):!1}getFormIdFromPayload(r){return r?.feedbackConfigurationId}publish(r,t,u){typeof window<"u"&&window.dispatchEvent(new CustomEvent(r,{detail:t,bubbles:!0})),u?.usePostMessage!==!1&&this.sendPostMessage(r,t)}subscribe(r,t,u){const s=Symbol("subscription"),c={eventType:r,handler:t,filter:u?.formId,id:s,once:u?.once};this.subscriptions.has(r)||this.subscriptions.set(r,[]),this.subscriptions.get(r).push(c);const E=h=>{const R=h,A=this.getFormIdFromPayload(R.detail);this.matchesFilter(A,u?.formId)&&(t(R.detail),u?.once&&typeof window<"u"&&window.removeEventListener(r,E))};return typeof window<"u"&&window.addEventListener(r,E),()=>{this.unsubscribe(s),typeof window<"u"&&window.removeEventListener(r,E)}}unsubscribe(r){for(const[t,u]of this.subscriptions.entries()){const s=u.findIndex(c=>c.id===r);if(s!==-1){u.splice(s,1),u.length===0&&this.subscriptions.delete(t);break}}}subscribeAll(r,t){const u=["form:show","form:started","form:submit","form:close","form:section:change","form:question:answered","form:error"],s=[];return u.forEach(c=>{const E=this.subscribe(c,h=>r(c,h),t);s.push(E)}),()=>{s.forEach(c=>c())}}getSubscriptionCount(r){return r?this.subscriptions.get(r)?.length||0:Array.from(this.subscriptions.values()).reduce((t,u)=>t+u.length,0)}clearSubscriptions(r){r?this.subscriptions.delete(r):this.subscriptions.clear()}sendPostMessage(r,t){const u={type:`encatch:${r}`,payload:t,source:"encatch-form-engine"};window.parent&&window.parent!==window&&window.parent.postMessage(u,this.targetOrigin),document.querySelectorAll("iframe").forEach(s=>{s.contentWindow&&s.contentWindow.postMessage(u,this.targetOrigin)}),typeof window.ReactNativeWebView<"u"&&window.ReactNativeWebView.postMessage(JSON.stringify({action:r.replace("form:",""),data:JSON.stringify(t)}))}}const v=new B,H=({formData:o,cssLink:r,scale:t=100,persistMode:u="none",instanceId:s,onFormEvent:c})=>{const[E,h]=d.useState(!1),[R,A]=d.useState(!0),g=d.useRef(null),_=d.useRef(null),a=d.useRef({});d.useEffect(()=>{if(!c)return;c({onSubmit:f=>{a.current.onSubmit=f},onShow:f=>{a.current.onShow=f},onClose:f=>{a.current.onClose=f},onSectionChange:f=>{a.current.onSectionChange=f},onQuestionAnswered:f=>{a.current.onQuestionAnswered=f},onError:f=>{a.current.onError=f}})},[c]),d.useEffect(()=>{if(!s)return;const i=[];return a.current.onSubmit&&i.push(v.subscribe("form:submit",a.current.onSubmit,{formId:s})),a.current.onShow&&i.push(v.subscribe("form:show",a.current.onShow,{formId:s})),a.current.onClose&&i.push(v.subscribe("form:close",a.current.onClose,{formId:s})),a.current.onSectionChange&&i.push(v.subscribe("form:section:change",a.current.onSectionChange,{formId:s})),a.current.onQuestionAnswered&&i.push(v.subscribe("form:question:answered",a.current.onQuestionAnswered,{formId:s})),a.current.onError&&i.push(v.subscribe("form:error",a.current.onError,{formId:s})),()=>{i.forEach(f=>f())}},[s]),d.useEffect(()=>{(()=>{try{return o?!0:(console.warn("[EncatchPreview] formData is not defined"),!1)}catch(f){return console.error("[EncatchPreview] Error during data validation:",f),!1}})()?h(!0):console.warn("[EncatchPreview] Data validation failed, retrying in 100ms...")},[o]);const k=d.useCallback(()=>{o.onClose&&o.onClose(),A(!1)},[o.onClose]);return d.useEffect(()=>{if(!E||!g.current)return;const i=document.createElement("encatch-app");return i.theme=o.theme,i.currentMode=o.currentMode,i.currentLanguage=o.currentLanguage,i.questions=o.questions,i.questionLanguages=o.questionLanguages,i.otherConfigurationProperties=o.otherConfigurationProperties,i.welcomeScreenProperties=o.welcomeScreenProperties,i.endScreenProperties=o.endScreenProperties,i.translations=o.translations,i.sections=o.sections,i.themeSettings=o.themeSettings,i.onClose=k,i.onSectionChange=o.onSectionChange,i.apiKey=o.apiKey,i.hostUrl=o.hostUrl,i.feedbackConfigId=o.feedbackConfigId,i.identifier=o.identifier,r&&(i.cssLink=r),i.scale=t,i.persistMode=u,s&&(i.instanceId=s),_.current=i,g.current.appendChild(i),()=>{g.current&&_.current&&g.current.removeChild(_.current),_.current=null}},[E,o,r,t,k,u,s]),R?X.jsx("div",{ref:g}):null};function Z(o,r,t){d.useEffect(()=>v.subscribe(o,r,t),[o,t?.formId,r])}function K(o,r){d.useEffect(()=>v.subscribeAll(o,r),[r?.formId,o])}w.EncatchPreview=H,w.useEncatchFormEvent=Z,w.useEncatchFormEventAll=K,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})}));
22
+ <%s key={someKey} {...props} />`,p,h,j,h),re[h+p]=!0)}if(h=null,l!==void 0&&(t(l),h=""+l),E(s)&&(t(s.key),h=""+s.key),"key"in s){l={};for(var G in s)G!=="key"&&(l[G]=s[G])}else l=s;return h&&b(l,typeof e=="function"?e.displayName||e.name||"Unknown":e),w(e,h,k,T,c(),l,z,Q)}function M(e){typeof e=="object"&&e!==null&&e.$$typeof===Y&&e._store&&(e._store.validated=1)}var I=f,Y=Symbol.for("react.transitional.element"),J=Symbol.for("react.portal"),g=Symbol.for("react.fragment"),A=Symbol.for("react.strict_mode"),_=Symbol.for("react.profiler"),F=Symbol.for("react.consumer"),q=Symbol.for("react.context"),i=Symbol.for("react.forward_ref"),W=Symbol.for("react.suspense"),$=Symbol.for("react.suspense_list"),n=Symbol.for("react.memo"),m=Symbol.for("react.lazy"),U=Symbol.for("react.activity"),V=Symbol.for("react.client.reference"),v=I.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,P=Object.prototype.hasOwnProperty,O=Array.isArray,u=console.createTask?console.createTask:function(){return null};I={react_stack_bottom_frame:function(e){return e()}};var D,Z={},K=I.react_stack_bottom_frame.bind(I,d)(),ee=u(a(d)),re={};N.Fragment=g,N.jsx=function(e,s,l,p,T){var k=1e4>v.recentlyCreatedOwnerStacks++;return C(e,s,l,!1,p,T,k?Error("react-stack-top-frame"):K,k?u(a(e)):ee)},N.jsxs=function(e,s,l,p,T){var k=1e4>v.recentlyCreatedOwnerStacks++;return C(e,s,l,!0,p,T,k?Error("react-stack-top-frame"):K,k?u(a(e)):ee)}})()),N}var H;function oe(){return H||(H=1,process.env.NODE_ENV==="production"?L.exports=ne():L.exports=te()),L.exports}var se=oe();class ie extends EventTarget{subscriptions=new Map;targetOrigin="*";matchesFilter(r,t){return!t||t==="*"||t===null||t===void 0?!0:typeof t=="string"?r===t:Array.isArray(t)?t.includes(r||""):typeof t=="function"?t(r||""):!1}getFormIdFromPayload(r){return r?.feedbackConfigurationId}publish(r,t,a){typeof window<"u"&&window.dispatchEvent(new CustomEvent(r,{detail:t,bubbles:!0})),a?.usePostMessage!==!1&&this.sendPostMessage(r,t)}subscribe(r,t,a){const c=Symbol("subscription"),d={eventType:r,handler:t,filter:a?.formId,id:c,once:a?.once};this.subscriptions.has(r)||this.subscriptions.set(r,[]),this.subscriptions.get(r).push(d);const E=b=>{const R=b,w=this.getFormIdFromPayload(R.detail);this.matchesFilter(w,a?.formId)&&(t(R.detail),a?.once&&typeof window<"u"&&window.removeEventListener(r,E))};return typeof window<"u"&&window.addEventListener(r,E),()=>{this.unsubscribe(c),typeof window<"u"&&window.removeEventListener(r,E)}}unsubscribe(r){for(const[t,a]of this.subscriptions.entries()){const c=a.findIndex(d=>d.id===r);if(c!==-1){a.splice(c,1),a.length===0&&this.subscriptions.delete(t);break}}}subscribeAll(r,t){const a=["form:show","form:started","form:submit","form:close","form:section:change","form:question:answered","form:error"],c=[];return a.forEach(d=>{const E=this.subscribe(d,b=>r(d,b),t);c.push(E)}),()=>{c.forEach(d=>d())}}getSubscriptionCount(r){return r?this.subscriptions.get(r)?.length||0:Array.from(this.subscriptions.values()).reduce((t,a)=>t+a.length,0)}clearSubscriptions(r){r?this.subscriptions.delete(r):this.subscriptions.clear()}sendPostMessage(r,t){const a={type:`encatch:${r}`,payload:t,source:"encatch-form-engine"};window.parent&&window.parent!==window&&window.parent.postMessage(a,this.targetOrigin),document.querySelectorAll("iframe").forEach(c=>{c.contentWindow&&c.contentWindow.postMessage(a,this.targetOrigin)}),typeof window.ReactNativeWebView<"u"&&window.ReactNativeWebView.postMessage(JSON.stringify({action:r.replace("form:",""),data:JSON.stringify(t)}))}}const y=new ie,ue=({formData:o,formConfig:r,formPageUrl:t,formId:a,cssLink:c,scale:d=100,persistMode:E="none",instanceId:b,onFormEvent:R})=>{const w=!!(r&&t),C=a??r?.formId??b??"preview",[M,I]=f.useState(!1),[Y,J]=f.useState(!0),g=f.useRef(null),A=f.useRef(null),_=f.useRef(null),F=f.useRef(!1),q=f.useRef(()=>{}),i=f.useRef({});f.useEffect(()=>{if(!R)return;R({onSubmit:m=>{i.current.onSubmit=m},onShow:m=>{i.current.onShow=m},onClose:m=>{i.current.onClose=m},onSectionChange:m=>{i.current.onSectionChange=m},onQuestionAnswered:m=>{i.current.onQuestionAnswered=m},onError:m=>{i.current.onError=m}})},[R]);const W=f.useCallback(()=>{if(!_.current?.contentWindow||!r)return;const n={formId:C,...r};_.current.contentWindow.postMessage({type:"sdk:formConfig",data:n},"*")},[r,C]);q.current=W,f.useEffect(()=>{!w||!F.current||q.current()},[w,r,W]),f.useEffect(()=>{if(!w||!g.current)return;const n=document.createElement("iframe"),m=new URL(t,window.location.origin);m.searchParams.set("formId",C),n.src=m.toString(),n.title="Encatch form",n.style.width="100%",n.style.height="100%",n.style.border="none",_.current=n,g.current.appendChild(n);const U=V=>{const v=V.data;if(!v||typeof v!="object"||!v.type)return;let P=v.type,O=v.formId,u=v.data;switch(P.startsWith("encatch:")&&(P=P.replace("encatch:",""),O=v.payload?.feedbackConfigurationId,u=v.payload),P){case"form:ready":F.current=!0,q.current();break;case"form:close":i.current.onClose?.({timestamp:Date.now()});break;case"form:complete":i.current.onClose?.({timestamp:Date.now()});break;case"form:submit":u&&i.current.onSubmit&&i.current.onSubmit({feedbackConfigurationId:u.feedbackConfigurationId,feedbackIdentifier:u.feedbackIdentifier,response:u.response,isPartialSubmit:u.isPartialSubmit,timestamp:Date.now()});break;case"form:show":u&&i.current.onShow&&i.current.onShow({feedbackConfigurationId:u.feedbackConfigurationId,feedbackIdentifier:u.feedbackIdentifier,timestamp:Date.now()});break;case"form:started":u&&i.current.onShow&&i.current.onShow({feedbackConfigurationId:u.feedbackConfigurationId,feedbackIdentifier:u.feedbackIdentifier,timestamp:Date.now()});break;case"form:section:change":u&&i.current.onSectionChange&&i.current.onSectionChange({feedbackConfigurationId:u.feedbackConfigurationId??O??"",sectionIndex:u.sectionIndex,sectionId:u.sectionId,timestamp:Date.now()});break;case"form:question:answered":u&&i.current.onQuestionAnswered&&i.current.onQuestionAnswered({feedbackConfigurationId:u.feedbackConfigurationId??O??"",questionId:u.questionId,questionType:u.questionType,answer:u.answer,timestamp:Date.now()});break;case"form:error":u&&i.current.onError&&i.current.onError({feedbackConfigurationId:u.feedbackConfigurationId??O??"",questionId:u.questionId,error:u.error??"Unknown error",timestamp:Date.now()});break}};return window.addEventListener("message",U),()=>{if(window.removeEventListener("message",U),F.current=!1,g.current&&_.current)try{g.current.removeChild(_.current)}catch{}_.current=null}},[w,t,C]),f.useEffect(()=>{if(w||!b)return;const n=[];return i.current.onSubmit&&n.push(y.subscribe("form:submit",i.current.onSubmit,{formId:b})),i.current.onShow&&n.push(y.subscribe("form:show",i.current.onShow,{formId:b})),i.current.onClose&&n.push(y.subscribe("form:close",i.current.onClose,{formId:b})),i.current.onSectionChange&&n.push(y.subscribe("form:section:change",i.current.onSectionChange,{formId:b})),i.current.onQuestionAnswered&&n.push(y.subscribe("form:question:answered",i.current.onQuestionAnswered,{formId:b})),i.current.onError&&n.push(y.subscribe("form:error",i.current.onError,{formId:b})),()=>n.forEach(m=>m())},[w,b]),f.useEffect(()=>{if(w){I(!1);return}try{if(!o){console.warn("[EncatchPreview] formData is not defined (inline mode)");return}I(!0)}catch(n){console.error("[EncatchPreview] Error during data validation:",n)}},[w,o]);const $=f.useCallback(()=>{o?.onClose&&o.onClose(),J(!1)},[o?.onClose]);return f.useEffect(()=>{if(w||!M||!g.current||!o)return;const n=document.createElement("encatch-app");return n.theme=o.theme,n.currentMode=o.currentMode,n.currentLanguage=o.currentLanguage,n.questions=o.questions,n.questionLanguages=o.questionLanguages,n.otherConfigurationProperties=o.otherConfigurationProperties,n.welcomeScreenProperties=o.welcomeScreenProperties,n.endScreenProperties=o.endScreenProperties,n.translations=o.translations,n.sections=o.sections,n.themeSettings=o.themeSettings,n.onClose=$,n.onSectionChange=o.onSectionChange,n.apiKey=o.apiKey,n.hostUrl=o.hostUrl,n.feedbackConfigId=o.feedbackConfigId,n.identifier=o.identifier,c&&(n.cssLink=c),n.scale=d,n.persistMode=E,b&&(n.instanceId=b),A.current=n,g.current.appendChild(n),()=>{if(g.current&&A.current)try{g.current.removeChild(A.current)}catch{}A.current=null}},[w,M,o,c,d,$,E,b]),Y?se.jsx("div",{ref:g,style:w?{width:"100%",height:"100%",minHeight:400}:void 0}):null};function ce(o,r,t){f.useEffect(()=>y.subscribe(o,r,t),[o,t?.formId,r])}function ae(o,r){f.useEffect(()=>y.subscribeAll(o,r),[r?.formId,o])}S.EncatchPreview=ue,S.useEncatchFormEvent=ce,S.useEncatchFormEventAll=ae,Object.defineProperty(S,Symbol.toStringTag,{value:"Module"})}));
package/dist/types.d.ts CHANGED
@@ -1,4 +1,45 @@
1
1
  import type { FormEventPayload } from '@encatch/event-publisher';
2
+ /**
3
+ * Full form configuration object used when rendering the form in an iframe (EncatchPreview iframe mode).
4
+ * Matches the payload shape expected by the shareable iframe form page (sdk:formConfig).
5
+ * EncatchPreview sends this via postMessage when formPageUrl is provided.
6
+ */
7
+ export interface EncatchFormConfig {
8
+ formId?: string;
9
+ questionnaireFields?: {
10
+ questions?: Record<string, unknown>;
11
+ sections?: unknown[];
12
+ selectedLanguages?: Array<{
13
+ value: string;
14
+ label: string;
15
+ isFixed?: boolean;
16
+ }>;
17
+ translations?: Record<string, unknown>;
18
+ };
19
+ appearanceProperties?: {
20
+ themes?: {
21
+ light?: {
22
+ theme?: string;
23
+ };
24
+ dark?: {
25
+ theme?: string;
26
+ };
27
+ };
28
+ featureSettings?: {
29
+ closeButton?: boolean;
30
+ shareableMode?: string;
31
+ };
32
+ };
33
+ welcomeScreenProperties?: unknown;
34
+ endScreenProperties?: unknown;
35
+ otherConfigurationProperties?: unknown;
36
+ feedbackConfigurationId?: string;
37
+ feedbackIdentifier?: string;
38
+ formConfiguration?: {
39
+ formTitle?: string;
40
+ };
41
+ [key: string]: unknown;
42
+ }
2
43
  /**
3
44
  * Callback function for form submit events
4
45
  */
@@ -85,3 +126,13 @@ export interface FormEventBuilder {
85
126
  * Function type for the onFormEvent prop
86
127
  */
87
128
  export type OnFormEventHandler = (formEvent: FormEventBuilder) => void;
129
+ /**
130
+ * Props for EncatchPreview when used in iframe mode.
131
+ * Pass formConfig and formPageUrl to render the form inside an iframe;
132
+ * formPageUrl should point at the shareable encatch-preview-form route (e.g. origin + /encatch-preview-form).
133
+ */
134
+ export interface EncatchPreviewIframeModeProps {
135
+ formConfig: EncatchFormConfig;
136
+ formPageUrl: string;
137
+ formId?: string;
138
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@encatch/ws-react",
3
- "version": "0.0.50-beta.0",
3
+ "version": "0.0.50-beta.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.umd.js",
6
6
  "module": "./dist/index.es.js",
@@ -20,7 +20,7 @@
20
20
  "react-dom": "^19.1.1"
21
21
  },
22
22
  "dependencies": {
23
- "@encatch/schema": "1.0.0-beta.0",
23
+ "@encatch/schema": "1.0.0",
24
24
  "@encatch/event-publisher": "1.0.0-beta.1"
25
25
  },
26
26
  "devDependencies": {