@samline/forms 2.4.0 → 2.7.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.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -8
  3. package/dist/browser/global.d.ts +28 -18
  4. package/dist/browser/global.global.js +477 -66
  5. package/dist/browser/global.global.js.map +1 -1
  6. package/dist/browser/index.cjs +1450 -0
  7. package/dist/browser/index.cjs.map +1 -0
  8. package/dist/browser/index.d.cts +14 -0
  9. package/dist/browser/index.d.ts +14 -0
  10. package/dist/browser/index.js +23 -0
  11. package/dist/browser/index.js.map +1 -0
  12. package/dist/chunk-WGPAVJTK.js +1415 -0
  13. package/dist/chunk-WGPAVJTK.js.map +1 -0
  14. package/dist/index.cjs +296 -40
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +4 -158
  17. package/dist/index.d.ts +4 -158
  18. package/dist/index.js +7 -1148
  19. package/dist/index.js.map +1 -1
  20. package/dist/registry-GzopYPCR.d.cts +164 -0
  21. package/dist/registry-GzopYPCR.d.ts +164 -0
  22. package/docs/api/add-cleanup.md +52 -0
  23. package/docs/api/append.md +96 -0
  24. package/docs/api/auto-submit.md +91 -0
  25. package/docs/api/clear-errors.md +69 -0
  26. package/docs/api/destroy.md +80 -0
  27. package/docs/api/disable-auto-submit.md +65 -0
  28. package/docs/api/element.md +78 -0
  29. package/docs/api/form.md +156 -0
  30. package/docs/api/format.md +270 -0
  31. package/docs/api/get-data.md +90 -0
  32. package/docs/api/get-field.md +87 -0
  33. package/docs/api/get-state.md +111 -0
  34. package/docs/api/get-value.md +114 -0
  35. package/docs/api/index.md +55 -0
  36. package/docs/api/observe.md +126 -0
  37. package/docs/api/on-submit.md +94 -0
  38. package/docs/api/parse-form-data.md +94 -0
  39. package/docs/api/prefill.md +89 -0
  40. package/docs/api/regex.md +155 -0
  41. package/docs/api/reset.md +61 -0
  42. package/docs/api/revalidate.md +52 -0
  43. package/docs/api/set-errors.md +97 -0
  44. package/docs/api/set-value.md +120 -0
  45. package/docs/api/subscribe.md +107 -0
  46. package/docs/api/unwatch.md +74 -0
  47. package/docs/api/validate-field-value.md +144 -0
  48. package/docs/api/validate-values.md +103 -0
  49. package/docs/api/validate.md +102 -0
  50. package/docs/api/watch.md +117 -0
  51. package/docs/browser.md +156 -0
  52. package/docs/css-styling.md +191 -0
  53. package/docs/getting-started.md +222 -0
  54. package/docs/options.md +326 -0
  55. package/docs/recipes.md +530 -0
  56. package/docs/typescript.md +615 -0
  57. package/docs/vanilla.md +12 -0
  58. package/package.json +58 -14
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Samuel Olvera (samline)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Forms
2
2
 
3
- > A small, framework-free form controller for vanilla JS and direct browser usage.
3
+ > A TypeScript-first, framework-free controller for native HTML forms.
4
4
 
5
- > It binds to an `HTMLFormElement`, keeps field state in sync with the DOM, runs validation, lets you react to changes via watchers or subscribers, and ships a serialized payload for `fetch` flows.
5
+ > Add validation, reactive field state, accessible error handling, serialization, auto-submit, and optional input formatting without adopting a UI framework.
6
6
 
7
7
  ---
8
8
 
@@ -42,10 +42,10 @@ Requires Node 20+ when bundling. Runtime target is ES2020.
42
42
  Use the browser build when you do not have a bundler and need to run the package directly in HTML, Shopify, WordPress, or any traditional template.
43
43
 
44
44
  ```html
45
- <script src="https://unpkg.com/@samline/forms@2.4.0/dist/browser/global.global.js"></script>
45
+ <script src="https://unpkg.com/@samline/forms@2.7.0/dist/browser/global.global.js"></script>
46
46
  ```
47
47
 
48
- > Pin the version in production. Replace `2.4.0` with the version you ship.
48
+ > Pin the version in production. Replace `2.7.0` with the version you ship.
49
49
 
50
50
  The browser bundle exposes a single global: `window.Forms`.
51
51
 
@@ -55,7 +55,7 @@ The browser bundle exposes a single global: `window.Forms`.
55
55
  <button type="submit">Send</button>
