mobx-formly 0.0.3 → 0.1.1

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.
@@ -1,4 +1,4 @@
1
- import { Form } from './form.js';
1
+ import type { Form } from './form.js';
2
2
  import type { FieldValues, FormOptions, FormSchema, SchemaOutput } from './types.js';
3
3
  export type InferredFormValues<S> = Extract<SchemaOutput<S>, FieldValues>;
4
4
  /**
@@ -6,6 +6,12 @@ export type InferredFormValues<S> = Extract<SchemaOutput<S>, FieldValues>;
6
6
  *
7
7
  * [**Documentation**](https://js2me.github.io/mobx-formly/guide/getting-started.html)
8
8
  */
9
- export declare const createForm: <S extends FormSchema<any>>(options: FormOptions<InferredFormValues<S>> & {
9
+ export declare function createForm<S extends FormSchema<any>>(options: FormOptions<InferredFormValues<S>> & {
10
10
  schema: S;
11
- }) => Form<InferredFormValues<S>>;
11
+ }): Form<InferredFormValues<S>>;
12
+ /**
13
+ * Creates a form with an explicit values type.
14
+ *
15
+ * [**Documentation**](https://js2me.github.io/mobx-formly/guide/getting-started.html)
16
+ */
17
+ export declare function createForm<T extends FieldValues = FieldValues>(options?: FormOptions<T>): Form<T>;
@@ -1,7 +1,5 @@
1
- import { Form } from './form.js';
2
- /**
3
- * Creates a form with values inferred from its schema.
4
- *
5
- * [**Documentation**](https://js2me.github.io/mobx-formly/guide/getting-started.html)
6
- */
7
- export const createForm = (options) => new Form(options);
1
+ import { BaseForm } from './form.js';
2
+ // Implementation signature is intentionally loose: callers only see the typed overloads above.
3
+ export function createForm(...args) {
4
+ return new BaseForm(args[0] ?? {});
5
+ }
package/dist/form.d.ts CHANGED
@@ -1,25 +1,215 @@
1
1
  import { type Ref } from 'yummies/mobx';
