@wix/headless-forms 0.0.10 → 0.0.11
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 +143 -18
- package/cjs/dist/react/Form.js +211 -19
- package/cjs/dist/react/core/Form.d.ts +80 -5
- package/cjs/dist/react/core/Form.js +41 -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 +2 -0
- package/cjs/dist/services/form-service.js +5 -2
- package/dist/react/Form.d.ts +143 -18
- package/dist/react/Form.js +212 -20
- package/dist/react/core/Form.d.ts +80 -5
- package/dist/react/core/Form.js +40 -0
- package/dist/react/utils.d.ts +13 -0
- package/dist/react/utils.js +17 -0
- package/dist/services/form-service.d.ts +2 -0
- package/dist/services/form-service.js +5 -2
- package/package.json +3 -3
|
@@ -135,14 +135,14 @@ export interface FormErrorRenderProps {
|
|
|
135
135
|
*/
|
|
136
136
|
export declare function LoadingError(props: FormErrorProps): import("react").ReactNode;
|
|
137
137
|
/**
|
|
138
|
-
* Props for Form Error headless component
|
|
138
|
+
* Props for Form Submit Error headless component
|
|
139
139
|
*/
|
|
140
140
|
export interface FormSubmitErrorProps {
|
|
141
141
|
/** Render prop function that receives submit error state data */
|
|
142
142
|
children: (props: FormSubmitErrorRenderProps) => React.ReactNode;
|
|
143
143
|
}
|
|
144
144
|
/**
|
|
145
|
-
* Render props for Form Error component
|
|
145
|
+
* Render props for Form Submit Error component
|
|
146
146
|
*/
|
|
147
147
|
export interface FormSubmitErrorRenderProps {
|
|
148
148
|
/** Submit error message */
|
|
@@ -222,14 +222,17 @@ export declare function Submitted(props: FormSubmittedProps): import("react").Re
|
|
|
222
222
|
/**
|
|
223
223
|
* Render props for Fields component
|
|
224
224
|
*/
|
|
225
|
-
interface FieldsRenderProps {
|
|
225
|
+
export interface FieldsRenderProps {
|
|
226
|
+
/** The form data, or null if not loaded */
|
|
226
227
|
form: forms.Form | null;
|
|
228
|
+
/** Function to submit the form with values */
|
|
227
229
|
submitForm: (formValues: FormValues) => Promise<void>;
|
|
228
230
|
}
|
|
229
231
|
/**
|
|
230
232
|
* Props for Fields headless component
|
|
231
233
|
*/
|
|
232
|
-
interface FieldsProps {
|
|
234
|
+
export interface FieldsProps {
|
|
235
|
+
/** Render prop function that receives form data and submit handler */
|
|
233
236
|
children: (props: FieldsRenderProps) => React.ReactNode;
|
|
234
237
|
}
|
|
235
238
|
/**
|
|
@@ -264,4 +267,76 @@ interface FieldsProps {
|
|
|
264
267
|
* ```
|
|
265
268
|
*/
|
|
266
269
|
export declare function Fields(props: FieldsProps): import("react").ReactNode;
|
|
267
|
-
|
|
270
|
+
/**
|
|
271
|
+
* Form view interface containing field definitions
|
|
272
|
+
*/
|
|
273
|
+
export interface FormView {
|
|
274
|
+
fields: FieldDefinition[];
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Field layout configuration
|
|
278
|
+
*/
|
|
279
|
+
export interface Layout {
|
|
280
|
+
column: number;
|
|
281
|
+
row: number;
|
|
282
|
+
height: number;
|
|
283
|
+
width: number;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Field definition including layout information
|
|
287
|
+
*/
|
|
288
|
+
export interface FieldDefinition {
|
|
289
|
+
id: string;
|
|
290
|
+
layout: Layout;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Render props for Field component
|
|
294
|
+
*/
|
|
295
|
+
export interface FieldRenderProps {
|
|
296
|
+
/** The field ID */
|
|
297
|
+
id: string;
|
|
298
|
+
/** The field layout configuration */
|
|
299
|
+
layout: Layout;
|
|
300
|
+
/** Grid styles for container */
|
|
301
|
+
gridStyles: {
|
|
302
|
+
label: React.CSSProperties;
|
|
303
|
+
input: React.CSSProperties;
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Props for Field headless component
|
|
308
|
+
*/
|
|
309
|
+
export interface FieldProps {
|
|
310
|
+
/** The unique identifier for this field */
|
|
311
|
+
id: string;
|
|
312
|
+
/** The field layout configuration */
|
|
313
|
+
layout: Layout;
|
|
314
|
+
/** Render prop function that receives field layout data */
|
|
315
|
+
children: (props: FieldRenderProps) => React.ReactNode;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Headless Field component that provides field layout data and grid styles.
|
|
319
|
+
* This component accesses field configuration and calculates grid positioning.
|
|
320
|
+
*
|
|
321
|
+
* @component
|
|
322
|
+
* @param {FieldProps} props - Component props
|
|
323
|
+
* @param {FieldProps['children']} props.children - Render prop function that receives field layout data
|
|
324
|
+
* @example
|
|
325
|
+
* ```tsx
|
|
326
|
+
* import { Form } from '@wix/headless-forms/react';
|
|
327
|
+
*
|
|
328
|
+
* function CustomField({ id, layout }) {
|
|
329
|
+
* return (
|
|
330
|
+
* <Form.Field id={id} layout={layout}>
|
|
331
|
+
* {({ id, layout, gridStyles }) => (
|
|
332
|
+
* <div data-field-id={id}>
|
|
333
|
+
* <div style={gridStyles.label}>Label</div>
|
|
334
|
+
* <div style={gridStyles.input}>Input</div>
|
|
335
|
+
* </div>
|
|
336
|
+
* )}
|
|
337
|
+
* </Form.Field>
|
|
338
|
+
* );
|
|
339
|
+
* }
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
export declare function Field(props: FieldProps): import("react").ReactNode;
|
package/dist/react/core/Form.js
CHANGED
|
@@ -2,6 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { useService, WixServices } from '@wix/services-manager-react';
|
|
3
3
|
import { createServicesMap } from '@wix/services-manager';
|
|
4
4
|
import { FormServiceDefinition, FormService, } from '../../services/form-service.js';
|
|
5
|
+
import { calculateGridStyles } from '../utils.js';
|
|
5
6
|
const DEFAULT_SUCCESS_MESSAGE = 'Your form has been submitted successfully.';
|
|
6
7
|
/**
|
|
7
8
|
* Root component that provides the Form service context to its children.
|
|
@@ -227,3 +228,42 @@ export function Fields(props) {
|
|
|
227
228
|
submitForm,
|
|
228
229
|
});
|
|
229
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Headless Field component that provides field layout data and grid styles.
|
|
233
|
+
* This component accesses field configuration and calculates grid positioning.
|
|
234
|
+
*
|
|
235
|
+
* @component
|
|
236
|
+
* @param {FieldProps} props - Component props
|
|
237
|
+
* @param {FieldProps['children']} props.children - Render prop function that receives field layout data
|
|
238
|
+
* @example
|
|
239
|
+
* ```tsx
|
|
240
|
+
* import { Form } from '@wix/headless-forms/react';
|
|
241
|
+
*
|
|
242
|
+
* function CustomField({ id, layout }) {
|
|
243
|
+
* return (
|
|
244
|
+
* <Form.Field id={id} layout={layout}>
|
|
245
|
+
* {({ id, layout, gridStyles }) => (
|
|
246
|
+
* <div data-field-id={id}>
|
|
247
|
+
* <div style={gridStyles.label}>Label</div>
|
|
248
|
+
* <div style={gridStyles.input}>Input</div>
|
|
249
|
+
* </div>
|
|
250
|
+
* )}
|
|
251
|
+
* </Form.Field>
|
|
252
|
+
* );
|
|
253
|
+
* }
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
export function Field(props) {
|
|
257
|
+
const { id, children, layout } = props;
|
|
258
|
+
const { formSignal } = useService(FormServiceDefinition);
|
|
259
|
+
const form = formSignal.get();
|
|
260
|
+
if (!form) {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
const gridStyles = calculateGridStyles(layout);
|
|
264
|
+
return children({
|
|
265
|
+
id,
|
|
266
|
+
layout,
|
|
267
|
+
gridStyles,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Layout } from './core/Form';
|
|
2
|
+
export declare function calculateGridStyles(layout: Layout): {
|
|
3
|
+
label: {
|
|
4
|
+
gridRow: string;
|
|
5
|
+
gridColumn: string;
|
|
6
|
+
display: string;
|
|
7
|
+
alignItems: string;
|
|
8
|
+
};
|
|
9
|
+
input: {
|
|
10
|
+
gridRow: string;
|
|
11
|
+
gridColumn: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function calculateGridStyles(layout) {
|
|
2
|
+
const labelRow = 1;
|
|
3
|
+
const inputRow = 2;
|
|
4
|
+
const gridColumn = `${layout.column + 1} / span ${layout.width}`;
|
|
5
|
+
return {
|
|
6
|
+
label: {
|
|
7
|
+
gridRow: `${labelRow} / span 1`,
|
|
8
|
+
gridColumn,
|
|
9
|
+
display: 'flex',
|
|
10
|
+
alignItems: 'flex-end',
|
|
11
|
+
},
|
|
12
|
+
input: {
|
|
13
|
+
gridRow: `${inputRow} / span 1`,
|
|
14
|
+
gridColumn,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -67,7 +67,10 @@ export const FormService = implementService.withConfig()(FormServiceDefinition,
|
|
|
67
67
|
}
|
|
68
68
|
async function defaultSubmitHandler(formId, formValues) {
|
|
69
69
|
try {
|
|
70
|
-
await submissions.createSubmission({
|
|
70
|
+
await submissions.createSubmission({
|
|
71
|
+
formId,
|
|
72
|
+
submissions: formValues,
|
|
73
|
+
});
|
|
71
74
|
// TODO: add message
|
|
72
75
|
return { type: 'success' };
|
|
73
76
|
}
|
|
@@ -88,7 +91,7 @@ export const FormService = implementService.withConfig()(FormServiceDefinition,
|
|
|
88
91
|
}
|
|
89
92
|
// @ts-expect-error
|
|
90
93
|
const formId = form._id ? form._id : form.id;
|
|
91
|
-
submitResponseSignal.set({ type: '
|
|
94
|
+
submitResponseSignal.set({ type: 'loading' });
|
|
92
95
|
try {
|
|
93
96
|
const handler = config.onSubmit || defaultSubmitHandler;
|
|
94
97
|
const response = await handler(formId, formValues);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/headless-forms",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
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.
|
|
45
|
-
"@wix/forms": "^1.0.
|
|
44
|
+
"@wix/form-public": "^0.40.0",
|
|
45
|
+
"@wix/forms": "^1.0.331",
|
|
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"
|