56
56
  </form>
57
57
 
58
- <script src="https://unpkg.com/@samline/forms@2.4.0/dist/browser/global.global.js"></script>
58
+ <script src="https://unpkg.com/@samline/forms@2.7.0/dist/browser/global.global.js"></script>
59
59
  <script>
60
60
  const contactForm = window.Forms.newForm({ id: 'contact-form' })
61
61
 
@@ -78,7 +78,8 @@ See [docs/browser.md](docs/browser.md) for the full browser surface.
78
78
  | Entrypoint | When to use |
79
79
  | --- | --- |
80
80
  | `@samline/forms` | Main vanilla API for bundlers, ESM, or CJS consumers. |
81
- | `@samline/forms/browser` | Pre-bundled IIFE that registers `window.Forms` for direct `<script>` usage. |
81
+ | `@samline/forms/browser` | ESM/CJS browser module with registry exports, bundled types, and `window.Forms` registration. |
82
+ | `@samline/forms/browser/global` | Pre-bundled IIFE for direct `<script>` usage; the CDN URL resolves to `dist/browser/global.global.js`. |
82
83
 
83
84
  The vanilla entrypoint also exports `browser`, the same `{ form, newForm, destroyForm, available }` surface as the IIFE but as a module-level singleton (no `globalThis` side-effect). Use it from a bundler when you want the registry helpers without the IIFE — see [docs/browser.md → Using the same shape from a bundler](docs/browser.md#using-the-same-shape-from-a-bundler).
84
85
 
@@ -133,7 +134,7 @@ The controller is built around one factory and a small set of focused methods. M
133
134
 
134
135
  | Group | Methods |
135
136
  | --- | --- |
136
- | Lifecycle | [`form`](docs/api/form.md) · [`destroy`](docs/api/destroy.md) · [`reset`](docs/api/reset.md) |
137
+ | Lifecycle | [`form`](docs/api/form.md) · [`addCleanup`](docs/api/add-cleanup.md) · [`destroy`](docs/api/destroy.md) · [`reset`](docs/api/reset.md) |
137
138
  | Registry (vanilla) | [`browser`](docs/getting-started.md#browser-registry-helpers) — bundler-friendly `{ form, newForm, destroyForm, available }` singleton. |
138
139
  | Properties | [`element`](docs/api/element.md) · [`options`](docs/options.md) |
139
140
  | Submission | [`onSubmit`](docs/api/on-submit.md) · [`autoSubmit`](docs/api/auto-submit.md) · [`disableAutoSubmit`](docs/api/disable-auto-submit.md) |
@@ -143,6 +144,8 @@ The controller is built around one factory and a small set of focused methods. M
143
144
  | State and data | [`getData`](docs/api/get-data.md) · [`getState`](docs/api/get-state.md) · [`append`](docs/api/append.md) |
144
145
  | Pure helpers | [`parseFormData`](docs/api/parse-form-data.md) · [`validateValues`](docs/api/validate-values.md) · [`validateFieldValue`](docs/api/validate-field-value.md) |
145
146
 
147
+ Validation supports explicit cross-field dependencies with `dependsOn`, per-member collection rules with `each`, and strict decimal `numeric` / inclusive `min` / `max` rules. Async `onSubmit` handlers are reflected by `getState().isSubmitting`, while `addCleanup()` lets integrations register controller-owned teardown work.
148
+
146
149
  See the full per-method reference in [`docs/api/`](docs/api/index.md).
147
150
 
148
151
  ---
@@ -177,4 +180,4 @@ Full API reference, guides, and examples are available at **[samline.github.io/f
177
180
 
178
181
  ## License
179
182
 
180
- MIT
183
+ MIT
@@ -2,23 +2,11 @@ type FormatType = 'general' | 'phone' | 'numeral' | 'date' | 'time' | 'creditCar
2
2
  interface FieldFormatConfig {
3
3
  /** One of the supported `@samline/formatter` `FormatType` values. */
4
4
  type: FormatType;
5
- /**
6
- * Canonical name of the formatted field. The value the backend
7
- * ultimately receives (the `raw` output of the formatter) is
8
- * exposed under this name; the formatter is responsible for
9
- * keeping it in sync with the visible input.
10
- */
5
+ /** Canonical field name or fields whose raw values are submitted. */
11
6
  field: string | string[];
12
7
  /**
13
- * Name of the visible input that shows the formatted value to
14
- * the user. Defaults to `${fieldName}_displayed`.
15
- *
16
- * The visible is **renamed** from `field` to `displayField` on
17
- * the first `format()` call (idempotent on re-binding). A hidden
18
- * `<input type="hidden" name="<field>">` is created next to it
19
- * to carry the raw value. Both names are first-class in the
20
- * controller's API (`getValue`, `getField`, `setValue`, `watch`,
21
- * `getData`) — see `docs/api/format.md` for the full contract.
8
+ * Name of the visible input for a single field. Array configurations
9
+ * must omit this option and derive one display name per field.
22
10
  */
23
11
  displayField?: string;
24
12
  /**
@@ -42,15 +30,30 @@ interface FieldValidationContext {
42
30
  field: string;
43
31
  value: FormFieldValue;
44
32
  values: FormValues;
33
+ /** Concrete control while validating one member through `each`. */
34
+ element?: FormFieldElement;
35
+ /** Zero-based member position while validating through `each`. */
36
+ index?: number;
45
37
  }
46
38
  type FieldValidator = (context: FieldValidationContext) => string | undefined | null | false | true;
47
- interface FieldValidationRules {
39
+ interface ValueValidationRules {
48
40
  required?: RuleConfig<boolean>;
49
41
  minLength?: RuleConfig<number>;
50
42
  maxLength?: RuleConfig<number>;
51
43
  pattern?: RuleConfig<RegExp>;
44
+ numeric?: RuleConfig<boolean>;
45
+ min?: RuleConfig<number>;
46
+ max?: RuleConfig<number>;
52
47
  validate?: FieldValidator | FieldValidator[];
53
48
  }
49
+ interface FieldValidationRules extends ValueValidationRules {
50
+ /** Field name whose non-empty value must equal this field's value. */
51
+ sameAs?: RuleConfig<string>;
52
+ /** Fields whose changes should revalidate this field. */
53
+ dependsOn?: string | string[];
54
+ /** Rules applied independently to each collection member. */
55
+ each?: ValueValidationRules;
56
+ }
54
57
  type ValidationSchema = Record<string, FieldValidationRules>;
55
58
  interface VisualAttributes {
56
59
  filled: string;
@@ -89,6 +92,7 @@ interface FormStateSnapshot {
89
92
  isValid: boolean;
90
93
  isValidated: boolean;
91
94
  autoSubmit: boolean;
95
+ isSubmitting: boolean;
92
96
  submitCount: number;
93
97
  }
94
98
  type FormStateListener = (state: FormStateSnapshot) => void;
@@ -103,12 +107,14 @@ interface AppendContentOptions {
103
107
  atStart?: boolean;
104
108
  }
105
109
  type FormFieldWatcher = (value: FormFieldValue, field: FormFieldElement | FormFieldElement[] | null, form: HTMLFormElement, state: FormStateSnapshot) => void;
106
- type FormSubmitHandler = (form: HTMLFormElement, data: Record<string, SerializedFormValue>, formData: FormData, state: FormStateSnapshot) => void;
110
+ type FormSubmitHandler = (form: HTMLFormElement, data: Record<string, SerializedFormValue>, formData: FormData, state: FormStateSnapshot) => void | Promise<void>;
111
+ type FormCleanup = () => void;
107
112
  interface FormController {
108
113
  readonly element: HTMLFormElement | null;
109
114
  readonly f: HTMLFormElement | null;
110
115
  readonly options: FormControllerOptions;
111
116
  onSubmit: (callback: FormSubmitHandler, preventDefault?: boolean) => FormController;
117
+ addCleanup: (cleanup: FormCleanup) => () => void;
112
118
  watch: (field: string, callback: FormFieldWatcher) => FormController;
113
119
  observe: (field: string, callback: FormFieldWatcher) => () => void;
114
120
  unwatch: (field?: string, callback?: FormFieldWatcher) => FormController;
@@ -155,10 +161,14 @@ interface FormsApi {
155
161
  }
156
162
 
157
163
  declare const Forms: FormsApi;
164
+ declare const form: (target: FormTarget, options?: FormControllerOptions) => FormController;
165
+ declare const newForm: (input: NewFormInput) => FormController | undefined;
166
+ declare const destroyForm: (id: string) => void;
167
+ declare const available: FormsAvailable;
158
168
  declare global {
159
169
  interface Window {
160
170
  Forms: FormsApi;
161
171
  }
162
172
  }
163
173
 
164
- export { Forms as default };
174
+ export { available, Forms as default, destroyForm, form, newForm };