@encatch/ws-react 0.0.17 → 0.0.19

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,9 +1,10 @@
1
1
  # @encatch/ws-react
2
2
 
3
- React wrapper for Encatch web form engine. This package provides a React component to easily integrate Encatch forms into your React applications.
3
+ 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
4
 
5
- ### Purpose
6
- Helpful when you want to manually open feedback (static forms) in your react application.
5
+ ## Purpose
6
+
7
+ Use this package when you want to manually integrate and control Encatch feedback forms (static forms) in your React application with full programmatic access to form events.
7
8
 
8
9
  ## Resources
9
10
 
@@ -19,3 +20,262 @@ pnpm add @encatch/ws-react
19
20
  # or
20
21
  yarn add @encatch/ws-react
21
22
  ```
23
+
24
+ ## Features
25
+
26
+ - **React Component**: `EncatchPreview` component for rendering forms
27
+ - **Event Hooks**: React hooks for subscribing to form events
28
+ - **Event Builder**: Declarative API for handling form lifecycle events
29
+ - **TypeScript Support**: Full type safety with TypeScript definitions
30
+ - **Automatic Cleanup**: Event subscriptions are automatically cleaned up on unmount
31
+ - **Instance Filtering**: Filter events by specific form instances
32
+
33
+ ## Components
34
+
35
+ ### EncatchPreview
36
+
37
+ The main component for rendering Encatch forms in your React application.
38
+
39
+ ```tsx
40
+ import { EncatchPreview } from '@encatch/ws-react';
41
+
42
+ function App() {
43
+ const formData = {
44
+ theme: 'default',
45
+ currentMode: 'light',
46
+ currentLanguage: 'en',
47
+ questions: [],
48
+ questionLanguages: [],
49
+ otherConfigurationProperties: {},
50
+ welcomeScreenProperties: {},
51
+ endScreenProperties: {},
52
+ translations: {},
53
+ sections: [],
54
+ themeSettings: null,
55
+ onClose: () => console.log('Form closed'),
56
+ onSectionChange: (index) => console.log('Section changed:', index),
57
+ apiKey: 'your-api-key',
58
+ hostUrl: 'https://api.encatch.com',
59
+ feedbackConfigId: 'your-config-id',
60
+ identifier: 'user-identifier',
61
+ };
62
+
63
+ return (
64
+ <EncatchPreview
65
+ formData={formData}
66
+ cssLink="https://your-styles.css"
67
+ scale={100}
68
+ persistMode="none"
69
+ instanceId="unique-form-id"
70
+ />
71
+ );
72
+ }
73
+ ```
74
+
75
+ #### Props
76
+
77
+ | Prop | Type | Required | Default | Description |
78
+ |------|------|----------|---------|-------------|
79
+ | `formData` | `object` | Yes | - | Form configuration object |
80
+ | `cssLink` | `string` | Yes | - | URL to the form's CSS stylesheet |
81
+ | `scale` | `number` | No | `100` | Scale factor for the form (percentage) |
82
+ | `persistMode` | `"retain" \| "reset" \| "none"` | No | `"none"` | Form state persistence mode |
83
+ | `instanceId` | `string` | No | - | Unique identifier for filtering events |
84
+ | `onFormEvent` | `OnFormEventHandler` | No | - | Event handler builder function |
85
+
86
+ ### Event Handling with onFormEvent
87
+
88
+ The `onFormEvent` prop provides a declarative way to handle form lifecycle events:
89
+
90
+ ```tsx
91
+ import { EncatchPreview } from '@encatch/ws-react';
92
+
93
+ function App() {
94
+ const handleFormEvents = (formEvent) => {
95
+ // Called when form is submitted
96
+ formEvent.onSubmit((data) => {
97
+ console.log('Form submitted:', data);
98
+ });
99
+
100
+ // Called when form is viewed/opened
101
+ formEvent.onView((data) => {
102
+ console.log('Form viewed:', data);
103
+ });
104
+
105
+ // Called when form is closed
106
+ formEvent.onClose((data) => {
107
+ console.log('Form closed:', data);
108
+ });
109
+
110
+ // Called when user navigates between sections
111
+ formEvent.onSectionChange((data) => {
112
+ console.log('Section changed to:', data.sectionIndex);
113
+ });
114
+
115
+ // Called when a question is answered
116
+ formEvent.onQuestionAnswered((data) => {
117
+ console.log('Question answered:', data.questionId, data.answer);
118
+ });
119
+
120
+ // Called when an error occurs
121
+ formEvent.onError((data) => {
122
+ console.log('Form error:', data.error);
123
+ });
124
+ };
125
+
126
+ return (
127
+ <EncatchPreview
128
+ formData={formData}
129
+ cssLink="https://your-styles.css"
130
+ instanceId="my-form"
131
+ onFormEvent={handleFormEvents}
132
+ />
133
+ );
134
+ }
135
+ ```
136
+
137
+ ## Hooks
138
+
139
+ ### useEncatchFormEvent
140
+
141
+ Subscribe to specific form events with automatic cleanup on unmount.
142
+
143
+ ```tsx
144
+ import { useEncatchFormEvent } from '@encatch/ws-react';
145
+
146
+ function MyComponent() {
147
+ // Subscribe to all form submit events
148
+ useEncatchFormEvent('form:submit', (payload) => {
149
+ console.log('Form submitted:', payload);
150
+ });
151
+
152
+ // Subscribe to events from a specific form instance
153
+ useEncatchFormEvent('form:submit', (payload) => {
154
+ console.log('Specific form submitted:', payload);
155
+ }, { formId: 'my-form-instance-1' });
156
+
157
+ // Subscribe once (unsubscribe after first event)
158
+ useEncatchFormEvent('form:view', (payload) => {
159
+ console.log('Form viewed:', payload);
160
+ }, { formId: 'my-form-instance-1', once: true });
161
+
162
+ return <div>Your component</div>;
163
+ }
164
+ ```
165
+
166
+ #### Available Event Types
167
+
168
+ - `form:submit` - Form submission event
169
+ - `form:view` - Form viewed/opened event
170
+ - `form:close` - Form closed event
171
+ - `form:section:change` - Section navigation event
172
+ - `form:question:answered` - Question answered event
173
+ - `form:error` - Error event
174
+
175
+ ### useEncatchFormEventAll
176
+
177
+ Subscribe to all form events with a single handler.
178
+
179
+ ```tsx
180
+ import { useEncatchFormEventAll } from '@encatch/ws-react';
181
+
182
+ function MyComponent() {
183
+ // Subscribe to all events from all forms
184
+ useEncatchFormEventAll((eventType, payload) => {
185
+ console.log(`Event ${eventType}:`, payload);
186
+ });
187
+
188
+ // Subscribe to all events from a specific form instance
189
+ useEncatchFormEventAll((eventType, payload) => {
190
+ console.log(`Event ${eventType} from form:`, payload);
191
+ }, { formId: 'my-form-instance-1' });
192
+
193
+ return <div>Your component</div>;
194
+ }
195
+ ```
196
+
197
+ ## TypeScript Support
198
+
199
+ The package includes full TypeScript definitions. Import types as needed:
200
+
201
+ ```tsx
202
+ import type {
203
+ FormEventType,
204
+ FormEventPayload,
205
+ SubscriptionOptions,
206
+ FormIdFilter,
207
+ FormEventBuilder,
208
+ OnFormEventHandler,
209
+ OnSubmitCallback,
210
+ OnViewCallback,
211
+ OnCloseCallback,
212
+ OnSectionChangeCallback,
213
+ OnQuestionAnsweredCallback,
214
+ OnErrorCallback,
215
+ } from '@encatch/ws-react';
216
+ ```
217
+
218
+ ## Persistence Modes
219
+
220
+ The `persistMode` prop controls how form state is handled:
221
+
222
+ - `"none"`: No persistence (default)
223
+ - `"retain"`: Keep form state across remounts
224
+ - `"reset"`: Clear state on remount
225
+
226
+ ## Best Practices
227
+
228
+ 1. **Use instanceId for multiple forms**: When rendering multiple forms, always provide unique `instanceId` values to filter events correctly.
229
+
230
+ 2. **Event filtering**: Use the `formId` option in hooks to subscribe only to events from specific form instances.
231
+
232
+ 3. **Choose the right API**:
233
+ - Use `onFormEvent` prop for component-scoped event handling
234
+ - Use hooks (`useEncatchFormEvent`) for app-level event handling
235
+
236
+ 4. **Memory management**: Event subscriptions are automatically cleaned up when components unmount.
237
+
238
+ ## Example: Multiple Forms
239
+
240
+ ```tsx
241
+ import { EncatchPreview, useEncatchFormEvent } from '@encatch/ws-react';
242
+
243
+ function MultiFormApp() {
244
+ // Listen to specific form events
245
+ useEncatchFormEvent('form:submit', (payload) => {
246
+ console.log('Survey submitted:', payload);
247
+ }, { formId: 'survey-form' });
248
+
249
+ useEncatchFormEvent('form:submit', (payload) => {
250
+ console.log('Feedback submitted:', payload);
251
+ }, { formId: 'feedback-form' });
252
+
253
+ return (
254
+ <>
255
+ <EncatchPreview
256
+ formData={surveyFormData}
257
+ cssLink="https://styles.css"
258
+ instanceId="survey-form"
259
+ />
260
+
261
+ <EncatchPreview
262
+ formData={feedbackFormData}
263
+ cssLink="https://styles.css"
264
+ instanceId="feedback-form"
265
+ />
266
+ </>
267
+ );
268
+ }
269
+ ```
270
+
271
+ ## Dependencies
272
+
273
+ - `react` >= 19.1.1 (peer dependency)
274
+ - `react-dom` >= 19.1.1 (peer dependency)
275
+ - `@encatch/schema` >= 0.1.31
276
+ - `@encatch/event-publisher` (internal)
277
+ - `@encatch/web-form-engine-core` (internal)
278
+
279
+ ## License
280
+
281
+ See the main repository for license information.