@wix/headless-forms 0.0.9 → 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.
@@ -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
- export {};
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;
@@ -6,10 +6,12 @@ exports.LoadingError = LoadingError;
6
6
  exports.Error = Error;
7
7
  exports.Submitted = Submitted;
8
8
  exports.Fields = Fields;
9
+ exports.Field = Field;
9
10
  const jsx_runtime_1 = require("react/jsx-runtime");
10
11
  const services_manager_react_1 = require("@wix/services-manager-react");
11
12
  const services_manager_1 = require("@wix/services-manager");
12
13
  const form_service_js_1 = require("../../services/form-service.js");
14
+ const utils_js_1 = require("../utils.js");
13
15
  const DEFAULT_SUCCESS_MESSAGE = 'Your form has been submitted successfully.';
14
16
  /**
15
17
  * Root component that provides the Form service context to its children.
@@ -235,3 +237,42 @@ function Fields(props) {
235
237
  submitForm,
236
238
  });
237
239
  }
240
+ /**
241
+ * Headless Field component that provides field layout data and grid styles.
242
+ * This component accesses field configuration and calculates grid positioning.
243
+ *
244
+ * @component
245
+ * @param {FieldProps} props - Component props
246
+ * @param {FieldProps['children']} props.children - Render prop function that receives field layout data
247
+ * @example
248
+ * ```tsx
249
+ * import { Form } from '@wix/headless-forms/react';
250
+ *
251
+ * function CustomField({ id, layout }) {
252
+ * return (
253
+ * <Form.Field id={id} layout={layout}>
254
+ * {({ id, layout, gridStyles }) => (
255
+ * <div data-field-id={id}>
256
+ * <div style={gridStyles.label}>Label</div>
257
+ * <div style={gridStyles.input}>Input</div>
258
+ * </div>
259
+ * )}
260
+ * </Form.Field>
261
+ * );
262
+ * }
263
+ * ```
264
+ */
265
+ function Field(props) {
266
+ const { id, children, layout } = props;
267
+ const { formSignal } = (0, services_manager_react_1.useService)(form_service_js_1.FormServiceDefinition);
268
+ const form = formSignal.get();
269
+ if (!form) {
270
+ return null;
271
+ }
272
+ const gridStyles = (0, utils_js_1.calculateGridStyles)(layout);
273
+ return children({
274
+ id,
275
+ layout,
276
+ gridStyles,
277
+ });
278
+ }
@@ -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,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
+ }
@@ -13,6 +13,8 @@ export type SubmitResponse = {
13
13
  message: string;
14
14
  } | {
15
15
  type: 'idle';
16
+ } | {
17
+ type: 'loading';
16
18
  };
