@encatch/ws-react 0.0.16 → 0.0.18

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.
@@ -1,5 +1,6 @@
1
1
  import { AppProps, Section } from "@encatch/schema";
2
2
  import React from "react";
3
+ import type { OnFormEventHandler } from "./types";
3
4
  declare module "react" {
4
5
  namespace JSX {
5
6
  interface IntrinsicElements {
@@ -24,6 +25,7 @@ declare module "react" {
24
25
  cssLink?: string;
25
26
  scale?: number;
26
27
  persistMode?: "retain" | "reset" | "none";
28
+ instanceId?: string;
27
29
  };
28
30
  }
29
31
  }
@@ -51,6 +53,27 @@ interface EncatchPreviewProps {
51
53
  cssLink: string;
52
54
  scale?: number;
53
55
  persistMode?: "retain" | "reset" | "none";
56
+ instanceId?: string;
57
+ /**
58
+ * Callback function that receives a FormEventBuilder to register event handlers
59
+ * Events are automatically filtered to only this form instance
60
+ *
61
+ * @example
62
+ * ```tsx
63
+ * const handleFormEvents = (formEvent) => {
64
+ * formEvent.onSubmit((data) => {
65
+ * console.log('Form submitted:', data);
66
+ * });
67
+ *
68
+ * formEvent.onQuestionAnswered((data) => {
69
+ * console.log('Question answered:', data.questionId, data.answer);
70
+ * });
71
+ * };
72
+ *
73
+ * <EncatchPreview onFormEvent={handleFormEvents} {...props} />
74
+ * ```
75
+ */
76
+ onFormEvent?: OnFormEventHandler;
54
77
  }
55
78
  export declare const EncatchPreview: React.FC<EncatchPreviewProps>;
56
79
  export {};
package/dist/index.d.ts CHANGED
@@ -1 +1,4 @@
1
1
  export { EncatchPreview } from "./EncatchPreview";
2
+ export { useEncatchFormEvent, useEncatchFormEventAll } from "./useEncatchFormEvent";
3
+ export type { FormEventType, FormEventPayload, SubscriptionOptions, FormIdFilter, } from "./useEncatchFormEvent";
4
+ export type { FormEventBuilder, OnFormEventHandler, OnSubmitCallback, OnViewCallback, OnCloseCallback, OnSectionChangeCallback, OnQuestionAnsweredCallback, OnErrorCallback, } from "./types";
@@ -0,0 +1,87 @@
1
+ import type { FormEventPayload } from '@encatch/event-publisher';
2
+ /**
3
+ * Callback function for form submit events
4
+ */
5
+ export type OnSubmitCallback = (payload: FormEventPayload['form:submit']) => void;
6
+ /**
7
+ * Callback function for form view events
8
+ */
9
+ export type OnViewCallback = (payload: FormEventPayload['form:view']) => void;
10
+ /**
11
+ * Callback function for form close events
12
+ */
13
+ export type OnCloseCallback = (payload: FormEventPayload['form:close']) => void;
14
+ /**
15
+ * Callback function for section change events
16
+ */
17
+ export type OnSectionChangeCallback = (payload: FormEventPayload['form:section:change']) => void;
18
+ /**
19
+ * Callback function for question answered events
20
+ */
21
+ export type OnQuestionAnsweredCallback = (payload: FormEventPayload['form:question:answered']) => void;
22
+ /**
23
+ * Callback function for form error events
24
+ */
25
+ export type OnErrorCallback = (payload: FormEventPayload['form:error']) => void;
26
+ /**
27
+ * Form Event Builder - provides methods to register callbacks for different form events
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * const handleFormEvents = (formEvent) => {
32
+ * formEvent.onSubmit((data) => {
33
+ * console.log('Form submitted:', data);
34
+ * });
35
+ *
36
+ * formEvent.onQuestionAnswered((data) => {
37
+ * console.log('Question answered:', data.questionId, data.answer);
38
+ * });
39
+ *
40
+ * formEvent.onSectionChange((data) => {
41
+ * console.log('Section changed to:', data.sectionIndex);
42
+ * });
43
+ * };
44
+ *
45
+ * <EncatchPreview
46
+ * instanceId="form-1"
47
+ * onFormEvent={handleFormEvents}
48
+ * {...otherProps}
49
+ * />
50
+ * ```
51
+ */
52
+ export interface FormEventBuilder {
53
+ /**
54
+ * Register a callback for form submission
55
+ * @param callback - Function to call when form is submitted
56
+ */
57
+ onSubmit: (callback: OnSubmitCallback) => void;
58
+ /**
59
+ * Register a callback for when form is viewed/opened
60
+ * @param callback - Function to call when form is viewed
61
+ */
62
+ onView: (callback: OnViewCallback) => void;
63
+ /**
64
+ * Register a callback for when form is closed
65
+ * @param callback - Function to call when form is closed
66
+ */
67
+ onClose: (callback: OnCloseCallback) => void;
68
+ /**
69
+ * Register a callback for section changes
70
+ * @param callback - Function to call when user navigates between sections
71
+ */
72
+ onSectionChange: (callback: OnSectionChangeCallback) => void;
73
+ /**
74
+ * Register a callback for when questions are answered
75
+ * @param callback - Function to call when a question is answered
76
+ */
77
+ onQuestionAnswered: (callback: OnQuestionAnsweredCallback) => void;
78
+ /**
79
+ * Register a callback for form errors
80
+ * @param callback - Function to call when an error occurs
81
+ */
82
+ onError: (callback: OnErrorCallback) => void;
83
+ }
84
+ /**
85
+ * Function type for the onFormEvent prop
86
+ */
87
+ export type OnFormEventHandler = (formEvent: FormEventBuilder) => void;
@@ -0,0 +1,50 @@
1
+ import { type FormEventType, type FormEventPayload, type SubscriptionOptions } from '@encatch/event-publisher';
2
+ /**
3
+ * React hook for subscribing to form events from the event publisher.
4
+ *
5
+ * @example
6
+ * ```tsx
7
+ * // Subscribe to all form submit events
8
+ * useEncatchFormEvent('form:submit', (payload) => {
9
+ * console.log('Form submitted:', payload);
10
+ * });
11
+ *
12
+ * // Subscribe to events from a specific form instance
13
+ * useEncatchFormEvent('form:submit', (payload) => {
14
+ * console.log('Specific form submitted:', payload);
15
+ * }, { formId: 'my-form-instance-1' });
16
+ *
17
+ * // Subscribe once
18
+ * useEncatchFormEvent('form:view', (payload) => {
19
+ * console.log('Form viewed:', payload);
20
+ * }, { formId: 'my-form-instance-1', once: true });
21
+ * ```
22
+ *
23
+ * @param eventType - The type of event to subscribe to
24
+ * @param handler - Callback function to handle the event
25
+ * @param options - Subscription options (formId filter, once flag)
26
+ * @returns void - Cleanup is handled automatically on unmount
27
+ */
28
+ export declare function useEncatchFormEvent<T extends FormEventType>(eventType: T, handler: (payload: FormEventPayload[T]) => void, options?: SubscriptionOptions): void;
29
+ /**
30
+ * React hook for subscribing to all form events.
31
+ *
32
+ * @example
33
+ * ```tsx
34
+ * // Subscribe to all events from all forms
35
+ * useEncatchFormEventAll((eventType, payload) => {
36
+ * console.log(`Event ${eventType}:`, payload);
37
+ * });
38
+ *
39
+ * // Subscribe to all events from a specific form instance
40
+ * useEncatchFormEventAll((eventType, payload) => {
41
+ * console.log(`Event ${eventType} from form:`, payload);
42
+ * }, { formId: 'my-form-instance-1' });
43
+ * ```
44
+ *
45
+ * @param handler - Callback function to handle all events
46
+ * @param options - Subscription options (formId filter, once flag)
47
+ * @returns void - Cleanup is handled automatically on unmount
48
+ */
49
+ export declare function useEncatchFormEventAll(handler: (eventType: FormEventType, payload: any) => void, options?: SubscriptionOptions): void;
50
+ export type { FormEventType, FormEventPayload, SubscriptionOptions, FormIdFilter } from '@encatch/event-publisher';