@samline/forms 2.7.2 → 2.9.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/CHANGELOG.md +316 -0
  2. package/README.md +119 -119
  3. package/dist/browser/global.d.ts +50 -16
  4. package/dist/browser/global.global.js +12 -6188
  5. package/dist/browser/global.global.js.map +1 -1
  6. package/dist/browser/index.cjs +340 -84
  7. package/dist/browser/index.cjs.map +1 -1
  8. package/dist/browser/index.d.cts +1 -1
  9. package/dist/browser/index.d.ts +1 -1
  10. package/dist/browser/index.js +1 -1
  11. package/dist/{chunk-MMHX34WB.js → chunk-6MA5TF6R.js} +341 -85
  12. package/dist/chunk-6MA5TF6R.js.map +1 -0
  13. package/dist/index.cjs +340 -84
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.cts +2 -2
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.js +1 -1
  18. package/dist/{registry-GzopYPCR.d.ts → registry-CPe_lf8-.d.cts} +50 -4
  19. package/dist/{registry-GzopYPCR.d.cts → registry-CPe_lf8-.d.ts} +50 -4
  20. package/package.json +39 -8
  21. package/dist/chunk-MMHX34WB.js.map +0 -1
  22. package/docs/api/add-cleanup.md +0 -52
  23. package/docs/api/append.md +0 -96
  24. package/docs/api/auto-submit.md +0 -91
  25. package/docs/api/clear-errors.md +0 -69
  26. package/docs/api/destroy.md +0 -80
  27. package/docs/api/disable-auto-submit.md +0 -65
  28. package/docs/api/element.md +0 -78
  29. package/docs/api/form.md +0 -156
  30. package/docs/api/format.md +0 -270
  31. package/docs/api/get-data.md +0 -90
  32. package/docs/api/get-field.md +0 -87
  33. package/docs/api/get-state.md +0 -111
  34. package/docs/api/get-value.md +0 -114
  35. package/docs/api/index.md +0 -55
  36. package/docs/api/observe.md +0 -126
  37. package/docs/api/on-submit.md +0 -94
  38. package/docs/api/parse-form-data.md +0 -94
  39. package/docs/api/prefill.md +0 -89
  40. package/docs/api/regex.md +0 -155
  41. package/docs/api/reset.md +0 -61
  42. package/docs/api/revalidate.md +0 -52
  43. package/docs/api/set-errors.md +0 -97
  44. package/docs/api/set-value.md +0 -120
  45. package/docs/api/subscribe.md +0 -107
  46. package/docs/api/unwatch.md +0 -74
  47. package/docs/api/validate-field-value.md +0 -144
  48. package/docs/api/validate-values.md +0 -103
  49. package/docs/api/validate.md +0 -102
  50. package/docs/api/watch.md +0 -117
  51. package/docs/browser.md +0 -156
  52. package/docs/css-styling.md +0 -191
  53. package/docs/getting-started.md +0 -222
  54. package/docs/options.md +0 -326
  55. package/docs/recipes.md +0 -531
  56. package/docs/typescript.md +0 -615
  57. package/docs/vanilla.md +0 -12
  58. /package/{LICENSE → LICENSE.md} +0 -0
@@ -106,7 +106,38 @@ interface AppendContentOptions {
106
106
  class?: string;
107
107
  atStart?: boolean;
108
108
  }
109
+ /** One `<option>` written by `setOptions()`. */
110
+ interface SelectOption {
111
+ value: string | number;
112
+ /** Visible text. Defaults to `String(value)`. Written as text, never HTML. */
113
+ label?: string;
114
+ disabled?: boolean;
115
+ /** Selects the option and makes it the default restored by `reset()`. */
116
+ selected?: boolean;
117
+ }
118
+ /** One `<optgroup>` written by `setOptions()`. */
119
+ interface SelectOptionGroup {
120
+ label: string;
121
+ options: Array<string | number | SelectOption>;
122
+ disabled?: boolean;
123
+ }
124
+ /** A plain string or number is shorthand for `{ value, label: String(value) }`. */
125
+ type SelectOptionInput = string | number | SelectOption | SelectOptionGroup;
126
+ interface SetOptionsConfig {
127
+ /**
128
+ * Keep the current selection when its value is still in the new list.
129
+ * Ignored for a select when any new option has `selected: true`.
130
+ * Defaults to `true`.
131
+ */
132
+ keepValue?: boolean;
133
+ }
109
134
  type FormFieldWatcher = (value: FormFieldValue, field: FormFieldElement | FormFieldElement[] | null, form: HTMLFormElement, state: FormStateSnapshot) => void;