17
19
  /**
18
20
  * API interface for the Form service, providing reactive form data management.
@@ -71,7 +71,10 @@ exports.FormService = services_definitions_1.implementService.withConfig()(expor
71
71
  }
72
72
  async function defaultSubmitHandler(formId, formValues) {
73
73
  try {
74
- await forms_1.submissions.createSubmission({ formId, ...formValues });
74
+ await forms_1.submissions.createSubmission({
75
+ formId,
76
+ submissions: formValues,
77
+ });
75
78
  // TODO: add message
76
79
  return { type: 'success' };
77
80
  }
@@ -92,7 +95,7 @@ exports.FormService = services_definitions_1.implementService.withConfig()(expor
92
95
  }
93
96
  // @ts-expect-error
94
97
  const formId = form._id ? form._id : form.id;
95
- submitResponseSignal.set({ type: 'idle' });
98
+ submitResponseSignal.set({ type: 'loading' });
96
99
  try {
97
100
  const handler = config.onSubmit || defaultSubmitHandler;
98
101
  const response = await handler(formId, formValues);
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
- import { type CheckboxGroupProps, type CheckboxProps, type PhoneInputProps, type DateInputProps, type DatePickerProps, type DateTimeInputProps, type DropdownProps, type FileUploadProps, type MultilineAddressProps, type NumberInputProps, type RadioGroupProps, type RatingInputProps, type RichTextProps, type SignatureProps, type SubmitButtonProps, type TagsProps, type TextAreaProps, type TextInputProps, type TimeInputProps, type ProductListProps, type FixedPaymentProps, type PaymentInputProps, type DonationProps, type AppointmentProps, type ImageChoiceProps } from './types';
3
- import { type FormServiceConfig } from '../services/form-service';
2
+ import { type CheckboxGroupProps, type CheckboxProps, type PhoneInputProps, type DateInputProps, type DatePickerProps, type DateTimeInputProps, type DropdownProps, type FileUploadProps, type MultilineAddressProps, type NumberInputProps, type RadioGroupProps, type RatingInputProps, type RichTextProps, type SignatureProps, type SubmitButtonProps, type TagsProps, type TextAreaProps, type TextInputProps, type TimeInputProps, type ProductListProps, type FixedPaymentProps, type PaymentInputProps, type DonationProps, type AppointmentProps, type ImageChoiceProps } from './types.js';
3
+ import { type FormServiceConfig } from '../services/form-service.js';
4
4
  /**
5
5
  * Props for the Form root component following the documented API
6
6
  */
@@ -303,6 +303,9 @@ export declare const Submitted: React.ForwardRefExoticComponent<SubmittedProps &
303
303
  /**
304
304
  * Mapping of form field types to their corresponding React components.
305
305
  *
306
+ * ALL field components in this map MUST use Form.Field for proper
307
+ * grid layout positioning.
308
+ *
306
309
  * Each key represents a field type identifier that matches the field types defined
307
310
  * in the form configuration, and each value is a React component that will receive
308
311
  * the field's props and render the appropriate UI element.
@@ -401,6 +404,8 @@ export interface FieldMap {
401
404
  *
402
405
  * @interface FieldsProps
403
406
  * @property {FieldMap} fieldMap - A mapping of field types to their corresponding React components
407
+ * @property {string} rowGapClassname - CSS class name for gap between rows
408
+ * @property {string} columnGapClassname - CSS class name for gap between columns
404
409
  * @example
405
410
  * ```tsx
406
411
  * const FIELD_MAP = {
@@ -414,11 +419,13 @@ export interface FieldMap {
414
419
  * // ... remaining field components
415
420
  * };
416
421
  *
417
- * <Form.Fields fieldMap={FIELD_MAP} />
422
+ * <Form.Fields fieldMap={FIELD_MAP} rowGapClassname="gap-y-4" columnGapClassname="gap-x-2" />
418
423
  * ```
419
424
  */
420
425
  interface FieldsProps {
421
426
  fieldMap: FieldMap;
427
+ rowGapClassname: string;
428
+ columnGapClassname: string;
422
429
  }
