@wix/headless-bookings 0.0.56 → 0.0.58

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.
Files changed (39) hide show
  1. package/cjs/dist/react/booking/Booking.js +1 -1
  2. package/cjs/dist/react/booking-form/BookingForm.d.ts +117 -17
  3. package/cjs/dist/react/booking-form/BookingForm.js +149 -15
  4. package/cjs/dist/react/core/booking/Booking.d.ts +3 -3
  5. package/cjs/dist/react/core/booking/Booking.js +2 -2
  6. package/cjs/dist/react/core/booking-form/BookingForm.d.ts +112 -10
  7. package/cjs/dist/react/core/booking-form/BookingForm.js +204 -64
  8. package/cjs/dist/react/core/payment/Payment.d.ts +84 -5
  9. package/cjs/dist/react/core/payment/Payment.js +102 -4
  10. package/cjs/dist/react/payment/Payment.d.ts +64 -58
  11. package/cjs/dist/react/payment/Payment.js +66 -80
  12. package/cjs/dist/services/booking-form/booking-form.d.ts +22 -13
  13. package/cjs/dist/services/booking-form/booking-form.js +67 -25
  14. package/cjs/dist/services/booking-form/utils.d.ts +52 -0
  15. package/cjs/dist/services/booking-form/utils.js +47 -0
  16. package/cjs/dist/services/index.d.ts +1 -1
  17. package/cjs/dist/services/payment/payment.d.ts +4 -4
  18. package/cjs/dist/services/payment/payment.def.d.ts +4 -4
  19. package/cjs/dist/services/payment/payment.js +26 -18
  20. package/dist/react/booking/Booking.js +1 -1
  21. package/dist/react/booking-form/BookingForm.d.ts +117 -17
  22. package/dist/react/booking-form/BookingForm.js +149 -15
  23. package/dist/react/core/booking/Booking.d.ts +3 -3
  24. package/dist/react/core/booking/Booking.js +2 -2
  25. package/dist/react/core/booking-form/BookingForm.d.ts +112 -10
  26. package/dist/react/core/booking-form/BookingForm.js +204 -64
  27. package/dist/react/core/payment/Payment.d.ts +84 -5
  28. package/dist/react/core/payment/Payment.js +102 -4
  29. package/dist/react/payment/Payment.d.ts +64 -58
  30. package/dist/react/payment/Payment.js +66 -80
  31. package/dist/services/booking-form/booking-form.d.ts +22 -13
  32. package/dist/services/booking-form/booking-form.js +67 -25
  33. package/dist/services/booking-form/utils.d.ts +52 -0
  34. package/dist/services/booking-form/utils.js +47 -0
  35. package/dist/services/index.d.ts +1 -1
  36. package/dist/services/payment/payment.d.ts +4 -4
  37. package/dist/services/payment/payment.def.d.ts +4 -4
  38. package/dist/services/payment/payment.js +26 -18
  39. package/package.json +3 -3
@@ -7,10 +7,44 @@ import React from 'react';
7
7
  import { AsChildSlot } from '@wix/headless-utils/react';
8
8
  import { Form } from '@wix/headless-forms/react';
9
9
  import * as CoreBookingForm from '../core/booking-form/BookingForm.js';
10
+ import { BookingFormConfigurationError } from '../../services/booking-form/booking-form.js';
11
+ /**
12
+ * Internal ErrorBoundary for catching BookingFormConfigurationError
13
+ */
14
+ class BookingFormErrorBoundary extends React.Component {
15
+ constructor(props) {
16
+ super(props);
17
+ this.state = { error: null };
18
+ }
19
+ static getDerivedStateFromError(error) {
20
+ return { error };
21
+ }
22
+ render() {
23
+ const { error } = this.state;
24
+ const { children, fallback } = this.props;
25
+ if (error) {
26
+ // If it's a configuration error, render the fallback
27
+ if (error instanceof BookingFormConfigurationError) {
28
+ if (typeof fallback === 'function') {
29
+ return fallback(error);
30
+ }
31
+ if (fallback) {
32
+ return fallback;
33
+ }
34
+ // Default fallback UI
35
+ return _jsx("div", { "data-testid": "booking-form-load-error", children: error.message });
36
+ }
37
+ // Re-throw non-configuration errors
38
+ throw error;
39
+ }
40
+ return children;
41
+ }
42
+ }
10
43
  var TestIds;
