@vulcantech/forms 0.1.1 → 0.2.0

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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as Yup from 'yup';
2
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as react from 'react';
3
3
  import { CSSProperties, DragEvent } from 'react';
4
4
 
5
5
  type FieldType = "text" | "email" | "phone" | "url" | "address" | "textarea" | "select" | "checkbox" | "radio" | "number" | "date" | "datetime" | "hidden" | "sensitive" | "file";
@@ -76,7 +76,15 @@ declare const DEFAULT_SENSITIVE_FIELD_CONFIG: SensitiveFieldConfig;
76
76
  /**
77
77
  * Configuration for the `file` field type. Files are uploaded to
78
78
  * Firebase Storage client-side; the form submission receives the
79
- * download URL(s) rather than raw file data.
79
+ * download URL(s) rather than raw file data — a string, or an array of
80
+ * strings when `multiple`. The CMS stores whatever string arrives and does
81
+ * nothing file-specific with it.
82
+ *
83
+ * KNOWN LIMITATION: `FileField`'s `onUpload` prop is not threaded through
84
+ * `DynamicFormField`, so a file field rendered via `DynamicForm` falls back
85
+ * to `URL.createObjectURL` and submits a `blob:` URL that is meaningless
86
+ * outside the submitting tab. Render `FileField` directly with an `onUpload`
87
+ * handler when real uploads are needed.
80
88
  */