2
- import type { FieldError, FieldErrors, FieldPath, FieldPathValue, FieldStateTree, FieldValues, FormOptions, RegisterOptions, RegisterReturn, ResetOptions, SetValueConfig, SubmitHandlers } from './types.js';
3
- export declare class Form<T extends FieldValues = FieldValues> {
2
+ import type { ErrorNamespacePath, FieldError, FieldErrors, FieldPath, FieldPathValue, FieldStateTree, FieldValues, FormOptions, RegisterOptions, RegisterReturn, ResetFieldOptions, ResetOptions, SetErrorConfig, SetValueConfig, SubmitHandlers, TriggerConfig } from './types.js';
3
+ /**
4
+ * Public form contract returned by `createForm()` and implemented by `BaseForm`.
5
+ *
6
+ * Coordinates form values, field registration, validation, and submission
7
+ * without any rendering concerns.
8
+ *
9
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html)
10
+ */
11
+ export interface Form<T extends FieldValues = FieldValues> {
4
12
  /**
5
13
  * Current form values.
6
14
  *
7
15
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#values)
8
16
  */
9
17
  values: T;
18
+ /**
19
+ * Cached default values used by reset, resetField, and dirty comparison.
20
+ * Updated by reset unless `keepDefaultValues` is passed.
21
+ *
22
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#defaultvalues)
23
+ */
24
+ defaultValues: T;
10
25
  /**
11
26
  * Validation errors nested by field path.
12
27
  *
13
28
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#errors)
14
29
  */
15
- private readonly errorsByPath;
16
- private readonly errorPathCounts;
17
- private readonly errorChildren;
18
- private readonly errorProxyCache;
19
- private readonly fieldStatesByPath;
20
- private readonly fieldStatePathCounts;
21
- private readonly fieldStateChildren;
22
- private readonly fieldStateProxyCache;
30
+ readonly errors: FieldErrors<T>;
31
+ /**
32
+ * Field paths whose values differ from their defaults.
33
+ *
34
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#dirtyfields)
35
+ */
36
+ dirtyFields: Record<string, true | undefined>;
37
+ /**
38
+ * Field paths that have been touched.
39
+ *
40
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#touchedfields)
41
+ */
42
+ touchedFields: Record<string, true | undefined>;
43
+ /**
44
+ * Field paths that are currently being validated.
45
+ *
46
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#validatingfields)
47
+ */
48
+ validatingFields: Record<string, true | undefined>;
49
+ /**
50
+ * Observable state for each registered field, nested by field path.
51
+ *
52
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#fieldstate)
53
+ */
54
+ readonly fieldState: FieldStateTree<T>;
55
+ /**
56
+ * Whether a submission is currently running.
57
+ *
58
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#issubmitting)
59
+ */
60
+ isSubmitting: boolean;
61
+ /**
62
+ * Whether the form has been submitted at least once.
63
+ *
64
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#issubmitted)
65
+ */
66
+ isSubmitted: boolean;
67
+ /**
68
+ * Whether the latest submission succeeded.
69
+ *
70
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#issubmitsuccessful)
71
+ */
72
+ isSubmitSuccessful: boolean;
73
+ /**
74
+ * Number of submission attempts.
75
+ *
76
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#submitcount)
77
+ */
78
+ submitCount: number;
79
+ /**
80
+ * Refs registered for fields.
81
+ *
82
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#refs)
83
+ */
84
+ readonly refs: Map<string, Ref<HTMLElement | null>>;
85
+ /**
86
+ * Whether registered event handlers ignore changes and blur events.
87
+ *
88
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#disabled)
89
+ */
90
+ readonly disabled: boolean;
91
+ /**
92
+ * Whether any field is dirty.
93
+ *
94
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#isdirty)
95
+ */
96
+ readonly isDirty: boolean;
97
+ /**
98
+ * Whether any field has been touched.
99
+ *
100
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#istouched)
101
+ */
102
+ readonly isTouched: boolean;
103
+ /**
104
+ * Whether any field validation is currently running.
105
+ *
106
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#isvalidating)
107
+ */
108
+ readonly isValidating: boolean;
109
+ /**
110
+ * Whether the form has no errors. With a schema or resolver the first read
111
+ * schedules a full validation pass, so validity reflects the schema instead
112
+ * of defaulting to true until something validates.
113
+ *
114
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#isvalid)
115
+ */
116
+ readonly isValid: boolean;
117
+ /**
118
+ * Returns a plain copy of the current values.
119
+ *
120
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#snapshot)
121
+ */
122
+ readonly snapshot: T;
123
+ /**
124
+ * Registers a field and returns its ref and event handlers.
125
+ *
126
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#registername-options)
127
+ */
128
+ register(name: FieldPath<T>, options?: RegisterOptions<T>): RegisterReturn;
129
+ /**
130
+ * Returns the stable MobX-aware ref for a field path, creating it on demand.
131
+ * The ref can be used by a view adapter before the field is registered.
132
+ */
133
+ ref(name: FieldPath<T>): Ref<HTMLElement | null>;
134
+ /**
135
+ * Removes a field, its value, and its associated state.
136
+ *
137
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#unregistername)
138
+ */
139
+ unregister(name: FieldPath<T>): void;
140
+ /**
141
+ * Updates a field value and optionally changes its state or validates it.
142
+ *
143
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#setvaluename-value-config)
144
+ */
145
+ setValue<P extends FieldPath<T>>(name: P, value: FieldPathValue<T, P>, config?: SetValueConfig): void;
146
+ /**
147
+ * Groups direct value changes, reconciles all dirty paths, and optionally
148
+ * validates the complete form.
149
+ *
150
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#mutatemutator-config)
151
+ */
152
+ mutate(mutator: () => Promise<void>, config?: SetValueConfig): Promise<void>;
153
+ mutate(mutator: () => void, config?: SetValueConfig): void;
154
+ /**
155
+ * Sets an error for a field and can focus it.
156
+ *
157
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#seterrorname-error)
158
+ */
159
+ setError(name: FieldPath<T> | ErrorNamespacePath, error: FieldError, config?: SetErrorConfig): void;
160
+ /**
161
+ * Clears one, several, or all field errors.
162
+ *
163
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#clearerrorsname)
164
+ */
165
+ clearErrors(name?: FieldPath<T> | ErrorNamespacePath | Array<FieldPath<T> | ErrorNamespacePath>): void;
166
+ /**
167
+ * Validates one field, several fields, or the complete form.
168
+ *
169
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#triggername)
170
+ */
171
+ trigger(name?: FieldPath<T> | FieldPath<T>[], config?: TriggerConfig): Promise<boolean>;
172
+ /**
173
+ * Creates an asynchronous submit handler with validation and result callbacks.
174
+ *
175
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#handlesubmithandlers)
176
+ */
177
+ handleSubmit(handlers: SubmitHandlers<T>): () => Promise<void>;
178
+ /**
179
+ * Resets values and selected form state.
180
+ *
181
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#resetvalues-options)
182
+ */
183
+ reset(values?: Partial<T>, options?: ResetOptions): void;
184
+ /**
185
+ * Resets one field to its current default value and clears its state.
186
+ *
187
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#resetfieldname)
188
+ */
189
+ resetField<P extends FieldPath<T>>(name: P, options?: ResetFieldOptions<T, P>): void;
190
+ /**
191
+ * Focuses a registered field when its ref points to a focusable element.
192
+ *
193
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#setfocusname)
194
+ */
195
+ setFocus(name: FieldPath<T>): void;
196
+ }
197
+ export declare class BaseForm<T extends FieldValues = FieldValues> implements Form<T> {
198
+ /**
199
+ * Current form values.
200
+ *
201
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#values)
202
+ */
203
+ values: T;
204
+ /**
205
+ * Cached default values used by reset, resetField, and dirty comparison.
206
+ * Updated by reset unless `keepDefaultValues` is passed.
207
+ *
208
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#defaultvalues)
209
+ */
210
+ defaultValues: T;
211
+ private readonly errorStore;
212
+ private readonly fieldStateStore;
23
213
  /** Validation errors nested by field path. */
24
214
  get errors(): FieldErrors<T>;
25
215
  /**
@@ -41,11 +231,10 @@ export declare class Form<T extends FieldValues = FieldValues> {
41
231
  */
42
232
  validatingFields: Record<string, true | undefined>;
43
233
  /**
44
- * Observable state for each registered field.
234
+ * Observable state for each registered field, nested by field path.
45
235
  *
46
236
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#fieldstate)
47
237
  */
48
- /** Observable state for each registered field, nested by field path. */
49
238
  get fieldState(): FieldStateTree<T>;
50
239
  /**
51
240
  * Whether a submission is currently running.
@@ -77,18 +266,17 @@ export declare class Form<T extends FieldValues = FieldValues> {
77
266
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#refs)
78
267
  */
79
268
  readonly refs: Map<string, Ref<HTMLElement | null, import("yummies/types").AnyObject>>;
80
- private readonly defaultValues;
81
269
  private readonly options;
82
270
  private readonly fieldOptions;
83
- private valueObservers?;
84
- private observerTimer?;
85
- private readonly changedPaths;
86
- private isMutating;
87
- private observerTreeChanged;
271
+ private readonly touchedValidationFields;
272
+ private readonly validator;
88
273
  private activeSubmissions;
89
274
  private resetVersion;
275
+ private readonly isValidOverride;
90
276
  private validationVersion;
91
277
  private readonly fieldValidationVersions;
278
+ /** Whether at least one validation pass has completed since construction or reset. */
279
+ private hasValidationRun;
92
280
  /** Creates a form with optional initial values, schema, and validation settings.
93
281
  *
94
282
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#constructor-options)
@@ -107,7 +295,17 @@ export declare class Form<T extends FieldValues = FieldValues> {
107
295
  */
108
296
  get isDirty(): boolean;
109
297
  /**
110
- * Whether the form has no errors.
298
+ * Whether any field has been touched.
299
+ *
300
+ * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#istouched)
301
+ */
302
+ get isTouched(): boolean;
303
+ /** Whether any field validation is currently running. */
304
+ get isValidating(): boolean;
305
+ /**
306
+ * Whether the form has no errors. With a schema or resolver the first read
307
+ * schedules a full validation pass, so validity reflects the schema instead
308
+ * of defaulting to true until something validates.
111
309
  *
112
310
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#isvalid)
113
311
  */
@@ -118,6 +316,11 @@ export declare class Form<T extends FieldValues = FieldValues> {
118
316
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#registername-options)
119
317
  */
120
318
  register(name: FieldPath<T>, options?: RegisterOptions<T>): RegisterReturn;
319
+ /**
320
+ * Returns the stable MobX-aware ref for a field path, creating it on demand.
321
+ * The ref can be used by a view adapter before the field is registered.
322
+ */
323
+ ref(name: FieldPath<T>): Ref<HTMLElement | null>;
121
324
  /**
122
325
  * Removes a field, its value, and its associated state.
123
326
  *
@@ -131,34 +334,34 @@ export declare class Form<T extends FieldValues = FieldValues> {
131
334
  */
132
335
  setValue<P extends FieldPath<T>>(name: P, value: FieldPathValue<T, P>, config?: SetValueConfig): void;
133
336
  /**
134
- * Groups direct value changes and processes their changed paths together.
337
+ * Groups direct value changes, reconciles all dirty paths, and optionally
338
+ * validates the complete form.
135
339
  *
136
340
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#mutatemutator-config)
137
341
  */
342
+ mutate(mutator: () => Promise<void>, config?: SetValueConfig): Promise<void>;
138
343
  mutate(mutator: () => void, config?: SetValueConfig): void;
344
+ private finishMutation;
139
345
  private applyValueChange;
140
- private ensureValueObservers;
141
- private scheduleObserverCleanup;
142
- private disposeValueObservers;
143
- private observeValueTree;
144
346
  /**
145
- * Sets an error for a field.
347
+ * Sets an error for a field and can focus it.
146
348
  *
147
349
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#seterrorname-error)
148
350
  */
149
- setError(name: FieldPath<T>, error: FieldError): void;
351
+ setError(name: FieldPath<T> | ErrorNamespacePath, error: FieldError, config?: SetErrorConfig): void;
150
352
  /**
151
353
  * Clears one, several, or all field errors.
152
354
  *
153
355
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#clearerrorsname)
154
356
  */
155
- clearErrors(name?: FieldPath<T> | FieldPath<T>[]): void;
357
+ clearErrors(name?: FieldPath<T> | ErrorNamespacePath | Array<FieldPath<T> | ErrorNamespacePath>): void;
156
358
  /**
157
359
  * Validates one field, several fields, or the complete form.
158
360
  *
159
361
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#triggername)
160
362
  */
161
- trigger(name?: FieldPath<T> | FieldPath<T>[]): Promise<boolean>;
363
+ trigger(name?: FieldPath<T> | FieldPath<T>[], config?: TriggerConfig): Promise<boolean>;
364
+ private runValidation;
162
365
  /**
163
366
  * Creates an asynchronous submit handler with validation and result callbacks.
164
367
  *
@@ -176,7 +379,7 @@ export declare class Form<T extends FieldValues = FieldValues> {
176
379
  *
177
380
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#resetfieldname)
178
381
  */
179
- resetField(name: FieldPath<T>): void;
382
+ resetField<P extends FieldPath<T>>(name: P, options?: ResetFieldOptions<T, P>): void;
180
383
  /**
181
384
  * Focuses a registered field when its ref points to a focusable element.
182
385
  *
@@ -189,25 +392,20 @@ export declare class Form<T extends FieldValues = FieldValues> {
189
392
  * [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#snapshot)
190
393
  */
191
394
  get snapshot(): T;
395
+ /** Reconciles dirty paths against the complete current value tree. */
396
+ private syncDirtyFields;
397
+ /** Clears an exact error path and every nested error below it. */
398
+ private clearErrorPath;
399
+ /** Focuses the first errored registered field, skipping form-level errors. */
400
+ private focusFirstError;
401
+ private dependentFields;
192
402
  private markTouched;
193
403
  private updateDirty;
194
404
  private shouldValidateOnChange;
195
405
  private isValidationCurrent;
196
406
  private transformValue;
197
- private validateSchema;
198
- private normalizeSchemaErrors;
199
- private validateRules;
200
407
  private ensureFieldState;
201
408
  private applyFieldState;
202
409
  private applyError;
203
- private getError;
204
410
  private hasError;
205
- private errorPaths;
206
- private deleteFieldState;
207
- private fieldStates;
208
- private setPathStore;
209
- private deletePathStore;
210
- private clearPathStore;
211
- private addPathToIndex;
212
- private createPathProxy;
213
411
  }