11
44
  (function (TestIds) {
12
45
  TestIds["bookingFormRoot"] = "booking-form-root";
13
46
  TestIds["bookingFormFields"] = "booking-form-fields";
47
+ TestIds["bookingFormActionValidateFormSubmission"] = "booking-form-action-validate-form-submission";
14
48
  })(TestIds || (TestIds = {}));
15
49
  /**
16
50
  * Root component for booking form that wraps CoreBookingForm.Root.
@@ -26,6 +60,12 @@ var TestIds;
26
60
  * // Minimal usage - uses default field components
27
61
  * <BookingForm.Root formId="your-form-id" />
28
62
  *
63
+ * // With serviceIds
64
+ * <BookingForm.Root
65
+ * formId="your-form-id"
66
+ * serviceIds={['service-1', 'service-2']}
67
+ * />
68
+ *
29
69
  * // With custom styling
30
70
  * <BookingForm.Root
31
71
  * formId="your-form-id"
@@ -51,8 +91,8 @@ var TestIds;
51
91
  * ```
52
92
  */
53
93
  export const Root = React.forwardRef((props, ref) => {
54
- const { children, asChild, className, formId, additionalMetadata, fieldMap, rowGapClassname = 'gap-y-4', columnGapClassname = 'gap-x-2', ...otherProps } = props;
55
- return (_jsx(CoreBookingForm.Root, { formId: formId, additionalMetadata: additionalMetadata, fieldMap: fieldMap, children: ({ fields }) => (_jsx(BookingFormContext.Provider, { value: { fields, rowGapClassname, columnGapClassname }, children: _jsx(AsChildSlot, { ref: ref, asChild: asChild, className: className, "data-testid": TestIds.bookingFormRoot, ...otherProps, children: _jsx("div", { children: children ?? (_jsxs(_Fragment, { children: [_jsx(Form.Loading, { className: "flex justify-center p-4 text-foreground" }), _jsx(Form.LoadingError, { className: "bg-destructive/10 border border-destructive/20 text-destructive px-4 py-3 rounded-lg mb-4" }), _jsx(Form.Fields, { fieldMap: fields, rowGapClassname: rowGapClassname, columnGapClassname: columnGapClassname }), _jsx(Form.Error, { className: "mt-4 bg-destructive/10 border border-destructive/20 text-destructive px-4 py-3 rounded-lg" }), _jsx(Form.Submitted, { className: "mt-4 bg-green-500/10 border border-green-500/20 text-green-500 px-4 py-3 rounded-lg" })] })) }) }) })) }));
94
+ const { children, asChild, className, formId, serviceIds, additionalMetadata, fieldMap, rowGapClassname = 'gap-y-4', columnGapClassname = 'gap-x-2', loadErrorFallback, ...otherProps } = props;
95
+ return (_jsx(BookingFormErrorBoundary, { fallback: loadErrorFallback, children: _jsx(CoreBookingForm.Root, { formId: formId, serviceIds: serviceIds, additionalMetadata: additionalMetadata, fieldMap: fieldMap, children: ({ fields }) => (_jsx(BookingFormContext.Provider, { value: { fields, rowGapClassname, columnGapClassname }, children: _jsx(AsChildSlot, { ref: ref, asChild: asChild, className: className, "data-testid": TestIds.bookingFormRoot, ...otherProps, children: _jsx("div", { children: children ?? (_jsxs(_Fragment, { children: [_jsx(Form.Loading, { className: "flex justify-center p-4 text-foreground" }), _jsx(Form.LoadingError, { className: "bg-destructive/10 border border-destructive/20 text-destructive px-4 py-3 rounded-lg mb-4" }), _jsx(Form.Fields, { fieldMap: fields, rowGapClassname: rowGapClassname, columnGapClassname: columnGapClassname }), _jsx(Form.Error, { className: "mt-4 bg-destructive/10 border border-destructive/20 text-destructive px-4 py-3 rounded-lg" }), _jsx(Form.Submitted, { className: "mt-4 bg-green-500/10 border border-green-500/20 text-green-500 px-4 py-3 rounded-lg" })] })) }) }) })) }) }));
56
96
  });