423
430
  /**
424
431
  * Fields component for rendering a form with custom field renderers.
@@ -428,6 +435,8 @@ interface FieldsProps {
428
435
  * @component
429
436
  * @param {FieldsProps} props - Component props
430
437
  * @param {FieldMap} props.fieldMap - A mapping of field types to their corresponding React components
438
+ * @param {string} props.rowGapClassname - CSS class name for gap between rows
439
+ * @param {string} props.columnGapClassname - CSS class name for gap between columns
431
440
  * @example
432
441
  * ```tsx
433
442
  * import { Form } from '@wix/headless-forms/react';
@@ -446,7 +455,11 @@ interface FieldsProps {
446
455
  * <Form.Root formServiceConfig={formServiceConfig}>
447
456
  * <Form.Loading className="flex justify-center p-4" />
448
457
  * <Form.LoadingError className="text-destructive px-4 py-3 rounded mb-4" />
449
- * <Form.Fields fieldMap={FIELD_MAP} />
458
+ * <Form.Fields
459
+ * fieldMap={FIELD_MAP}
460
+ * rowGapClassname="gap-y-4"
461
+ * columnGapClassname="gap-x-2"
462
+ * />
450
463
  * </Form.Root>
451
464
  * );
452
465
  * }
@@ -461,12 +474,15 @@ interface FieldsProps {
461
474
  * - Field validation and error display
462
475
  * - Form state management
463
476
  * - Field value updates
477
+ * - Grid layout with configurable row and column gaps
464
478
  *
465
479
  * Must be used within Form.Root to access form context.
466
480
  *
467
481
  * @component
468
482
  * @param {FieldsProps} props - The component props
469
483
  * @param {FieldMap} props.fieldMap - A mapping of field types to their corresponding React components. Each key represents a field type (e.g., 'TEXT_INPUT', 'CHECKBOX') and the value is the React component that should render that field type.
484
+ * @param {string} props.rowGapClassname - CSS class name for gap between form rows
485
+ * @param {string} props.columnGapClassname - CSS class name for gap between form columns
470
486
  *
471
487
  * @example
472
488
  * ```tsx
@@ -534,7 +550,11 @@ interface FieldsProps {
534
550
  * <Form.Root formServiceConfig={formServiceConfig}>
535
551
  * <Form.Loading className="flex justify-center p-4" />
536
552
  * <Form.LoadingError className="text-destructive px-4 py-3 rounded mb-4" />
537
- * <Form.Fields fieldMap={FIELD_MAP} />
553
+ * <Form.Fields
554
+ * fieldMap={FIELD_MAP}
555
+ * rowGapClassname="gap-y-4"
556
+ * columnGapClassname="gap-x-2"
557
+ * />
538
558
  * <Form.Error className="text-destructive p-4 rounded-lg mb-4" />
539
559
  * <Form.Submitted className="text-green-500 p-4 rounded-lg mb-4" />
540
560
  * </Form.Root>
@@ -544,25 +564,130 @@ interface FieldsProps {
544
564
  *
545
565
  * @example
546
566
  * ```tsx
547
- * // Advanced usage with custom field components
548
- * const CustomTextField = ({ value, onChange, label, error, ...props }) => (
549
- * <div className="form-field">
550
- * <label className="text-foreground font-paragraph">{label}</label>
551
- * <input
552
- * value={value || ''}
553
- * onChange={(e) => onChange(e.target.value)}
554
- * className="bg-background border-foreground text-foreground"
555
- * {...props}
556
- * />
557
- * {error && <span className="text-destructive">{error}</span>}
558
- * </div>
559
- * );
567
+ * // Creating custom field components - ALL field components MUST use Form.Field
568
+ * // This example shows the REQUIRED structure for a TEXT_INPUT component
569
+ * import { Form, type TextInputProps } from '@wix/headless-forms/react';
570
+ *
571
+ * const TextInput = (props: TextInputProps) => {
572
+ * const { id, value, onChange, label, error, required, ...inputProps } = props;
573
+ *
574
+ * // Form.Field provides automatic grid layout positioning
575
+ * return (
576
+ * <Form.Field id={id}>
577
+ * <Form.Field.Label>
578
+ * <label className="text-foreground font-paragraph">
579
+ * {label}
580
+ * {required && <span className="text-destructive ml-1">*</span>}
581
+ * </label>
582
+ * </Form.Field.Label>
583
+ * <Form.Field.Input
584
+ * description={error && <span className="text-destructive text-sm">{error}</span>}
585
+ * >
586
+ * <input
587
+ * type="text"
588
+ * value={value || ''}
589
+ * onChange={(e) => onChange(e.target.value)}
590
+ * className="bg-background border-foreground text-foreground"
591
+ * aria-invalid={!!error}
592
+ * {...inputProps}
593
+ * />
594
+ * </Form.Field.Input>
595
+ * </Form.Field>
596
+ * );
597
+ * };
560
598
  *
561
599
  * const FIELD_MAP = {
562
- * TEXT_INPUT: CustomTextField,
563
- * // ... other field components
600
+ * TEXT_INPUT: TextInput,
601
+ * // ... all other field components must also use Form.Field
564
602
  * };
565
603
  * ```
566
604
  */
