@punica/form-mcp 0.5.0 → 0.6.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.
@@ -0,0 +1,63 @@
1
+ import type { FormController } from '@punica/form';
2
+ import { type JsonFormItem, type JsonFormValues, type RuleDefinition } from '@punica/form-json';
3
+ /**
4
+ * A form you draw yourself (your own markup, @punica/components or native
5
+ * controls) whose values, rules and submit are Punica Form's: the engine
6
+ * holds the value of each named control as the person types or chooses; a
7
+ * submit checks the engine's rules and each control's own constraints (in the
8
+ * words the control already uses), shows what is wrong on the control's
9
+ * `punica-field`, focuses the first wrong control, holds the submit buttons
10
+ * while it works, and hands on the values. A control of the form the engine
11
+ * was not given still holds to its own constraints.
12
+ *
13
+ * ```ts
14
+ * import { bindForm } from '@punica/form-kit/dom';
15
+ *
16
+ * const form = bindForm(element, {
17
+ * fields: { name: nameInput, amount: amountInput },
18
+ * rules: { amount: [{ rule: 'pattern', value: '^[0-9]+$', message: 'A whole number.' }] },
19
+ * onSubmit: (values) => save(values),
20
+ * alert: errorLine
21
+ * });
22
+ * ```
23
+ */
24
+ /** A control of a hand-drawn form: a @punica/components field control (or a native one) with a value. */
25
+ export type BoundControl = HTMLElement & {
26
+ value?: unknown;
27
+ checked?: boolean;
28
+ disabled?: boolean;
29
+ validity?: ValidityState;
30
+ validationMessage?: string;
31
+ checkValidity?: () => boolean;
32
+ setCustomValidity?: (message: string) => void;
33
+ updateComplete?: Promise<unknown>;
34
+ };
35
+ export interface BindFormOptions {
36
+ /** The form's controls, by the name each value is handed on under. */
37
+ fields: Record<string, BoundControl>;
38
+ /** Rules of the engine beyond what each control says itself: `{ rule, value?, message? }`. */
39
+ rules?: Record<string, RuleDefinition[]>;
40
+ /** Called with the values when every rule and every control holds. What it throws is said in `alert` (or `failed`). */
41
+ onSubmit: (values: Record<string, unknown>) => unknown;
42
+ /** Called when a submit failed, instead of saying it in `alert`. */
43
+ failed?: (err: unknown) => void;
44
+ /** Where a failure is said. */
45
+ alert?: HTMLElement;
46
+ /** After a submit that went through, put every field back to where the form started. */
47
+ reset?: boolean;
48
+ /**
49
+ * `false` for a form whose buttons decide how it goes (several ways through, Ctrl+Enter): the browser's and the
50
+ * controls' implicit submit (Enter in a field) is let go, and only `submit()` sends it. `true` by default.
51
+ */
52
+ submitOnEnter?: boolean;
53
+ }
54
+ export interface BoundForm {
55
+ ready: Promise<FormController<JsonFormValues, JsonFormItem>>;
56
+ submit: () => Promise<boolean>;
57
+ values: () => Promise<Record<string, unknown>>;
58
+ set: (name: string, value: unknown) => Promise<void>;
59
+ dispose: () => void;
60
+ }
61
+ export declare function bindForm(form: HTMLFormElement, opts: BindFormOptions): BoundForm;
62
+ /** `bindForm`, handing the form back: for a form built inside an expression. */
63
+ export declare function boundForm(form: HTMLFormElement, opts: BindFormOptions): HTMLFormElement;
@@ -1 +1 @@
1
- {"version":"0.4.0"}
1
+ {"version":"0.5.0"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@punica/form-mcp",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "MCP server for Punica Form: look up the exact API of its packages, read the rules and check code that uses them.",
5
5
  "keywords": [
6
6
  "mcp",
@@ -43,7 +43,7 @@
43
43
  "@punica/form": "^2.3.0",
44
44
  "@punica/form-service": "^2.3.0",
45
45
  "@punica/form-react": "^0.2.0",
46
- "@punica/form-kit": "^0.4.0",
46
+ "@punica/form-kit": "^0.5.0",
47
47
  "@punica/form-schema": "^0.1.0"
48
48
  },
49
49
  "scripts": {
package/src/guide.md CHANGED
@@ -91,6 +91,34 @@ and runs it.
91
91
  - `fromJsonSchema(schema)` turns a flat JSON Schema (string, number, integer,
92
92
  boolean and enum properties) into a definition.
93
93
 
94
+ ## A form drawn by hand, without React
95
+
96
+ When the markup is your own (a page drawn with the DOM, a framework other
97
+ than React, server-rendered HTML), bind the form to the engine with
98
+ `bindForm` from `@punica/form-kit/dom` instead of rewriting it as a class.
99
+ It takes the controls by name (@punica/components elements or native ones);
100
+ the engine holds their values, checks its rules and each control's own
101
+ constraints on submit, shows errors on each control's `punica-field`,
102
+ focuses the first wrong control and holds the submit buttons while it works.
103
+
104
+ ```ts
105
+ import { bindForm } from '@punica/form-kit/dom';
106
+
107
+ const form = bindForm(document.querySelector('form')!, {
108
+ fields: { email: emailInput, seats: seatsInput },
109
+ rules: { seats: [{ rule: 'min', value: 1, message: 'At least one seat.' }] },
110
+ onSubmit: (values) => api.post('orders', { json: values }),
111
+ alert: document.querySelector<HTMLElement>('.form-error')!
112
+ });
113
+ ```
114
+
115
+ - `onSubmit` runs only when everything holds; what it throws is shown in
116
+ `alert` (or given to `failed`).
117
+ - `submitOnEnter: false` for a form whose buttons decide (several actions):
118
+ call `form.submit()` from each button.
119
+ - `form.set(name, value)` writes into the engine and the control;
120
+ `form.dispose()` before the form is drawn again.
121
+
94
122
  ## Lifecycle
95
123
 
96
124
  ```ts
package/src/main.mjs CHANGED
@@ -12,6 +12,7 @@ const packages = [
12
12
  loadPackage('@punica/form-kit'),
13
13
  loadPackage('@punica/form-kit', process.cwd(), 'react'),
14
14
  loadPackage('@punica/form-kit', process.cwd(), 'json'),
15
+ loadPackage('@punica/form-kit', process.cwd(), 'dom'),
15
16
  loadPackage('@punica/form-json'),
16
17
  loadPackage('@punica/form-schema')
17
18
  ].filter(Boolean);