@vulcantech/forms 0.1.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/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # @vulcantech/forms
2
+
3
+ Dynamic form renderer for VulcanTech apps. Renders a form from a JSON `FormDefinition` (authored in the CMS form builder) using Formik + Yup, with sensitive-field encryption and Tailwind styling.
4
+
5
+ **Exports**: `DynamicForm`, `DynamicFormSection`, `FormDefinition` (type), `buildYupSchema`, `buildInitialValues`, plus transform / crypto / email-template helpers.
6
+
7
+ **Field types**: `text`, `email`, `phone`, `textarea`, `select`, `checkbox`, `radio`, `number`, `date`, `datetime`, `url`, `address`, `hidden`.
8
+
9
+ ## Integration
10
+
11
+ Wire forms into a consumer site (Next.js App Router):
12
+
13
+ **1. Add the dependency** (`package.json`):
14
+ ```json
15
+ "@vulcantech/forms": "*"
16
+ ```
17
+
18
+ **2. Transpile + alias it** (`next.config.js`):
19
+ ```js
20
+ transpilePackages: ["@vulcantech/forms", /* ... */],
21
+ webpack: (config) => {
22
+ config.resolve.alias["@vulcantech/forms"] = path.join(__dirname, "../../packages/forms");
23
+ return config;
24
+ },
25
+ turbopack: {
26
+ resolveAlias: { "@vulcantech/forms": "./../../packages/forms/index.tsx" },
27
+ },
28
+ ```
29
+
30
+ **3. Tailwind content (required)** — add the package source to Tailwind, or the renderer's classes won't generate and forms render unstyled. Tailwind v4 (in the app's `globals.css` / `tailwind.css`); adjust the relative path to the app's depth:
31
+ ```css
32
+ @source "../../../../packages/forms/src";
33
+ ```
34
+ (JS config: add `"../../packages/forms/src/**/*.{ts,tsx}"` to `content`.)
35
+
36
+ **4. Server fetcher** `src/lib/cms/forms.ts` — ISR-cached wrapper over the CMS `/api/forms` (copy from a reference app, e.g. dry-ice / eurocomp). Exposes `getFormById(id)` / `getFormBySlug(slug)`; tags each fetch `form:{projectId}:{formId}` + `forms:{projectId}`; returns `null` on failure so the page still renders.
37
+
38
+ **5. Per-form loader** `src/lib/cms/load<Name>Form.ts` — pin the form's Firestore id and wrap the fetcher with a fallback:
39
+ ```ts
40
+ import { getFormById } from "./forms";
41
+ export const CONTACT_FORM_ID = "<firestore-doc-id-from-CMS>";
42
+ export async function loadContactForm() {
43
+ try { return (await getFormById(CONTACT_FORM_ID)) ?? undefined; }
44
+ catch { return undefined; }
45
+ }
46
+ ```
47
+
48
+ **6. Render** — load server-side, pass to a `"use client"` wrapper around `<DynamicForm>`:
49
+ ```tsx
50
+ // page.tsx (server component)
51
+ const definition = await loadContactForm();
52
+ return <ContactForm definition={definition} />;
53
+
54
+ // ContactForm.tsx ("use client")
55
+ import { DynamicForm, type FormDefinition } from "@vulcantech/forms";
56
+ export function ContactForm({ definition }: { definition?: FormDefinition }) {
57
+ return <DynamicForm definition={definition ?? FALLBACK_DEFINITION} apiRoute="/api/contact" />;
58
+ }
59
+ ```
60
+
61
+ **7. ISR invalidation** — ensure `src/app/api/revalidate/route.ts` exists; the CMS purges `form:{projectId}:{formId}` (single) and `forms:{projectId}` (list) on edit.
62
+
63
+ ### Required env
64
+ `VULCANTECH_PROJECT_ID`, `VULCANTECH_API_KEY` (required) · `VULCANTECH_CMS_URL` (optional) · `VULCANTECH_FORMS_NO_CACHE` (optional, `1`/`true` to bypass ISR).
65
+
66
+ ### Per-app values
67
+ - **Form definition ID(s)** — Firestore doc id from the CMS admin (one per form), pinned in `load<Name>Form.ts` (e.g. eurocomp `SERVICE_REQUEST_FORM_ID`, castlerents `CONTACT_FORM_ID`).
68
+
69
+ ### Notes
70
+ - `FormDefinition` is the CMS-authored schema (sections, fields, submit config, style).
71
+ - `style.buttonPlacement: "inline"` renders the submit button beside the fields instead of full-width below them — suited to single-field forms (newsletter email + Join). Editable in the CMS form builder under Style → Submit Button → Button Placement.
72
+ - Newsletter forms (`submissionType: "newsletter"`) should submit to an app route that proxies to the CMS `/api/contact` — the CMS routes them into `newsletter_subscribers` (see dry-ice-pro-auto `src/app/api/newsletter/route.ts`).
73
+ - The package also ships `buildYupSchema` / `buildInitialValues` for custom render paths, but consumer sites use the server-fetcher pattern above for ISR caching.