@wix/headless-forms 0.0.8 → 0.0.10

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 { forms } from '@wix/forms';
2
2
  import { type ReadOnlySignal } from '@wix/services-definitions/core-services/signals';
3
+ import { FormValues } from '../react/types.js';
3
4
  /**
4
5
  * Response type for form submission operations.
5
6
  * Represents the different states a form submission can be in.
@@ -15,7 +16,7 @@ export type SubmitResponse = {
15
16
  };
16
17
  /**
17
18
  * API interface for the Form service, providing reactive form data management.
18
- * This service handles loading and managing form data, loading state, and errors.
19
+ * This service handles loading and managing form data, loading state, errors, and submissions.
19
20
  * It supports both pre-loaded form data and lazy loading with form IDs.
20
21
  *
21
22
  * @interface FormServiceAPI
@@ -27,6 +28,10 @@ export interface FormServiceAPI {
27
28
  isLoadingSignal: ReadOnlySignal<boolean>;
28
29
  /** Reactive signal containing any error message, or null if no error */
29
30
  errorSignal: ReadOnlySignal<string | null>;
31
+ /** Reactive signal containing submission response state */
32
+ submitResponseSignal: ReadOnlySignal<SubmitResponse>;
33
+ /** Function to submit form with current values */
34
+ submitForm: (formValues: FormValues) => Promise<void>;
30
35
  }
31
36
  /**
32
37
  * Service definition for the Form service.
@@ -39,23 +44,29 @@ export declare const FormServiceDefinition: string & {
39
44
  __config: {};
40
45
  isServiceDefinition?: boolean;
41
46
  } & FormServiceAPI;
47
+ type OnSubmit = (formId: string, formValues: FormValues) => Promise<SubmitResponse>;
42
48
  /**
43
49
  * Configuration type for the Form service.
44
50
  * Supports two distinct patterns for providing form data:
45
51
  * - Pre-loaded form data (SSR/SSG scenarios)
46
52
  * - Lazy loading with form ID (client-side routing)
47
53
  *
54
+ * Optionally accepts a custom submission handler to override default behavior.
55
+ *
48
56
  * @type {FormServiceConfig}
49
57
  */
50
58
  export type FormServiceConfig = {
51
59
  formId: string;
60
+ onSubmit?: OnSubmit;
52
61
  } | {
53
62
  form: forms.Form;
63
+ onSubmit?: OnSubmit;
54
64
  };
55
65
  /**
56
- * Implementation of the Form service that manages reactive form data.
57
- * This service provides signals for form data, loading state, and error handling.
66
+ * Implementation of the Form service that manages reactive form data and submissions.
67
+ * This service provides signals for form data, loading state, error handling, and submission state.
58
68
  * It supports both pre-loaded form data and lazy loading with form IDs.
69
+ * Consumers can provide a custom submission handler via config.
59
70
  *
60
71
  * @example
61
72
  * ```tsx
@@ -64,6 +75,16 @@ export type FormServiceConfig = {
64
75
  *
65
76
  * // Lazy loading with form ID (client-side)
66
77
  * const formService = FormService.withConfig({ formId: 'form-123' });
78
+ *
79
+ * // With custom submission handler
80
+ * const formService = FormService.withConfig({
81
+ * formId: 'form-123',
82
+ * onSubmit: async (formId, formValues) => {
83
+ * // Custom submission logic
84
+ * await fetch('/api/submit', { method: 'POST', body: JSON.stringify({ formId, ...formValues }) });
85
+ * return { type: 'success', message: 'Form submitted!' };
86
+ * }
87
+ * });
67
88
  * ```
68
89
  */