567
605
  export declare const Fields: React.ForwardRefExoticComponent<FieldsProps & React.RefAttributes<HTMLDivElement>>;
606
+ /**
607
+ * Props for Field container component
608
+ */
609
+ export interface FieldProps {
610
+ /** The unique identifier for this field */
611
+ id: string;
612
+ /** Child components (Field.Label, Field.Input, etc.) */
613
+ children: React.ReactNode;
614
+ /** Whether to render as a child component */
615
+ asChild?: boolean;
616
+ /** CSS classes to apply to the root element */
617
+ className?: string;
618
+ }
619
+ /**
620
+ * Field component with sub-components
621
+ */
622
+ interface FieldComponent extends React.ForwardRefExoticComponent<FieldProps & React.RefAttributes<HTMLDivElement>> {
623
+ Label: typeof FieldLabel;
624
+ Input: typeof FieldInput;
625
+ }
626
+ /**
627
+ * Props for Field.Label component
628
+ */
629
+ export interface FieldLabelProps {
630
+ /** Label content to display */
631
+ children: React.ReactNode;
632
+ /** Whether to render as a child component */
633
+ asChild?: boolean;
634
+ /** CSS classes to apply to the label element */
635
+ className?: string;
636
+ }
637
+ /**
638
+ * Props for Field.Input component
639
+ */
640
+ export interface FieldInputProps {
641
+ /** Input element to render */
642
+ children: React.ReactNode;
643
+ /** Whether to render as a child component */
644
+ asChild?: boolean;
645
+ /** CSS classes to apply to the input element */
646
+ className?: string;
647
+ /** Description text to display below the input */
648
+ description?: React.ReactNode;
649
+ }
650
+ /**
651
+ * Label component for a form field with automatic grid positioning.
652
+ * Must be used within a Form.Field component.
653
+ * Renders in the label row of the field's grid layout.
654
+ *
655
+ * @component
656
+ * @example
657
+ * ```tsx
658
+ * import { Form } from '@wix/headless-forms/react';
659
+ *
660
+ * <Form.Field id="email">
661
+ * <Form.Field.Label>
662
+ * <label className="text-foreground font-paragraph">Email Address</label>
663
+ * </Form.Field.Label>
664
+ * <Form.Field.Input>
665
+ * <input type="email" className="bg-background border-foreground" />
666
+ * </Form.Field.Input>
667
+ * </Form.Field>
668
+ * ```
669
+ */
670
+ export declare const FieldLabel: React.ForwardRefExoticComponent<FieldLabelProps & React.RefAttributes<HTMLDivElement>>;
671
+ /**
672
+ * Input component for a form field with automatic grid positioning.
673
+ * Must be used within a Form.Field component.
674
+ * Renders in the input row of the field's grid layout with optional description.
675
+ *
676
+ * @component
677
+ * @example
678
+ * ```tsx
679
+ * import { Form } from '@wix/headless-forms/react';
680
+ *
681
+ * <Form.Field id="password">
682
+ * <Form.Field.Label>
683
+ * <label className="text-foreground font-paragraph">Password</label>
684
+ * </Form.Field.Label>
685
+ * <Form.Field.Input description={<span className="text-secondary-foreground">Min 8 characters</span>}>
686
+ * <input type="password" className="bg-background border-foreground text-foreground" />
687
+ * </Form.Field.Input>
688
+ * </Form.Field>
689
+ * ```
690
+ */
691
+ export declare const FieldInput: React.ForwardRefExoticComponent<FieldInputProps & React.RefAttributes<HTMLDivElement>>;
692
+ export declare const Field: FieldComponent;
568
693
  export {};