@wix/headless-forms 0.0.0
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 +46 -0
- package/cjs/dist/react/Form.d.ts +802 -0
- package/cjs/dist/react/Form.js +776 -0
- package/cjs/dist/react/Phone.d.ts +47 -0
- package/cjs/dist/react/Phone.js +56 -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/context/FieldContext.d.ts +12 -0
- package/cjs/dist/react/context/FieldContext.js +16 -0
- package/cjs/dist/react/context/FieldLayoutContext.d.ts +12 -0
- package/cjs/dist/react/context/FieldLayoutContext.js +21 -0
- package/cjs/dist/react/core/Form.d.ts +342 -0
- package/cjs/dist/react/core/Form.js +278 -0
- package/cjs/dist/react/index.d.ts +3 -0
- package/cjs/dist/react/index.js +42 -0
- package/cjs/dist/react/types.d.ts +3 -0
- package/cjs/dist/react/types.js +2 -0
- package/cjs/dist/react/utils.d.ts +13 -0
- package/cjs/dist/react/utils.js +20 -0
- package/cjs/dist/services/form-service.d.ts +114 -0
- package/cjs/dist/services/form-service.js +152 -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 +802 -0
- package/dist/react/Form.js +740 -0
- package/dist/react/Phone.d.ts +47 -0
- package/dist/react/Phone.js +50 -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/context/FieldContext.d.ts +12 -0
- package/dist/react/context/FieldContext.js +9 -0
- package/dist/react/context/FieldLayoutContext.d.ts +12 -0
- package/dist/react/context/FieldLayoutContext.js +13 -0
- package/dist/react/core/Form.d.ts +342 -0
- package/dist/react/core/Form.js +269 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.js +3 -0
- package/dist/react/types.d.ts +3 -0
- package/dist/react/types.js +1 -0
- package/dist/react/utils.d.ts +13 -0
- package/dist/react/utils.js +17 -0
- package/dist/services/form-service.d.ts +114 -0
- package/dist/services/form-service.js +148 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/package.json +62 -0
- package/react/package.json +4 -0
- package/services/package.json +4 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateGridStyles = calculateGridStyles;
|
|
4
|
+
function calculateGridStyles(layout) {
|
|
5
|
+
const labelRow = 1;
|
|
6
|
+
const inputRow = 2;
|
|
7
|
+
const gridColumn = `${layout.column + 1} / span ${layout.width}`;
|
|
8
|
+
return {
|
|
9
|
+
label: {
|
|
10
|
+
gridRow: `${labelRow} / span 1`,
|
|
11
|
+
gridColumn,
|
|
12
|
+
display: 'flex',
|
|
13
|
+
alignItems: 'flex-end',
|
|
14
|
+
},
|
|
15
|
+
input: {
|
|
16
|
+
gridRow: `${inputRow} / span 1`,
|
|
17
|
+
gridColumn,
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { forms } from '@wix/forms';
|
|
2
|
+
import { type ReadOnlySignal } from '@wix/services-definitions/core-services/signals';
|
|
3
|
+
import { FormValues } from '../react/types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Response type for form submission operations.
|
|
6
|
+
* Represents the different states a form submission can be in.
|
|
7
|
+
*/
|
|
8
|
+
export type SubmitResponse = {
|
|
9
|
+
type: 'success';
|
|
10
|
+
message?: string;
|
|
11
|
+
} | {
|
|
12
|
+
type: 'error';
|
|
13
|
+
message: string;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'idle';
|
|
16
|
+
} | {
|
|
17
|
+
type: 'loading';
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* API interface for the Form service, providing reactive form data management.
|
|
21
|
+
* This service handles loading and managing form data, loading state, errors, and submissions.
|
|
22
|
+
* It supports both pre-loaded form data and lazy loading with form IDs.
|
|
23
|
+
*
|
|
24
|
+
* @interface FormServiceAPI
|
|
25
|
+
*/
|
|
26
|
+
export interface FormServiceAPI {
|
|
27
|
+
/** Reactive signal containing the current form data */
|
|
28
|
+
formSignal: ReadOnlySignal<forms.Form | null>;
|
|
29
|
+
/** Reactive signal indicating if a form is currently being loaded */
|
|
30
|
+
isLoadingSignal: ReadOnlySignal<boolean>;
|
|
31
|
+
/** Reactive signal containing any error message, or null if no error */
|
|
32
|
+
errorSignal: ReadOnlySignal<string | null>;
|
|
33
|
+
/** Reactive signal containing submission response state */
|
|
34
|
+
submitResponseSignal: ReadOnlySignal<SubmitResponse>;
|
|
35
|
+
/** Function to submit form with current values */
|
|
36
|
+
submitForm: (formValues: FormValues) => Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Service definition for the Form service.
|
|
40
|
+
* This defines the contract that the FormService must implement.
|
|
41
|
+
*
|
|
42
|
+
* @constant
|
|
43
|
+
*/
|
|
44
|
+
export declare const FormServiceDefinition: string & {
|
|
45
|
+
__api: FormServiceAPI;
|
|
46
|
+
__config: {};
|
|
47
|
+
isServiceDefinition?: boolean;
|
|
48
|
+
} & FormServiceAPI;
|
|
49
|
+
type OnSubmit = (formId: string, formValues: FormValues) => Promise<SubmitResponse>;
|
|
50
|
+
/**
|
|
51
|
+
* Configuration type for the Form service.
|
|
52
|
+
* Supports two distinct patterns for providing form data:
|
|
53
|
+
* - Pre-loaded form data (SSR/SSG scenarios)
|
|
54
|
+
* - Lazy loading with form ID (client-side routing)
|
|
55
|
+
*
|
|
56
|
+
* Optionally accepts a custom submission handler to override default behavior.
|
|
57
|
+
*
|
|
58
|
+
* @type {FormServiceConfig}
|
|
59
|
+
*/
|
|
60
|
+
export type FormServiceConfig = {
|
|
61
|
+
formId: string;
|
|
62
|
+
onSubmit?: OnSubmit;
|
|
63
|
+
} | {
|
|
64
|
+
form: forms.Form;
|
|
65
|
+
onSubmit?: OnSubmit;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Implementation of the Form service that manages reactive form data and submissions.
|
|
69
|
+
* This service provides signals for form data, loading state, error handling, and submission state.
|
|
70
|
+
* It supports both pre-loaded form data and lazy loading with form IDs.
|
|
71
|
+
* Consumers can provide a custom submission handler via config.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```tsx
|
|
75
|
+
* // Pre-loaded form data (SSR/SSG)
|
|
76
|
+
* const formService = FormService.withConfig({ form: formData });
|
|
77
|
+
*
|
|
78
|
+
* // Lazy loading with form ID (client-side)
|
|
79
|
+
* const formService = FormService.withConfig({ formId: 'form-123' });
|
|
80
|
+
*
|
|
81
|
+
* // With custom submission handler
|
|
82
|
+
* const formService = FormService.withConfig({
|
|
83
|
+
* formId: 'form-123',
|
|
84
|
+
* onSubmit: async (formId, formValues) => {
|
|
85
|
+
* // Custom submission logic
|
|
86
|
+
* await fetch('/api/submit', { method: 'POST', body: JSON.stringify({ formId, ...formValues }) });
|
|
87
|
+
* return { type: 'success', message: 'Form submitted!' };
|
|
88
|
+
* }
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare const FormService: import("@wix/services-definitions").ServiceFactory<string & {
|
|
93
|
+
__api: FormServiceAPI;
|
|
94
|
+
__config: {};
|
|
95
|
+
isServiceDefinition?: boolean;
|
|
96
|
+
} & FormServiceAPI, FormServiceConfig>;
|
|
97
|
+
/**
|
|
98
|
+
* Loads form service configuration from the Wix Forms API for SSR initialization.
|
|
99
|
+
* This function is designed to be used during Server-Side Rendering (SSR) to preload
|
|
100
|
+
* a specific form by ID that will be used to configure the FormService.
|
|
101
|
+
*
|
|
102
|
+
* @param {string} formId - The unique identifier of the form to load
|
|
103
|
+
* @returns {Promise<FormServiceConfig>} Configuration object with pre-loaded form data
|
|
104
|
+
* @throws {Error} When the form cannot be loaded
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```tsx
|
|
108
|
+
* // In your SSR/SSG setup
|
|
109
|
+
* const formConfig = await loadFormServiceConfig('form-123');
|
|
110
|
+
* const formService = FormService.withConfig(formConfig);
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export declare function loadFormServiceConfig(formId: string): Promise<FormServiceConfig>;
|
|
114
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* Service definition for the Form service.
|
|
10
|
+
* This defines the contract that the FormService must implement.
|
|
11
|
+
*
|
|
12
|
+
* @constant
|
|
13
|
+
*/
|
|
14
|
+
exports.FormServiceDefinition = (0, services_definitions_1.defineService)('formService');
|
|
15
|
+
/**
|
|
16
|
+
* Implementation of the Form service that manages reactive form data and submissions.
|
|
17
|
+
* This service provides signals for form data, loading state, error handling, and submission state.
|
|
18
|
+
* It supports both pre-loaded form data and lazy loading with form IDs.
|
|
19
|
+
* Consumers can provide a custom submission handler via config.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* // Pre-loaded form data (SSR/SSG)
|
|
24
|
+
* const formService = FormService.withConfig({ form: formData });
|
|
25
|
+
*
|
|
26
|
+
* // Lazy loading with form ID (client-side)
|
|
27
|
+
* const formService = FormService.withConfig({ formId: 'form-123' });
|
|
28
|
+
*
|
|
29
|
+
* // With custom submission handler
|
|
30
|
+
* const formService = FormService.withConfig({
|
|
31
|
+
* formId: 'form-123',
|
|
32
|
+
* onSubmit: async (formId, formValues) => {
|
|
33
|
+
* // Custom submission logic
|
|
34
|
+
* await fetch('/api/submit', { method: 'POST', body: JSON.stringify({ formId, ...formValues }) });
|
|
35
|
+
* return { type: 'success', message: 'Form submitted!' };
|
|
36
|
+
* }
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
exports.FormService = services_definitions_1.implementService.withConfig()(exports.FormServiceDefinition, ({ getService, config }) => {
|
|
41
|
+
const signalsService = getService(signals_1.SignalsServiceDefinition);
|
|
42
|
+
const isLoadingSignal = signalsService.signal(false);
|
|
43
|
+
const errorSignal = signalsService.signal(null);
|
|
44
|
+
const submitResponseSignal = signalsService.signal({
|
|
45
|
+
type: 'idle',
|
|
46
|
+
});
|
|
47
|
+
const hasSchema = 'form' in config;
|
|
48
|
+
const formSignal = signalsService.signal(hasSchema ? config.form : null);
|
|
49
|
+
if (!hasSchema) {
|
|
50
|
+
loadForm(config.formId);
|
|
51
|
+
}
|
|
52
|
+
async function loadForm(id) {
|
|
53
|
+
isLoadingSignal.set(true);
|
|
54
|
+
errorSignal.set(null);
|
|
55
|
+
try {
|
|
56
|
+
const result = await fetchForm(id);
|
|
57
|
+
if (result) {
|
|
58
|
+
formSignal.set(result);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
errorSignal.set('Form not found');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
errorSignal.set('Failed to load form');
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
isLoadingSignal.set(false);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function defaultSubmitHandler(formId, formValues) {
|
|
73
|
+
try {
|
|
74
|
+
await forms_1.submissions.createSubmission({
|
|
75
|
+
formId,
|
|
76
|
+
submissions: formValues,
|
|
77
|
+
});
|
|
78
|
+
// TODO: add message
|
|
79
|
+
return { type: 'success' };
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
console.error('Form submission failed:', error);
|
|
83
|
+
return { type: 'error', message: 'Failed to submit form' };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Submits the form with the provided values.
|
|
88
|
+
* Uses custom handler if provided in config, otherwise uses default submission.
|
|
89
|
+
*/
|
|
90
|
+
async function submitForm(formValues) {
|
|
91
|
+
const form = formSignal.get();
|
|
92
|
+
if (!form) {
|
|
93
|
+
console.error('Cannot submit: form not loaded');
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
// @ts-expect-error
|
|
97
|
+
const formId = form._id ? form._id : form.id;
|
|
98
|
+
submitResponseSignal.set({ type: 'loading' });
|
|
99
|
+
try {
|
|
100
|
+
const handler = config.onSubmit || defaultSubmitHandler;
|
|
101
|
+
const response = await handler(formId, formValues);
|
|
102
|
+
submitResponseSignal.set(response);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.error('Unexpected error during submission:', error);
|
|
106
|
+
submitResponseSignal.set({
|
|
107
|
+
type: 'error',
|
|
108
|
+
message: 'Unexpected error during submission',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
formSignal: formSignal,
|
|
114
|
+
isLoadingSignal: isLoadingSignal,
|
|
115
|
+
errorSignal: errorSignal,
|
|
116
|
+
submitResponseSignal: submitResponseSignal,
|
|
117
|
+
submitForm: submitForm,
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
async function fetchForm(id) {
|
|
121
|
+
try {
|
|
122
|
+
const result = await forms_1.forms.getForm(id);
|
|
123
|
+
if (!result) {
|
|
124
|
+
throw new Error(`Form ${id} not found`);
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
console.error('Failed to load form:', id, err);
|
|
130
|
+
throw err;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Loads form service configuration from the Wix Forms API for SSR initialization.
|
|
135
|
+
* This function is designed to be used during Server-Side Rendering (SSR) to preload
|
|
136
|
+
* a specific form by ID that will be used to configure the FormService.
|
|
137
|
+
*
|
|
138
|
+
* @param {string} formId - The unique identifier of the form to load
|
|
139
|
+
* @returns {Promise<FormServiceConfig>} Configuration object with pre-loaded form data
|
|
140
|
+
* @throws {Error} When the form cannot be loaded
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```tsx
|
|
144
|
+
* // In your SSR/SSG setup
|
|
145
|
+
* const formConfig = await loadFormServiceConfig('form-123');
|
|
146
|
+
* const formService = FormService.withConfig(formConfig);
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
async function loadFormServiceConfig(formId) {
|
|
150
|
+
const form = await fetchForm(formId);
|
|
151
|
+
return { form };
|
|
152
|
+
}
|
|
@@ -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