@wix/headless-forms 0.0.1
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/cjs/dist/react/Form.d.ts +632 -0
- package/cjs/dist/react/Form.js +652 -0
- package/cjs/dist/react/constants/calling-country-codes.d.ts +242 -0
- package/cjs/dist/react/constants/calling-country-codes.js +242 -0
- package/cjs/dist/react/core/Form.d.ts +215 -0
- package/cjs/dist/react/core/Form.js +196 -0
- package/cjs/dist/react/index.d.ts +2 -0
- package/cjs/dist/react/index.js +41 -0
- package/cjs/dist/react/types.d.ts +1251 -0
- package/cjs/dist/react/types.js +2 -0
- package/cjs/dist/services/form-service.d.ts +189 -0
- package/cjs/dist/services/form-service.js +141 -0
- package/cjs/dist/services/index.d.ts +1 -0
- package/cjs/dist/services/index.js +17 -0
- package/cjs/package.json +3 -0
- package/dist/react/Form.d.ts +632 -0
- package/dist/react/Form.js +613 -0
- package/dist/react/constants/calling-country-codes.d.ts +242 -0
- package/dist/react/constants/calling-country-codes.js +241 -0
- package/dist/react/core/Form.d.ts +215 -0
- package/dist/react/core/Form.js +189 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +2 -0
- package/dist/react/types.d.ts +1251 -0
- package/dist/react/types.js +1 -0
- package/dist/services/form-service.d.ts +189 -0
- package/dist/services/form-service.js +137 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/package.json +49 -0
- package/react/package.json +4 -0
- package/services/package.json +4 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { forms } from '@wix/forms';
|
|
2
|
+
import { type Signal } from '@wix/services-definitions/core-services/signals';
|
|
3
|
+
export type SubmitResponse = {
|
|
4
|
+
type: 'success';
|
|
5
|
+
message?: string;
|
|
6
|
+
} | {
|
|
7
|
+
type: 'error';
|
|
8
|
+
message: string;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'idle';
|
|
11
|
+
};
|
|
12
|
+
export interface FormServiceAPI {
|
|
13
|
+
form: Signal<forms.Form | null>;
|
|
14
|
+
isLoading: Signal<boolean>;
|
|
15
|
+
error: Signal<string | null>;
|
|
16
|
+
submitResponse: Signal<SubmitResponse>;
|
|
17
|
+
}
|
|
18
|
+
export declare const FormServiceDefinition: string & {
|
|
19
|
+
__api: FormServiceAPI;
|
|
20
|
+
__config: {};
|
|
21
|
+
isServiceDefinition?: boolean;
|
|
22
|
+
} & FormServiceAPI;
|
|
23
|
+
/**
|
|
24
|
+
* Configuration object for initializing the Form service.
|
|
25
|
+
*
|
|
26
|
+
* The Form service supports two distinct patterns for providing form data:
|
|
27
|
+
*
|
|
28
|
+
* **Pattern 1: Pre-loaded Form Data (SSR/SSG)**
|
|
29
|
+
* - Use when you have form data available at service initialization
|
|
30
|
+
* - Recommended for server-side rendering and static site generation
|
|
31
|
+
* - Provides immediate form availability with no loading states
|
|
32
|
+
* - Better performance and SEO as form data is available immediately
|
|
33
|
+
*
|
|
34
|
+
* **Pattern 2: Lazy Loading with Form ID (Client-side)**
|
|
35
|
+
* - Use when you only have a form ID and need to load form data asynchronously
|
|
36
|
+
* - Ideal for client-side routing and dynamic form loading
|
|
37
|
+
* - Service will automatically fetch form data using the provided formId
|
|
38
|
+
* - Provides loading states and error handling during form fetch
|
|
39
|
+
*
|
|
40
|
+
* @interface FormServiceConfig
|
|
41
|
+
* @property {forms.Form} [form] - Pre-loaded form data. When provided, the service uses this data immediately without any network requests. Recommended for SSR/SSG scenarios.
|
|
42
|
+
* @property {string} [formId] - Form ID for lazy loading. When provided (and no form data), the service will fetch form data asynchronously from the Wix Forms API. Ideal for client-side routing.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```tsx
|
|
46
|
+
* // Pattern 1: Pre-loaded form data (SSR/SSG)
|
|
47
|
+
* // Server-side: Load form data first
|
|
48
|
+
* const formServiceConfigResult = await loadFormServiceConfig('form-123');
|
|
49
|
+
* if (formServiceConfigResult.type === 'success') {
|
|
50
|
+
* // Use pre-loaded form data
|
|
51
|
+
* <Form.Root formServiceConfig={formServiceConfigResult.config} />
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* // Or with direct form data
|
|
55
|
+
* const config1: FormServiceConfig = { form: myForm };
|
|
56
|
+
* <Form.Root formServiceConfig={config1} />
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```tsx
|
|
61
|
+
* // Pattern 2: Lazy loading with form ID (Client-side)
|
|
62
|
+
* // Simple formId-based loading - service handles the rest
|
|
63
|
+
* const config2: FormServiceConfig = { formId: 'form-123' };
|
|
64
|
+
* <Form.Root formServiceConfig={config2} />
|
|
65
|
+
*
|
|
66
|
+
* // With loading and error handling
|
|
67
|
+
* <Form.Root formServiceConfig={{ formId: 'form-123' }}>
|
|
68
|
+
* <Form.Loading>
|
|
69
|
+
* {({ isLoading }) => isLoading ? <div>Loading form...</div> : null}
|
|
70
|
+
* </Form.Loading>
|
|
71
|
+
* <Form.LoadingError>
|
|
72
|
+
* {({ error, hasError }) => hasError ? <div>Error: {error}</div> : null}
|
|
73
|
+
* </Form.LoadingError>
|
|
74
|
+
* <Form.Fields fieldMap={FIELD_MAP} />
|
|
75
|
+
* </Form.Root>
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```tsx
|
|
80
|
+
* // Pattern 3: Both provided (form takes precedence)
|
|
81
|
+
* // The pre-loaded form data will be used, formId is ignored
|
|
82
|
+
* const config3: FormServiceConfig = {
|
|
83
|
+
* form: myForm,
|
|
84
|
+
* formId: 'form-123' // This will be ignored
|
|
85
|
+
* };
|
|
86
|
+
* <Form.Root formServiceConfig={config3} />
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @throws {Error} Throws an error if neither form nor formId is provided
|
|
90
|
+
*/
|
|
91
|
+
export interface FormServiceConfig {
|
|
92
|
+
form?: forms.Form;
|
|
93
|
+
formId?: string;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Form service implementation that supports both pre-loaded form data and lazy loading.
|
|
97
|
+
*
|
|
98
|
+
* This service provides reactive state management for form data, loading states, errors, and submission responses.
|
|
99
|
+
* It automatically handles form loading when only a formId is provided, making it suitable for both SSR and client-side scenarios.
|
|
100
|
+
*
|
|
101
|
+
* ## Service Behavior
|
|
102
|
+
*
|
|
103
|
+
* **Configuration Resolution:**
|
|
104
|
+
* - If `form` is provided: Uses pre-loaded form data immediately (SSR/SSG pattern)
|
|
105
|
+
* - If only `formId` is provided: Loads form data asynchronously from Wix Forms API
|
|
106
|
+
* - If both are provided: Uses pre-loaded `form` data and ignores `formId`
|
|
107
|
+
* - If neither is provided: Throws an error during service initialization
|
|
108
|
+
*
|
|
109
|
+
* **Loading States:**
|
|
110
|
+
* - `isLoading`: `true` when loading form data via `formId`, `false` otherwise
|
|
111
|
+
* - `form`: `null` initially when using `formId`, populated after successful load
|
|
112
|
+
* - `error`: `null` initially, populated if form loading fails
|
|
113
|
+
* - `submitResponse`: `{ type: 'idle' }` initially, updated during form submission
|
|
114
|
+
*
|
|
115
|
+
* **Error Handling:**
|
|
116
|
+
* - Network errors during form loading are caught and stored in the `error` signal
|
|
117
|
+
* - "Form not found" errors are handled when the formId doesn't exist
|
|
118
|
+
* - All errors are logged to console for debugging
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```tsx
|
|
122
|
+
* // Service automatically handles loading states
|
|
123
|
+
* const service = useService(FormServiceDefinition);
|
|
124
|
+
*
|
|
125
|
+
* // Check loading state
|
|
126
|
+
* const isLoading = service.isLoading.get();
|
|
127
|
+
*
|
|
128
|
+
* // Access form data (null during loading)
|
|
129
|
+
* const form = service.form.get();
|
|
130
|
+
*
|
|
131
|
+
* // Check for errors
|
|
132
|
+
* const error = service.error.get();
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
136
|
+
export declare const FormService: import("@wix/services-definitions").ServiceFactory<string & {
|
|
137
|
+
__api: FormServiceAPI;
|
|
138
|
+
__config: {};
|
|
139
|
+
isServiceDefinition?: boolean;
|
|
140
|
+
} & FormServiceAPI, FormServiceConfig>;
|
|
141
|
+
export type FormServiceConfigResult = {
|
|
142
|
+
type: 'success';
|
|
143
|
+
config: FormServiceConfig;
|
|
144
|
+
} | {
|
|
145
|
+
type: 'notFound';
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Loads form service configuration by form ID.
|
|
149
|
+
*
|
|
150
|
+
* This function fetches form data from the Wix Forms API and returns a configuration
|
|
151
|
+
* object that can be used to initialize the Form service. This is the recommended approach
|
|
152
|
+
* for server-side rendering (SSR) and static site generation (SSG) scenarios.
|
|
153
|
+
*
|
|
154
|
+
* @param {string} id - The unique identifier of the form to load
|
|
155
|
+
* @returns {Promise<FormServiceConfigResult>} A promise that resolves to either:
|
|
156
|
+
* - `{ type: 'success', config: FormServiceConfig }` if the form is found and loaded successfully
|
|
157
|
+
* - `{ type: 'notFound' }` if the form doesn't exist or an error occurs during loading
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```tsx
|
|
161
|
+
* import { loadFormServiceConfig } from '@wix/headless-forms/services';
|
|
162
|
+
*
|
|
163
|
+
* // Server-side loading (Astro/SSR) - pre-loads form data
|
|
164
|
+
* const formServiceConfigResult = await loadFormServiceConfig('form-id');
|
|
165
|
+
*
|
|
166
|
+
* if (formServiceConfigResult.type === 'notFound') {
|
|
167
|
+
* return Astro.redirect('/404');
|
|
168
|
+
* }
|
|
169
|
+
*
|
|
170
|
+
* // Use pre-loaded form data
|
|
171
|
+
* const formServiceConfig = formServiceConfigResult.config;
|
|
172
|
+
* <Form.Root formServiceConfig={formServiceConfig} />
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```tsx
|
|
177
|
+
* // Alternative: Client-side loading with formId
|
|
178
|
+
* // No need to pre-load, service handles loading automatically
|
|
179
|
+
* <Form.Root formServiceConfig={{ formId: 'form-id' }}>
|
|
180
|
+
* <Form.Loading>
|
|
181
|
+
* {({ isLoading }) => isLoading ? <div>Loading...</div> : null}
|
|
182
|
+
* </Form.Loading>
|
|
183
|
+
* <Form.Fields fieldMap={FIELD_MAP} />
|
|
184
|
+
* </Form.Root>
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* @throws {Error} Logs errors to console but returns 'notFound' result instead of throwing
|
|
188
|
+
*/
|
|
189
|
+
export declare function loadFormServiceConfig(id: string): Promise<FormServiceConfigResult>;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FormService = exports.FormServiceDefinition = void 0;
|
|
4
|
+
exports.loadFormServiceConfig = loadFormServiceConfig;
|
|
5
|
+
const forms_1 = require("@wix/forms");
|
|
6
|
+
const services_definitions_1 = require("@wix/services-definitions");
|
|
7
|
+
const signals_1 = require("@wix/services-definitions/core-services/signals");
|
|
8
|
+
exports.FormServiceDefinition = (0, services_definitions_1.defineService)('formService');
|
|
9
|
+
/**
|
|
10
|
+
* Form service implementation that supports both pre-loaded form data and lazy loading.
|
|
11
|
+
*
|
|
12
|
+
* This service provides reactive state management for form data, loading states, errors, and submission responses.
|
|
13
|
+
* It automatically handles form loading when only a formId is provided, making it suitable for both SSR and client-side scenarios.
|
|
14
|
+
*
|
|
15
|
+
* ## Service Behavior
|
|
16
|
+
*
|
|
17
|
+
* **Configuration Resolution:**
|
|
18
|
+
* - If `form` is provided: Uses pre-loaded form data immediately (SSR/SSG pattern)
|
|
19
|
+
* - If only `formId` is provided: Loads form data asynchronously from Wix Forms API
|
|
20
|
+
* - If both are provided: Uses pre-loaded `form` data and ignores `formId`
|
|
21
|
+
* - If neither is provided: Throws an error during service initialization
|
|
22
|
+
*
|
|
23
|
+
* **Loading States:**
|
|
24
|
+
* - `isLoading`: `true` when loading form data via `formId`, `false` otherwise
|
|
25
|
+
* - `form`: `null` initially when using `formId`, populated after successful load
|
|
26
|
+
* - `error`: `null` initially, populated if form loading fails
|
|
27
|
+
* - `submitResponse`: `{ type: 'idle' }` initially, updated during form submission
|
|
28
|
+
*
|
|
29
|
+
* **Error Handling:**
|
|
30
|
+
* - Network errors during form loading are caught and stored in the `error` signal
|
|
31
|
+
* - "Form not found" errors are handled when the formId doesn't exist
|
|
32
|
+
* - All errors are logged to console for debugging
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* // Service automatically handles loading states
|
|
37
|
+
* const service = useService(FormServiceDefinition);
|
|
38
|
+
*
|
|
39
|
+
* // Check loading state
|
|
40
|
+
* const isLoading = service.isLoading.get();
|
|
41
|
+
*
|
|
42
|
+
* // Access form data (null during loading)
|
|
43
|
+
* const form = service.form.get();
|
|
44
|
+
*
|
|
45
|
+
* // Check for errors
|
|
46
|
+
* const error = service.error.get();
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
*/
|
|
50
|
+
exports.FormService = services_definitions_1.implementService.withConfig()(exports.FormServiceDefinition, ({ getService, config }) => {
|
|
51
|
+
const signalsService = getService(signals_1.SignalsServiceDefinition);
|
|
52
|
+
const { form: initialForm, formId } = config;
|
|
53
|
+
// Validation: ensure either form or formId is provided
|
|
54
|
+
if (!initialForm && !formId) {
|
|
55
|
+
throw new Error('FormServiceConfig must provide either "form" or "formId"');
|
|
56
|
+
}
|
|
57
|
+
const form = signalsService.signal(initialForm || null);
|
|
58
|
+
const isLoading = signalsService.signal(!!formId && !initialForm);
|
|
59
|
+
const error = signalsService.signal(null);
|
|
60
|
+
const submitResponse = signalsService.signal({ type: 'idle' });
|
|
61
|
+
// Client-side form loading for formId case
|
|
62
|
+
if (formId && !initialForm) {
|
|
63
|
+
// Load form asynchronously
|
|
64
|
+
forms_1.forms
|
|
65
|
+
.getForm(formId)
|
|
66
|
+
.then((loadedForm) => {
|
|
67
|
+
if (loadedForm) {
|
|
68
|
+
form.set(loadedForm);
|
|
69
|
+
isLoading.set(false);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
error.set('Form not found');
|
|
73
|
+
isLoading.set(false);
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
.catch((err) => {
|
|
77
|
+
console.error('Failed to load form:', err);
|
|
78
|
+
error.set('Failed to load form');
|
|
79
|
+
isLoading.set(false);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return { form, isLoading, error, submitResponse };
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* Loads form service configuration by form ID.
|
|
86
|
+
*
|
|
87
|
+
* This function fetches form data from the Wix Forms API and returns a configuration
|
|
88
|
+
* object that can be used to initialize the Form service. This is the recommended approach
|
|
89
|
+
* for server-side rendering (SSR) and static site generation (SSG) scenarios.
|
|
90
|
+
*
|
|
91
|
+
* @param {string} id - The unique identifier of the form to load
|
|
92
|
+
* @returns {Promise<FormServiceConfigResult>} A promise that resolves to either:
|
|
93
|
+
* - `{ type: 'success', config: FormServiceConfig }` if the form is found and loaded successfully
|
|
94
|
+
* - `{ type: 'notFound' }` if the form doesn't exist or an error occurs during loading
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```tsx
|
|
98
|
+
* import { loadFormServiceConfig } from '@wix/headless-forms/services';
|
|
99
|
+
*
|
|
100
|
+
* // Server-side loading (Astro/SSR) - pre-loads form data
|
|
101
|
+
* const formServiceConfigResult = await loadFormServiceConfig('form-id');
|
|
102
|
+
*
|
|
103
|
+
* if (formServiceConfigResult.type === 'notFound') {
|
|
104
|
+
* return Astro.redirect('/404');
|
|
105
|
+
* }
|
|
106
|
+
*
|
|
107
|
+
* // Use pre-loaded form data
|
|
108
|
+
* const formServiceConfig = formServiceConfigResult.config;
|
|
109
|
+
* <Form.Root formServiceConfig={formServiceConfig} />
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```tsx
|
|
114
|
+
* // Alternative: Client-side loading with formId
|
|
115
|
+
* // No need to pre-load, service handles loading automatically
|
|
116
|
+
* <Form.Root formServiceConfig={{ formId: 'form-id' }}>
|
|
117
|
+
* <Form.Loading>
|
|
118
|
+
* {({ isLoading }) => isLoading ? <div>Loading...</div> : null}
|
|
119
|
+
* </Form.Loading>
|
|
120
|
+
* <Form.Fields fieldMap={FIELD_MAP} />
|
|
121
|
+
* </Form.Root>
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @throws {Error} Logs errors to console but returns 'notFound' result instead of throwing
|
|
125
|
+
*/
|
|
126
|
+
async function loadFormServiceConfig(id) {
|
|
127
|
+
try {
|
|
128
|
+
const form = await forms_1.forms.getForm(id);
|
|
129
|
+
if (!form) {
|
|
130
|
+
return { type: 'notFound' };
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
type: 'success',
|
|
134
|
+
config: { form },
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
console.error('Failed to load form:', error);
|
|
139
|
+
return { type: 'notFound' };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './form-service.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./form-service.js"), exports);
|
package/cjs/package.json
ADDED