@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.
@@ -0,0 +1,189 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useService, WixServices } from '@wix/services-manager-react';
3
+ import { FormServiceDefinition, FormService, } from '../../services/form-service.js';
4
+ import { createServicesMap } from '@wix/services-manager';
5
+ const DEFAULT_SUCCESS_MESSAGE = 'Your form has been submitted successfully.';
6
+ /**
7
+ * Root component that provides the Form service context to its children.
8
+ * This component sets up the necessary services for rendering and managing form data.
9
+ *
10
+ * @order 1
11
+ * @component
12
+ * @param {React.ReactNode} children - Child components that will have access to form context
13
+ * @param {FormServiceConfig} formServiceConfig - Configuration object containing form data
14
+ * @example
15
+ * ```tsx
16
+ * import { Form } from '@wix/headless-forms/react';
17
+ * import { loadFormServiceConfig } from '@wix/headless-forms/services';
18
+ *
19
+ * // Pattern 1: Pre-loaded form data (SSR/SSG)
20
+ * function FormPage({ formServiceConfig }) {
21
+ * return (
22
+ * <Form.Root formServiceConfig={formServiceConfig}>
23
+ * <Form.Loading>
24
+ * {({ isLoading }) => isLoading ? <div>Loading form...</div> : null}
25
+ * </Form.Loading>
26
+ * <Form.LoadingError>
27
+ * {({ error, hasError }) => hasError ? <div>{error}</div> : null}
28
+ * </Form.LoadingError>
29
+ * <Form.Fields fieldMap={FIELD_MAP} />
30
+ * </Form.Root>
31
+ * );
32
+ * }
33
+ *
34
+ * // Pattern 2: Lazy loading with formId (Client-side)
35
+ * function DynamicFormPage({ formId }) {
36
+ * return (
37
+ * <Form.Root formServiceConfig={{ formId }}>
38
+ * <Form.Loading>
39
+ * {({ isLoading }) => isLoading ? <div>Loading form...</div> : null}
40
+ * </Form.Loading>
41
+ * <Form.LoadingError>
42
+ * {({ error, hasError }) => hasError ? <div>{error}</div> : null}
43
+ * </Form.LoadingError>
44
+ * <Form.Fields fieldMap={FIELD_MAP} />
45
+ * </Form.Root>
46
+ * );
47
+ * }
48
+ * ```
49
+ */
50
+ export function Root(props) {
51
+ return (_jsx(WixServices, { servicesMap: createServicesMap().addService(FormServiceDefinition, FormService, props.formServiceConfig), children: props.children }));
52
+ }
53
+ /**
54
+ * Headless component for form loading state access
55
+ *
56
+ * @component
57
+ * @param {FormLoadingProps} props - Component props
58
+ * @param {FormLoadingProps['children']} props.children - Render prop function that receives loading state
59
+ * @example
60
+ * ```tsx
61
+ * import { Form } from '@wix/headless-forms/react';
62
+ *
63
+ * function FormLoadingIndicator() {
64
+ * return (
65
+ * <Form.Loading>
66
+ * {({ isLoading }) => (
67
+ * isLoading ? (
68
+ * <div>Loading form...</div>
69
+ * ) : null
70
+ * )}
71
+ * </Form.Loading>
72
+ * );
73
+ * }
74
+ * ```
75
+ */
76
+ export function Loading(props) {
77
+ const service = useService(FormServiceDefinition);
78
+ const isLoading = service.isLoading?.get() || false;
79
+ return props.children({
80
+ isLoading,
81
+ });
82
+ }
83
+ /**
84
+ * Headless component for form error state access
85
+ *
86
+ * @component
87
+ * @param {FormErrorProps} props - Component props
88
+ * @param {FormErrorProps['children']} props.children - Render prop function that receives error state
89
+ * @example
90
+ * ```tsx
91
+ * import { Form } from '@wix/headless-forms/react';
92
+ *
93
+ * function FormErrorDisplay() {
94
+ * return (
95
+ * <Form.LoadingError>
96
+ * {({ error, hasError }) => (
97
+ * hasError ? (
98
+ * <div className="error-message">
99
+ * {error}
100
+ * </div>
101
+ * ) : null
102
+ * )}
103
+ * </Form.LoadingError>
104
+ * );
105
+ * }
106
+ * ```
107
+ */
108
+ export function LoadingError(props) {
109
+ const service = useService(FormServiceDefinition);
110
+ const error = service.error?.get() || null;
111
+ const hasError = !!error;
112
+ return props.children({
113
+ error,
114
+ hasError,
115
+ });
116
+ }
117
+ /**
118
+ * Headless component for form submit error state access
119
+ *
120
+ * @component
121
+ * @param {FormSubmitErrorProps} props - Component props
122
+ * @param {FormSubmitErrorProps['children']} props.children - Render prop function that receives submit error state
123
+ * @example
124
+ * ```tsx
125
+ * import { Form } from '@wix/headless-forms/react';
126
+ *
127
+ * function FormSubmitErrorDisplay() {
128
+ * return (
129
+ * <Form.Error>
130
+ * {({ error, hasError }) => (
131
+ * hasError ? (
132
+ * <div className="error-message">
133
+ * {error}
134
+ * </div>
135
+ * ) : null
136
+ * )}
137
+ * </Form.Error>
138
+ * );
139
+ * }
140
+ * ```
141
+ */
142
+ export function Error(props) {
143
+ const service = useService(FormServiceDefinition);
144
+ const submitResponse = service.submitResponse?.get() || { type: 'idle' };
145
+ const error = submitResponse.type === 'error' ? submitResponse.message : null;
146
+ const hasError = submitResponse.type === 'error';
147
+ return props.children({
148
+ error,
149
+ hasError,
150
+ });
151
+ }
152
+ /**
153
+ * Headless component for form submission state access
154
+ *
155
+ * @component
156
+ * @param {FormSubmittedProps} props - Component props
157
+ * @param {FormSubmittedProps['children']} props.children - Render prop function that receives submission state
158
+ * @example
159
+ * ```tsx
160
+ * import { Form } from '@wix/headless-forms/react';
161
+ *
162
+ * function FormSubmittedDisplay() {
163
+ * return (
164
+ * <Form.Submitted>
165
+ * {({ isSubmitted, message }) => (
166
+ * isSubmitted ? (
167
+ * <div className="success-message">
168
+ * <h2>Thank You!</h2>
169
+ * <p>{message}</p>
170
+ * </div>
171
+ * ) : null
172
+ * )}
173
+ * </Form.Submitted>
174
+ * );
175
+ * }
176
+ * ```
177
+ */
178
+ export function Submitted(props) {
179
+ const service = useService(FormServiceDefinition);
180
+ const submitResponse = service.submitResponse?.get() || { type: 'idle' };
181
+ const isSubmitted = submitResponse.type === 'success';
182
+ const message = submitResponse.type === 'success'
183
+ ? submitResponse.message || DEFAULT_SUCCESS_MESSAGE
184
+ : DEFAULT_SUCCESS_MESSAGE;
185
+ return props.children({
186
+ isSubmitted,
187
+ message,
188
+ });
189
+ }
@@ -0,0 +1,2 @@
1
+ export * as Form from './Form.js';
2
+ export * from './types.js';
@@ -0,0 +1,2 @@
1
+ export * as Form from './Form.js';
2
+ export * from './types.js';