57
97
  Root.displayName = 'BookingForm.Root';
58
98
  const BookingFormContext = React.createContext(null);
@@ -87,6 +127,77 @@ export const Fields = React.forwardRef((props, ref) => {
87
127
  return (_jsx("div", { ref: ref, className: className, "data-testid": TestIds.bookingFormFields, ...otherProps, children: _jsx(Form.Fields, { fieldMap: context.fields, rowGapClassname: rowGapClassname, columnGapClassname: columnGapClassname }) }));
88
128
  });
89
129
  Fields.displayName = 'BookingForm.Fields';
130
+ /**
131
+ * Button to validate the booking form submission.
132
+ * Must be used within BookingForm.Root or BookingForm.Data context.
133
+ * Default label is "Validate".
134
+ *
135
+ * @component
136
+ * @example
137
+ * ```tsx
138
+ * // Within BookingForm.Root
139
+ * <BookingForm.Root formId="your-form-id" fieldMap={...}>
140
+ * <BookingForm.Fields />
141
+ * <BookingForm.Actions.ValidateFormSubmission />
142
+ * </BookingForm.Root>
143
+ *
144
+ * // With custom label
145
+ * <BookingForm.Actions.ValidateFormSubmission label="Check Form" />
146
+ *
147
+ * // With asChild
148
+ * <BookingForm.Actions.ValidateFormSubmission asChild>
149
+ * <button className="btn-primary">Validate Booking</button>
150
+ * </BookingForm.Actions.ValidateFormSubmission>
151
+ *
152
+ * // Using render prop pattern with validation callback
153
+ * <BookingForm.Actions.ValidateFormSubmission
154
+ * onValidationComplete={(result) => {
155
+ * if (result.valid) {
156
+ * console.log('Form is valid!');
157
+ * } else {
158
+ * console.log('Validation errors:', result.validationFailures);
159
+ * }
160
+ * }}
161
+ * >
162
+ * {({ onClick, valid, validationFailures }) => (
163
+ * <button onClick={onClick}>
164
+ * {valid === null ? 'Validate' : valid ? 'Valid ✓' : 'Invalid ✗'}
165
+ * {validationFailures.length > 0 && (
166
+ * <span> ({validationFailures.length} errors)</span>
167
+ * )}
168
+ * </button>
169
+ * )}
170
+ * </BookingForm.Actions.ValidateFormSubmission>
171
+ * ```
172
+ */
173
+ export const ValidateFormSubmission = React.forwardRef((props, ref) => {
174
+ const { asChild, children, className, label = 'Validate', onValidationComplete, } = props;
175
+ const [valid, setValid] = React.useState(null);
176
+ const [validationFailures, setValidationFailures] = React.useState([]);
177
+ return (_jsx(CoreBookingForm.Actions, { children: ({ validateFormSubmission }) => {
178
+ const handleClick = async () => {
179
+ const result = await validateFormSubmission();
180
+ setValid(result.valid);
181
+ setValidationFailures(result.validationFailures);
182
+ if (onValidationComplete) {
183
+ onValidationComplete(result);
184
+ }
185
+ };
186
+ return (_jsx(AsChildSlot, { ref: ref, asChild: asChild, className: className, "data-testid": TestIds.bookingFormActionValidateFormSubmission, "data-valid": valid, customElement: children, customElementProps: {
187
+ onClick: handleClick,
188
+ valid,
189
+ validationFailures,
190
+ }, children: _jsx("button", { onClick: handleClick, children: label }) }));
191
+ } }));
192
+ });
193
+ ValidateFormSubmission.displayName =
194
+ 'BookingForm.Actions.ValidateFormSubmission';
195
+ /**
196
+ * Actions namespace for BookingForm
197
+ */
198
+ export const Actions = {
199
+ ValidateFormSubmission,
200
+ };
90
201
  /**
91
202
  * Data component that provides booking form data via render props.
92
203
  * Use this when you need programmatic access to form state and actions.
@@ -95,12 +206,17 @@ Fields.displayName = 'BookingForm.Fields';
95
206
  * @example
96
207
  * ```tsx
97
208
  * <BookingForm.Data formId="your-form-id">
98
- * {({ formId, fields, formValues, setFormSubmission, validateFormSubmission }) => (
209
+ * {({ formId, fields, formValues, formRef, validate, setFormSubmission }) => (
99
210
  * <div>
100
- * <Form.Fields fieldMap={fields} rowGapClassname="gap-y-4" columnGapClassname="gap-x-2" />
211
+ * <Form.Fields
212
+ * fieldMap={fields}
213
+ * rowGapClassname="gap-y-4"
214
+ * columnGapClassname="gap-x-2"
215
+ * formRef={formRef}
216
+ * />
101
217
  * <button onClick={async () => {
102
- * const result = await validateFormSubmission();
103
- * if (result.valid) {
218
+ * const isValid = await validate();
219
+ * if (isValid) {
104
220
  * // Store current form values before submission
105
221
  * setFormSubmission(formValues);
106
222
  * }
@@ -110,17 +226,35 @@ Fields.displayName = 'BookingForm.Fields';
110
226
  * </div>
111
227
  * )}
112
228
  * </BookingForm.Data>
229
+ *
230
+ * // With serviceIds
231
+ * <BookingForm.Data
232
+ * formId="your-form-id"
233
+ * serviceIds={['service-1', 'service-2']}
234
+ * >
235
+ * {({ fields, formRef }) => (
236
+ * <Form.Fields fieldMap={fields} formRef={formRef} />
237
+ * )}
238
+ * </BookingForm.Data>
113
239
  * ```
114
240
  */