81
89
  interface FileFieldConfig {
82
90
  /** Accepted MIME types. Default: "image/*" */
@@ -260,11 +268,47 @@ type FormStatus = "draft" | "published" | "archived";
260
268
  * (optional, for preferences/consent) fields. Duplicate emails within a
261
269
  * project update the existing record's preferences rather than creating
262
270
  * a new entry.
271
+ * - `booking`: writes to the project's `bookings` subcollection and surfaces
272
+ * in the Bookings dashboard. The submitted values are stored like an
273
+ * application (nested `data` map), plus a denormalized booking `date`
274
+ * (epoch ms) parsed from the form's designated date field — see
275
+ * {@link FormBookingConfig}.
263
276
  *
264
277
  * Only the routing changes — spam protection, sensitive field handling,
265
278
  * notification emails, and integrations behave identically.
266
279
  */
267
- type FormSubmissionType = "contact" | "application" | "newsletter";
280
+ type FormSubmissionType = "contact" | "application" | "newsletter" | "booking";
281
+ /**
282
+ * Booking settings for a form with `submissionType: "booking"`. The renderer
283
+ * is unaffected — booking forms submit exactly like any other form; the CMS
284
+ * submission route uses this config to pull the booking date(s) out of the
285
+ * submitted values.
286
+ *
287
+ * Date-only convention: a `date` field submits `"yyyy-MM-dd"`, which parses
288
+ * to UTC midnight, so a stored epoch that is an exact multiple of a day is
289
+ * treated as date-only and displayed in UTC. A `datetime` field submits a
290
+ * full ISO string and is stored as a real instant. An unparseable value is
291
+ * stored as an explicit `null` rather than a fabricated date.
292
+ */
293
+ interface FormBookingConfig {
294
+ /**
295
+ * Field id of the `date`/`datetime` field holding the booking date (the
296
+ * start date, when `endDateFieldId` is also set). When omitted, the
297
+ * submission route falls back to the form's first date/datetime field in
298
+ * render order, and then to a top-level `date` key on the submission.
299
+ */
300
+ dateFieldId?: string;
301
+ /**
302
+ * Optional second `date`/`datetime` field id for begin/end bookings.
303
+ * Bookings without an end date use `defaultDurationMinutes` instead.
304
+ */
305
+ endDateFieldId?: string;
306
+ /**
307
+ * Default duration (minutes) stamped onto each new booking. Individual
308
+ * bookings can be overridden afterwards in the Bookings dashboard.
309
+ */
310
+ defaultDurationMinutes?: number;
311
+ }
268
312
  /**
269
313
  * Field types allowed on a newsletter form. Enforced by the form builder UI
270
314
  * and (defensively) by the submission route. Keep in sync with
@@ -311,6 +355,16 @@ declare const APPLICATION_STATUS_KEYS: ApplicationStatusKey[];
311
355
  */
312
356
  type ApplicationStatusEmailTemplates = Partial<Record<ApplicationStatusKey, NotificationEmailTemplate>>;
313
357
  declare const DEFAULT_APPLICATION_STATUS_EMAIL_TEMPLATES: Record<ApplicationStatusKey, NotificationEmailTemplate>;
358
+ /**
359
+ * The CMS-authored schema for one form — the value `DynamicForm` renders and
360
+ * the CMS submission route consults to route and validate a submission.
361
+ *
362
+ * Consumer sites fetch this from `GET /api/forms` rather than building it.
363
+ * Four fields are stripped from every client-facing response by the CMS
364
+ * (`stripSensitiveFields`) and are therefore always `undefined` outside the
365
+ * CMS server: `projectId`, `integrations`, `notificationEmailTemplate`, and
366
+ * `applicationStatusEmails`.
367
+ */
314
368
  interface FormDefinition {
315
369
  id: string;
316
370
  /**
@@ -323,17 +377,37 @@ interface FormDefinition {
323
377
  */
324
378
  projectId?: string;
325
379
  name: string;
380
+ /** Unique within a project — the CMS rejects a colliding slug with a 409. */
326
381
  slug: string;
327
382
  description?: string;
383
+ /** Bumped on every CMS edit. Emitted by the renderer as `data-form-version`. */
328
384
  version: number;
385
+ /**
386
+ * FormStatus values: "draft" | "published" | "archived".
387
+ *
388
+ * Deliberately typed `string`, not the {@link FormStatus} union, because
389
+ * definitions cross a JSON boundary. Compare against the literals. The
390
+ * public GET route already filters to published forms unless the caller
391
+ * passes `includeDrafts=1`.
392
+ */
329
393
  status: string;
330
394
  /**
331
395
  * Routes submissions to the matching dashboard collection.
332
396
  * Defaults to `"contact"` when omitted. See {@link FormSubmissionType}.
397
+ *
398
+ * Read server-side from this definition, never from the request body — a
399
+ * submission whose `formId` does not resolve always routes to `contact`.
333
400
  */
334
401
  submissionType?: FormSubmissionType;
402
+ /**
403
+ * Booking settings. Only consulted when `submissionType === "booking"`.
404
+ * See {@link FormBookingConfig}.
405
+ */
406
+ bookingConfig?: FormBookingConfig;
407
+ /** Rendered in `order`, not array order — the renderer sorts before rendering. */
335
408
  sections: FormSection[];
336
409
  submitConfig: FormSubmitConfig;
410
+ /** Absent = {@link DEFAULT_FORM_STYLE} (structure only; colors stay unset). */
337
411
  style?: FormStyle;
338
412
  /**
339
413
  * Optional hex color applied as the default text color for every section
@@ -346,15 +420,19 @@ interface FormDefinition {
346
420
  trackAdReferral?: boolean;
347
421
  customFields?: Record<string, unknown>;
348
422
  };
423
+ /** Server-only — stripped from client-facing responses like `projectId`. */
349
424
  integrations?: FormIntegrations;
350
425
  /**
351
426
  * Customizable email sent to project members when a submission arrives.
352
427
  * Falls back to `DEFAULT_NOTIFICATION_EMAIL_TEMPLATE` when undefined.
428
+ *
429
+ * Server-only — stripped from client-facing responses like `projectId`.
353
430
  */
354
431
  notificationEmailTemplate?: NotificationEmailTemplate;
355
432
  /**
356
433
  * Per-status applicant-facing email templates. Only consulted when
357
- * `submissionType === "application"`. Missing entries fall back to
434
+ * `submissionType === "application"`. Server-only — stripped from
435
+ * client-facing responses like `projectId`. Missing entries fall back to
358
436
  * `DEFAULT_APPLICATION_STATUS_EMAIL_TEMPLATES[status]`. Set to an empty
359
437
  * object to opt every status into the defaults explicitly.
360
438
  */
@@ -476,6 +554,8 @@ interface FormDefinitionJSON {
476
554
  version: number;
477
555
  status: string;
478
556
  submissionType?: string;
557
+ /** See {@link FormDefinition.bookingConfig} — no narrowed unions, so the strict type is reused. */
558
+ bookingConfig?: FormBookingConfig;
479
559
  sections: FormSectionJSON[];
480
560
  submitConfig: FormSubmitConfigJSON;
481
561
  style?: FormStyleJSON;
@@ -698,16 +778,51 @@ declare function encryptSensitiveValue(plaintext: string, publicKey: TransportPu
698
778
  declare function deriveLast4(plaintext: string): string;
699
779
 
700
780
  interface DynamicFormProps {
781
+ /**
782
+ * The CMS-authored schema, normally fetched from `GET /api/forms`. Note
783
+ * that `projectId`, `integrations`, `notificationEmailTemplate` and
784
+ * `applicationStatusEmails` are stripped from client-facing responses, so
785
+ * they are always undefined here — never read `definition.projectId`.
786
+ */
701
787
  definition: FormDefinition;
788
+ /**
789
+ * Endpoint this form POSTs to — expected to be a server-side proxy in the
790
+ * consuming app that adds credentials and forwards to the CMS
791
+ * `POST /api/contact`.
792
+ *
793
+ * The body is a FLAT JSON object: the definition's field ids as top-level
794
+ * keys, plus `extraData`, plus `formId`. There is no `data`/`values`
795
+ * wrapper. The honeypot, fields hidden by an unmet `showWhen`, and the
796
+ * synthetic `_sameAs_*` toggles are stripped before sending. The renderer
797
+ * never sends `projectId` / `apiKey` / `url` — the proxy must add those,
798
+ * and must forward the browser's body WHOLE (hand-picking fields into a
799
+ * new object drops values the CMS then rejects as required).
800
+ *
801
+ * Two CMS rejections to expect: `url` must exactly equal the project's
802
+ * stored URL (so localhost dev submissions get `400 "Invalid url"`), and
803
+ * a top-level `email` key is required on every submission type.
804
+ */
702
805
  apiRoute: string;
703
806
  /**
704
807
  * Called after a successful submission. The argument is the parsed JSON
705
808
  * response body, when one was returned — application submissions echo
706
809
  * `{ success: true, referenceNumber: "APP-…" }` here so the consumer
707
810
  * can display it on its confirmation page.
811
+ *
812
+ * Also fired on the spam path (honeypot filled or submitted faster than
813
+ * `minSubmitTime`) with NO argument and no network request at all — treat
814
+ * a missing argument as "no server response", not as an error.
708
815
  */
709
816
  onSuccess?: (data?: unknown) => void;
817
+ /**
818
+ * Called when the submission fails. `DynamicForm` catches everything
819
+ * internally and never throws at the call site, so this callback is the
820
+ * only error channel. The message is the CMS `error` string when one was
821
+ * returned (e.g. "Invalid url", "Validation failed"), otherwise
822
+ * `Submission failed (<status>)`.
823
+ */
710
824
  onError?: (error: Error) => void;
825
+ /** Extra top-level keys merged into the payload alongside the field values. */
711
826
  extraData?: Record<string, unknown>;
712
827
  className?: string;
713
828
  /**
@@ -716,6 +831,10 @@ interface DynamicFormProps {
716
831
  * `/api/forms/transport-key` (resolved against the same origin).
717
832
  *
718
833
  * The endpoint must respond with `{ kid: string, jwk: JsonWebKey }`.
834
+ *
835
+ * That route lives in the CMS, not in consumer apps — a consumer site with
836
+ * a sensitive field must either proxy it or pass an absolute CMS URL here.
837
+ * Only fetched when the definition actually contains a sensitive field.
719
838
  */
720
839
  transportKeyEndpoint?: string;
721
840
  /**
@@ -754,7 +873,21 @@ interface DynamicFormProps {
754
873
  */
755
874
  previewTextColor?: string;
756
875
  }
757
- declare const DynamicForm: ({ definition, apiRoute, onSuccess, onError, extraData, className, onFormView, onFormSubmit: onFormSubmitCallback, transportKeyEndpoint, previewState, previewTouchAllSignal, previewUntouchAllSignal, previewBg, previewTextColor, }: DynamicFormProps) => react_jsx_runtime.JSX.Element;
876
+ /**
877
+ * Render a CMS-authored {@link FormDefinition} as a working form.
878
+ *
879
+ * Sections and fields render in `order` (not array order); validation comes
880
+ * from `buildYupSchema(definition)`; sensitive fields are encrypted in the
881
+ * browser before submission. Submission goes to {@link DynamicFormProps.apiRoute}
882
+ * as a flat JSON payload — see that prop for the wire contract.
883
+ *
884
+ * Failure mode: this component never throws. Network and CMS errors are
885
+ * caught, shown in the built-in error banner, and reported via `onError`.
886
+ * Note that a suppressed duplicate (an identical resubmit within 10 minutes)
887
+ * and a server-side spam catch both answer `200` and are indistinguishable
888
+ * from a stored lead by design.
889
+ */
890
+ declare const DynamicForm: ({ definition, apiRoute, onSuccess, onError, extraData, className, onFormView, onFormSubmit: onFormSubmitCallback, transportKeyEndpoint, previewState, previewTouchAllSignal, previewUntouchAllSignal, previewBg, previewTextColor, }: DynamicFormProps) => react.JSX.Element;
758
891
 
759
892
  /**
760
893
  * Single source of truth for the CSS custom properties the renderer publishes
@@ -842,7 +975,7 @@ interface Props$1 {
842
975
  */
843
976
  inlineSubmit?: React.ReactNode;
844
977
  }
845
- declare const DynamicFormSection: ({ section, isLast, formDefaultTextColor, inlineSubmit, }: Props$1) => react_jsx_runtime.JSX.Element;
978
+ declare const DynamicFormSection: ({ section, isLast, formDefaultTextColor, inlineSubmit, }: Props$1) => react.JSX.Element;
846
979
 
847
980
  interface Props {
848
981
  field: FormFieldDefinition;
@@ -851,7 +984,7 @@ interface Props {
851
984
  /** How a mirrored target renders while the toggle is on. */
852
985
  mirrorBehavior?: "hide" | "lock";
853
986
  }
854
- declare const DynamicFormField: ({ field, mirrored, mirrorBehavior }: Props) => react_jsx_runtime.JSX.Element | null;
987
+ declare const DynamicFormField: ({ field, mirrored, mirrorBehavior }: Props) => react.JSX.Element | null;
855
988
 
856
989
  interface BaseFetchOptions {
857
990
  cmsUrl: string;
@@ -974,4 +1107,4 @@ interface UseDragReorderResult extends DragReorderState {
974
1107
  */
975
1108
  declare function useDragReorder<T>({ items, getId, onReorder, dragType, }: UseDragReorderOptions<T>): UseDragReorderResult;
976
1109
 
977
- export { APPLICATION_STATUS_KEYS, type ApplicationStatusEmailTemplates, type ApplicationStatusKey, CORE_EMAIL_TEMPLATE_VARIABLES, DEFAULT_APPLICATION_STATUS_EMAIL_TEMPLATES, DEFAULT_FILE_FIELD_CONFIG, DEFAULT_FORM_STYLE, DEFAULT_NOTIFICATION_EMAIL_HTML, DEFAULT_NOTIFICATION_EMAIL_SUBJECT, DEFAULT_NOTIFICATION_EMAIL_TEMPLATE, DEFAULT_SENSITIVE_FIELD_CONFIG, DEFAULT_SPAM_PROTECTION, type DatetimeConfig, type DragReorderItemHandlers, type DragReorderState, type DropPosition, DynamicForm, DynamicFormField, DynamicFormSection, type EmailTemplateVariableDescriptor, type EmailTemplateVariables, type EncryptedSensitiveValue, FORM_CSS_VAR, FORM_DEFAULT_COLOR, FORM_VAR, type FieldOption, type FieldTransform, type FieldType, type FieldVisibilityCondition, type FieldWidth, type FileFieldConfig, type FormDefinition, type FormDefinitionJSON, type FormFieldDefinition, type FormFieldDefinitionJSON, type FormIdentifier, type FormIntegrationConfig, type FormIntegrationConfigJSON, type FormIntegrations, type FormIntegrationsJSON, type FormSection, type FormSectionJSON, type FormSpamProtectionConfig, type FormStatus, type FormStyle, type FormStyleJSON, type FormStylePreset, type FormSubmissionType, type FormSubmitConfig, type FormSubmitConfigJSON, type FormSuccessBehavior, HONEYPOT_FIELD_NAME, type InputVariant, type IntegrationFieldMappingEntry, type IntegrationFieldMappingEntryJSON, NEWSLETTER_ALLOWED_FIELD_TYPES, type NotificationEmailTemplate, SAME_AS_TOGGLE_PREFIX, SAMPLE_EMAIL_TEMPLATE_VARIABLES, type SectionSameAsConfig, type SensitiveFieldConfig, type SensitiveFieldConfigJSON, type SetStateUpdater, type TextTransform, type TransportPublicKey, type UseDragReorderOptions, type UseDragReorderResult, type UseFormDefinitionOptions, type UseUndoRedoControls, type UseUndoRedoOptions, type UseUndoRedoReturn, type ValidationRule, type ValidationRuleJSON, type VisibilityOperator, applyTextTransform, applyTransform, base64ToBuffer, bufferToBase64, buildEmailTemplateVariables, buildFormCssVars, buildInitialValues, buildYupSchema, deriveLast4, encryptSensitiveValue, formatSsn, formatUsPhone, getFormDefinition, getSafeRedirectUrl, isVisibilityConditionMet, parseFormDefinition, renderEmailTemplate, renderNotificationEmail, sameAsToggleKey, useDragReorder, useFormDefinition, useUndoRedo, useUndoRedoShortcuts };
1110
+ export { APPLICATION_STATUS_KEYS, type ApplicationStatusEmailTemplates, type ApplicationStatusKey, CORE_EMAIL_TEMPLATE_VARIABLES, DEFAULT_APPLICATION_STATUS_EMAIL_TEMPLATES, DEFAULT_FILE_FIELD_CONFIG, DEFAULT_FORM_STYLE, DEFAULT_NOTIFICATION_EMAIL_HTML, DEFAULT_NOTIFICATION_EMAIL_SUBJECT, DEFAULT_NOTIFICATION_EMAIL_TEMPLATE, DEFAULT_SENSITIVE_FIELD_CONFIG, DEFAULT_SPAM_PROTECTION, type DatetimeConfig, type DragReorderItemHandlers, type DragReorderState, type DropPosition, DynamicForm, DynamicFormField, DynamicFormSection, type EmailTemplateVariableDescriptor, type EmailTemplateVariables, type EncryptedSensitiveValue, FORM_CSS_VAR, FORM_DEFAULT_COLOR, FORM_VAR, type FieldOption, type FieldTransform, type FieldType, type FieldVisibilityCondition, type FieldWidth, type FileFieldConfig, type FormBookingConfig, type FormDefinition, type FormDefinitionJSON, type FormFieldDefinition, type FormFieldDefinitionJSON, type FormIdentifier, type FormIntegrationConfig, type FormIntegrationConfigJSON, type FormIntegrations, type FormIntegrationsJSON, type FormSection, type FormSectionJSON, type FormSpamProtectionConfig, type FormStatus, type FormStyle, type FormStyleJSON, type FormStylePreset, type FormSubmissionType, type FormSubmitConfig, type FormSubmitConfigJSON, type FormSuccessBehavior, HONEYPOT_FIELD_NAME, type InputVariant, type IntegrationFieldMappingEntry, type IntegrationFieldMappingEntryJSON, NEWSLETTER_ALLOWED_FIELD_TYPES, type NotificationEmailTemplate, SAME_AS_TOGGLE_PREFIX, SAMPLE_EMAIL_TEMPLATE_VARIABLES, type SectionSameAsConfig, type SensitiveFieldConfig, type SensitiveFieldConfigJSON, type SetStateUpdater, type TextTransform, type TransportPublicKey, type UseDragReorderOptions, type UseDragReorderResult, type UseFormDefinitionOptions, type UseUndoRedoControls, type UseUndoRedoOptions, type UseUndoRedoReturn, type ValidationRule, type ValidationRuleJSON, type VisibilityOperator, applyTextTransform, applyTransform, base64ToBuffer, bufferToBase64, buildEmailTemplateVariables, buildFormCssVars, buildInitialValues, buildYupSchema, deriveLast4, encryptSensitiveValue, formatSsn, formatUsPhone, getFormDefinition, getSafeRedirectUrl, isVisibilityConditionMet, parseFormDefinition, renderEmailTemplate, renderNotificationEmail, sameAsToggleKey, useDragReorder, useFormDefinition, useUndoRedo, useUndoRedoShortcuts };