135
+ /**
136
+ * Callback for `watch()` / `observe()` with a list of fields. `values` has
137
+ * one entry per watched field. `changed` is the field that triggered the
138
+ * call, or `null` on registration and after `reset()`.
139
+ */
140
+ type FormFieldsWatcher = (values: FormValues, changed: string | null, form: HTMLFormElement, state: FormStateSnapshot) => void;
110
141
  type FormSubmitHandler = (form: HTMLFormElement, data: Record<string, SerializedFormValue>, formData: FormData, state: FormStateSnapshot) => void | Promise<void>;
111
142
  type FormCleanup = () => void;
112
143
  interface FormController {
@@ -115,15 +146,30 @@ interface FormController {
115
146
  readonly options: FormControllerOptions;
116
147
  onSubmit: (callback: FormSubmitHandler, preventDefault?: boolean) => FormController;
117
148
  addCleanup: (cleanup: FormCleanup) => () => void;
118
- watch: (field: string, callback: FormFieldWatcher) => FormController;
119
- observe: (field: string, callback: FormFieldWatcher) => () => void;
120
- unwatch: (field?: string, callback?: FormFieldWatcher) => FormController;
149
+ watch: {
150
+ (field: string, callback: FormFieldWatcher): FormController;
151
+ (fields: string[], callback: FormFieldsWatcher): FormController;
152
+ };
153
+ observe: {
154
+ (field: string, callback: FormFieldWatcher): () => void;
155
+ (fields: string[], callback: FormFieldsWatcher): () => void;
156
+ };
157
+ unwatch: {
158
+ (field?: string, callback?: FormFieldWatcher): FormController;
159
+ (fields: string[], callback?: FormFieldsWatcher): FormController;
160
+ };
121
161
  subscribe: (listener: FormStateListener) => () => void;
122
162
  prefill: (fieldName?: string) => FormController;
123
163
  append: (options: AppendContentOptions) => HTMLElement | null;
124
164
  setErrors: (fields: string[] | FormErrors) => FormController;
125
165
  clearErrors: (fields?: string[]) => FormController;
126
166
  setValue: (name: string, value: unknown) => FormController;
167
+ /**
168
+ * Replace the `<option>` / `<optgroup>` children of every `<select>`
169
+ * named `name`. Fires the normal change pipeline only when the
170
+ * selected value changes. Chainable.
171
+ */
172
+ setOptions: (name: string, options: SelectOptionInput[], config?: SetOptionsConfig) => FormController;
127
173
  validate: (fields?: string[]) => ValidationResult;
128
174
  revalidate: (fields?: string[]) => ValidationResult;
129
175
  /**
@@ -153,22 +199,10 @@ interface NewFormInput {
153
199
  interface FormsAvailable {
154
200
  [id: string]: FormController;
155
201
  }
156
- interface FormsApi {
157
- form: (target: FormTarget, options?: FormControllerOptions) => FormController;
158
- newForm: (input: NewFormInput) => FormController | undefined;
159
- destroyForm: (id: string) => void;
160
- available: FormsAvailable;
161
- }
162
202
 
163
- declare const Forms: FormsApi;
164
203
  declare const form: (target: FormTarget, options?: FormControllerOptions) => FormController;
165
204
  declare const newForm: (input: NewFormInput) => FormController | undefined;
166
205
  declare const destroyForm: (id: string) => void;
167
206
  declare const available: FormsAvailable;
168
- declare global {
169
- interface Window {
170
- Forms: FormsApi;
171
- }
172
- }
173
207
 
174
- export { available, Forms as default, destroyForm, form, newForm };
208
+ export { available, destroyForm, form, newForm };