115
241
  export function Data(props) {
116
- const { formId, additionalMetadata, fieldMap, children } = props;
117
- return (_jsx(CoreBookingForm.Root, { formId: formId, additionalMetadata: additionalMetadata, fieldMap: fieldMap, children: ({ formId: id, fields, formValues, formSubmission, setFormSubmission, validateFormSubmission, }) => children({
118
- formId: id,
119
- fields,
120
- formValues,
121
- formSubmission,
122
- setFormSubmission,
123
- validateFormSubmission,
124
- }) }));
242
+ const { formId, serviceIds, additionalMetadata, fieldMap, children, loadErrorFallback, } = props;
243
+ return (_jsx(BookingFormErrorBoundary, { fallback: loadErrorFallback, children: _jsx(CoreBookingForm.Root, { formId: formId, serviceIds: serviceIds, additionalMetadata: additionalMetadata, fieldMap: fieldMap, children: ({ formId: id, fields, formValues, formSubmission, setFormSubmission, validateFormSubmission, formRef, }) => children({
244
+ formId: id,
245
+ fields,
246
+ formValues,
247
+ formSubmission,
248
+ setFormSubmission,
249
+ formRef,
250
+ validate: validateFormSubmission,
251
+ }) }) }));
125
252
  }
126
253
  Data.displayName = 'BookingForm.Data';