69
90
  export declare const FormService: import("@wix/services-definitions").ServiceFactory<string & {
@@ -88,3 +109,4 @@ export declare const FormService: import("@wix/services-definitions").ServiceFac
88
109
  * ```
89
110
  */
90
111
  export declare function loadFormServiceConfig(formId: string): Promise<FormServiceConfig>;
112
+ export {};
@@ -1,4 +1,4 @@
1
- import { forms } from '@wix/forms';
1
+ import { forms, submissions } from '@wix/forms';
2
2
  import { defineService, implementService } from '@wix/services-definitions';
3
3
  import { SignalsServiceDefinition, } from '@wix/services-definitions/core-services/signals';
4
4
  /**
@@ -9,9 +9,10 @@ import { SignalsServiceDefinition, } from '@wix/services-definitions/core-servic
9
9
  */
10
10
  export const FormServiceDefinition = defineService('formService');
11
11
  /**
12
- * Implementation of the Form service that manages reactive form data.
13
- * This service provides signals for form data, loading state, and error handling.
12
+ * Implementation of the Form service that manages reactive form data and submissions.
13
+ * This service provides signals for form data, loading state, error handling, and submission state.
14
14
  * It supports both pre-loaded form data and lazy loading with form IDs.
15
+ * Consumers can provide a custom submission handler via config.
15
16
  *
16
17
  * @example
17
18
  * ```tsx
@@ -20,12 +21,25 @@ export const FormServiceDefinition = defineService('formService');
20
21
  *
21
22
  * // Lazy loading with form ID (client-side)
22
23
  * const formService = FormService.withConfig({ formId: 'form-123' });
24
+ *
25
+ * // With custom submission handler
26
+ * const formService = FormService.withConfig({
27
+ * formId: 'form-123',
28
+ * onSubmit: async (formId, formValues) => {
29
+ * // Custom submission logic
30
+ * await fetch('/api/submit', { method: 'POST', body: JSON.stringify({ formId, ...formValues }) });
31
+ * return { type: 'success', message: 'Form submitted!' };
32
+ * }
33
+ * });
23
34
  * ```
24
35
  */
25
36
  export const FormService = implementService.withConfig()(FormServiceDefinition, ({ getService, config }) => {
26
37
  const signalsService = getService(SignalsServiceDefinition);
27
38
  const isLoadingSignal = signalsService.signal(false);
28
39
  const errorSignal = signalsService.signal(null);
40
+ const submitResponseSignal = signalsService.signal({
41
+ type: 'idle',
42
+ });
29
43
  const hasSchema = 'form' in config;
30
44
  const formSignal = signalsService.signal(hasSchema ? config.form : null);
31
45
  if (!hasSchema) {
@@ -51,10 +65,49 @@ export const FormService = implementService.withConfig()(FormServiceDefinition,
51
65
  isLoadingSignal.set(false);
52
66
  }
53
67
  }
68
+ async function defaultSubmitHandler(formId, formValues) {
69
+ try {
70
+ await submissions.createSubmission({ formId, ...formValues });
71
+ // TODO: add message
72
+ return { type: 'success' };
73
+ }
74
+ catch (error) {
75
+ console.error('Form submission failed:', error);
76
+ return { type: 'error', message: 'Failed to submit form' };
77
+ }
78
+ }
79
+ /**
80
+ * Submits the form with the provided values.
81
+ * Uses custom handler if provided in config, otherwise uses default submission.
82
+ */
83
+ async function submitForm(formValues) {
84
+ const form = formSignal.get();
85
+ if (!form) {
86
+ console.error('Cannot submit: form not loaded');
87
+ return;
88
+ }
89
+ // @ts-expect-error
90
+ const formId = form._id ? form._id : form.id;
91
+ submitResponseSignal.set({ type: 'idle' });
92
+ try {
93
+ const handler = config.onSubmit || defaultSubmitHandler;
94
+ const response = await handler(formId, formValues);
95
+ submitResponseSignal.set(response);
96
+ }
97
+ catch (error) {
98
+ console.error('Unexpected error during submission:', error);
99
+ submitResponseSignal.set({
100
+ type: 'error',
101
+ message: 'Unexpected error during submission',
102
+ });
103
+ }
104
+ }
54
105
  return {
55
106
  formSignal: formSignal,
56
107
  isLoadingSignal: isLoadingSignal,
57
108
  errorSignal: errorSignal,
109
+ submitResponseSignal: submitResponseSignal,
110
+ submitForm: submitForm,
58
111
  };
59
112
  });
60
113
  async function fetchForm(id) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/headless-forms",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "npm run build:esm && npm run build:cjs",
@@ -41,8 +41,8 @@
41
41
  "vitest": "^3.1.4"
42
42
  },
43
43
  "dependencies": {
44
- "@wix/form-public": "^0.17.0",
45
- "@wix/forms": "^1.0.329",
44
+ "@wix/form-public": "^0.26.0",
45
+ "@wix/forms": "^1.0.330",
46
46
  "@wix/headless-utils": "0.0.4",
47
47
  "@wix/services-definitions": "^0.1.4",
48
48
  "@wix/services-manager-react": "^0.1.26"