254
+ // ============================================================================
255
+ // Error Handling
256
+ // ============================================================================
257
+ /**
258
+ * Re-export LoadError component and utilities for error handling
259
+ */
260
+ export { LoadError, isBookingFormConfigurationError, BookingFormConfigurationError, } from '../core/booking-form/BookingForm.js';
@@ -7,7 +7,7 @@ import { type BookingServiceConfig, type ServiceSelection, type BookingServiceAc
7
7
  import type { FormValues } from '@wix/form-public';
8
8
  import type { TimeSlot, Resource } from '@wix/auto_sdk_bookings_availability-time-slots';
9
9
  import type { Service, Location as ServiceLocationType } from '@wix/auto_sdk_bookings_services';
10
- import type { SlotService } from '../../../services/payment/payment.js';
10
+ import type { PricingServiceSelection } from '../../../services/payment/payment.js';
11
11
  /**
12
12
  * Props for Booking.Root component
13
13
  */
@@ -167,8 +167,8 @@ export declare function ItemsData(props: ItemsDataProps): React.ReactNode;
167
167
  * Data exposed by PaymentData render prop
168
168
  */
169
169
  export interface PaymentDataRenderProps {
170
- /** SlotServices array for Payment.Root */
171
- slotServices: SlotService[];
170
+ /** Pricing service selections array for Payment.Root */
171
+ pricingServiceSelections: PricingServiceSelection[];
172
172
  }
173
173
  /**
174
174
  * Props for PaymentData render prop component
@@ -267,9 +267,9 @@ export function PaymentData(props) {
267
267
  const { children } = props;
268
268
  const bookingService = useService(BookingServiceDefinition);
269
269
  const serviceSelections = bookingService.serviceSelections.get();
270
- const slotServices = serviceSelections.map((selection) => ({
270
+ const pricingServiceSelections = serviceSelections.map((selection) => ({
271
271
  service: selection.service,
272
272
  lineItemId: selection.instanceId,
273
273
  }));
274
- return children({ slotServices });
274
+ return children({ pricingServiceSelections });
275
275
  }
@@ -9,11 +9,18 @@
9
9
  * 2. SSR/SSG: Provide a pre-loaded form object for server-side rendering
10
10
  */
11
11
  import * as React from 'react';
12
- import type { FormValues } from '@wix/form-public';
13
- import { forms } from '@wix/forms';
14
- import { type ValidationResult } from '../../../services/booking-form/booking-form.js';
12
+ import { type FormValues } from '@wix/form-public';
13
+ import { forms, submissions } from '@wix/forms';
14
+ import { BookingFormConfigurationError, type ValidationResult } from '../../../services/booking-form/booking-form.js';
15
15
  import { Form } from '@wix/headless-forms/react';
16
16
  type FieldMap = Parameters<typeof Form.Fields>[0]['fieldMap'];
17
+ export type Submit = () => Promise<submissions.GetSubmissionResponse['submission'] | undefined>;
18
+ export type ValidateStep = (stepId: string) => Promise<boolean>;
19
+ export type FormHandle = {
20
+ submit: Submit;
21
+ validate: () => Promise<boolean>;
22
+ validateStep?: ValidateStep;
23
+ };
17
24
  /**
18
25
  * Render props data provided to BookingForm children
19
26
  */
@@ -26,19 +33,23 @@ export interface BookingFormRenderProps {
26
33
  formSubmission: FormValues | null;
27
34
  /** Action to store form submission data */
28
35
  setFormSubmission: (formValues: FormValues) => void;
29
- /** Action to validate the form */
30
- validateFormSubmission: () => Promise<ValidationResult>;
36
+ /** Action to validate the form fields. Returns true if valid, false if there are errors. */
37
+ validateFormSubmission: () => Promise<boolean>;
31
38
  /** Merged field map (DEFAULT_BOOKING_FIELD_MAP + user overrides) */
32
39
  fields: FieldMap;
40
+ /** Ref to access form methods imperatively (pass to Form.Fields) */
41
+ formRef: React.RefObject<FormHandle | null>;
33
42
  }
34
43
  /**
35
44
  * Base props shared by all BookingForm config patterns
36
45
  */
37
46
  interface BookingFormBaseProps {
47
+ /** Optional service IDs to pass to FormService (will be merged into additionalMetadata) */
48
+ serviceIds?: string[];
38
49
  /** Optional additional metadata to pass to FormService */
39
50
  additionalMetadata?: Record<string, string | string[]>;
40
- /** Optional partial field map to override default booking field components */
41
- fieldMap?: Partial<FieldMap>;
51
+ /** Field map to override default booking field components */
52
+ fieldMap: FieldMap;
42
53
  /** Render prop function that receives form data and actions */
43
54
  children: (data: BookingFormRenderProps) => React.ReactNode;
44
55
  }
@@ -46,13 +57,14 @@ interface BookingFormBaseProps {
46
57
  * Props for BookingForm with formId (client-side loading)
47
58
  */
48
59
  interface BookingFormWithFormIdProps extends BookingFormBaseProps {
49
- /** The form ID to load (client-side loading) */
50
- formId: string;
60
+ /** The form ID to load (client-side loading) if not provided, the formId will be extracted from the selected service if available */
61
+ formId?: string;
51
62
  /** Pre-loaded form - not used in this mode */
52
63
  form?: never;
53
64
  }
54
65
  /**
55
66
  * Props for BookingForm with pre-loaded form (SSR/SSG)
67
+ * Returns undefined if formId should be extracted from services by the service layer
56
68
  */
57
69
  interface BookingFormWithFormProps extends BookingFormBaseProps {
58
70
  /** Pre-loaded form object (SSR/SSG) - must be from bookings namespace */
@@ -136,4 +148,94 @@ export type BookingFormProps = BookingFormWithFormIdProps | BookingFormWithFormP
136
148
  * ```
137
149
  */
138
150
  export declare function Root(props: BookingFormProps): React.ReactNode;
139
- export {};
151
+ /**
152
+ * Render props for BookingForm.Actions component
153
+ */
154
+ export interface ActionsRenderProps {
155
+ /** Action to validate the form submission */
156
+ validateFormSubmission: () => Promise<ValidationResult>;
157
+ /** The form ID being used */
158
+ formId: string;
159
+ /** Current form values */
160
+ formValues: FormValues;
161
+ /** Current form submission data */
162
+ formSubmission: FormValues | null;
163
+ }
164
+ /**
165
+ * Props for BookingForm.Actions component
166
+ */
167
+ export interface ActionsProps {
168
+ children: (data: ActionsRenderProps) => React.ReactNode;
169
+ }
170
+ /**
171
+ * Core component that provides booking form actions via render props.
172
+ * Must be used within BookingForm.Root context.
173
+ *
174
+ * @component
175
+ * @example
176
+ * ```tsx
177
+ * <CoreBookingForm.Root formId="form-123">
178
+ * {({ fields }) => (
179
+ * <>
180
+ * <Form.Fields fieldMap={fields} />
181
+ * <CoreBookingForm.Actions>
182
+ * {({ validateFormSubmission, formId }) => (
183
+ * <button onClick={async () => {
184
+ * const result = await validateFormSubmission();
185
+ * console.log('Validation result:', result);
186
+ * }}>
187
+ * Validate Form
188
+ * </button>
189
+ * )}
190
+ * </CoreBookingForm.Actions>
191
+ * </>
192
+ * )}
193
+ * </CoreBookingForm.Root>
194
+ * ```
195
+ */
196
+ export declare function Actions(props: ActionsProps): React.ReactNode;
197
+ /**
198
+ * Props for BookingForm.LoadError component
199
+ */
200
+ export interface LoadErrorProps {
201
+ /** The error that was caught */
202
+ error: Error;
203
+ /** Whether to render as a child component */
204
+ asChild?: boolean;
205
+ /** Custom content to render. If not provided, displays the error message. */
206
+ children?: React.ReactNode;
207
+ /** Additional CSS class name */
208
+ className?: string;
209
+ }
210
+ /**
211
+ * Component to display when BookingForm fails to load due to configuration errors.
212
+ * This component should be used in an error boundary to catch BookingFormConfigurationError.
213
+ *
214
+ * @component
215
+ * @example
216
+ * ```tsx
217
+ * import * as CoreBookingForm from '@wix/headless-bookings/react/core/booking-form';
218
+ *
219
+ * // Default usage with className
220
+ * <CoreBookingForm.LoadError error={error} className="text-destructive p-4" />
221
+ *
222
+ * // Custom content
223
+ * <CoreBookingForm.LoadError error={error}>
224
+ * <div className="error-container">
225
+ * <h3>Error Loading Form</h3>
226
+ * <p>Please try again later.</p>
227
+ * </div>
228
+ * </CoreBookingForm.LoadError>
229
+ *
230
+ * // With asChild for custom components
231
+ * <CoreBookingForm.LoadError error={error} asChild>
232
+ * <CustomErrorComponent />
233
+ * </CoreBookingForm.LoadError>
234
+ * ```
235
+ */
236
+ export declare const LoadError: React.ForwardRefExoticComponent<LoadErrorProps & React.RefAttributes<HTMLElement>>;
237
+ /**
238
+ * Type guard to check if an error is a BookingFormConfigurationError
239
+ */
240
+ export declare function isBookingFormConfigurationError(error: unknown): error is BookingFormConfigurationError;
241
+ export { BookingFormConfigurationError };